| SpreadControl | ユーザーコントロールに線と文字を表示してテキストボックス1つで入力し、 2次元インデクサでアクセスできるようにした例です。 |
| SpreadTest | SpreadControl を配置して5x5のスプレッドシートです。 (1,1)に初期値を入れています。 button1 を押すとデバッガ出力でスプレッドシートの入力値が確認できます。 |
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace SpreadControl
{
/// <summary>
/// UserControl1 の概要の説明です。
/// </summary>
public class SpreadControl : System.Windows.Forms.UserControl
{
private int columns ;
private int rows ;
private int preferredColumnWidth ;
private int preferredRowHeight ;
private int currentColumnIndex ;
private int currentRowIndex ;
private string[,] text ;
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// 必要なデザイナ変数です。
/// </summary>
private System.ComponentModel.Container components = null;
public SpreadControl()
{
// この呼び出しは、Windows.Forms フォーム デザイナで必要です。
InitializeComponent();
// TODO: InitComponent 呼び出しの後に初期化処理を追加してください。
columns = 3 ;
rows = 2 ;
preferredColumnWidth = 75 ;
preferredRowHeight = 15 ;
currentColumnIndex = 0 ;
currentRowIndex = 0 ;
text = new string[rows,columns] ;
textBox1.BorderStyle = BorderStyle.None ;
textBox1.Visible = false ;
textBox1.Size = new Size(preferredColumnWidth, preferredRowHeight) ;
if ( preferredColumnWidth < textBox1.Size.Width )
{
preferredColumnWidth = textBox1.Size.Width + 1 ;
}
if ( preferredRowHeight <= textBox1.Size.Height )
{
preferredRowHeight = textBox1.Size.Height + 1 ;
}
}
/// <summary>
/// 使用されているリソースに後処理を実行します。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
#region コンポーネント デザイナで生成されたコード
/// <summary>
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード]エディタで変更しないでください。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(0, 24);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "textBox1";
//
// SpreadControl
//
this.Controls.Add(this.textBox1);
this.Name = "SpreadControl";
this.Enter += new System.EventHandler(this.SpreadControl_Enter);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.SpreadControl_Paint);
this.Leave += new System.EventHandler(this.SpreadControl_Leave);
this.ResumeLayout(false);
}
#endregion
private void ResizeData()
{
string[,] newtext = new string[rows,columns] ;
for ( int i=0 ; i<text.GetLength(0) ; i++ )
{
if ( i >= rows )
{
break ;
}
for ( int j=0 ; j<text.GetLength(1) ; j++ )
{
if ( j >= columns )
{
break ;
}
newtext[i,j] = text[i,j] ;
}
}
text = newtext ;
Refresh() ;
}
private void EnterTextBox()
{
textBox1.Location = new Point( currentColumnIndex * preferredColumnWidth + 1,
currentRowIndex * preferredRowHeight + 1 ) ;
textBox1.Size = new Size(preferredColumnWidth - 1, preferredRowHeight - 1) ;
textBox1.Visible = true ;
textBox1.Text = text[currentRowIndex,currentColumnIndex] ;
textBox1.Focus() ;
}
private void LeaveTextBox()
{
text[currentRowIndex,currentColumnIndex] = textBox1.Text ;
textBox1.Visible = false ;
}
public int Rows
{
get
{
return rows ;
}
set
{
rows = value ;
ResizeData() ;
}
}
public int Columns
{
get
{
return columns ;
}
set
{
columns = value ;
ResizeData() ;
}
}
public string this [int rowindex,int columnindex]
{
get
{
if ( rowindex >= 0 && rowindex < rows &&
columnindex >= 0 && columnindex < columns )
{
return text[rowindex,columnindex] ;
}
return "" ;
}
set
{
if ( rowindex >= 0 && rowindex < rows &&
columnindex >= 0 && columnindex < columns )
{
text[rowindex,columnindex] = value ;
}
}
}
private void SpreadControl_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
for ( int i=0 ; i<rows ; i++ )
{
for ( int j=0 ; j<columns ; j++ )
{
Rectangle rect = new Rectangle( j * preferredColumnWidth, i * preferredRowHeight,
preferredColumnWidth, preferredRowHeight ) ;
e.Graphics.DrawRectangle(Pens.Black, rect ) ;
e.Graphics.DrawString(text[i,j],this.Font,new SolidBrush(this.ForeColor), rect ) ;
}
}
}
private void SpreadControl_Enter(object sender, System.EventArgs e)
{
EnterTextBox() ;
}
private void SpreadControl_Leave(object sender, System.EventArgs e)
{
LeaveTextBox() ;
Refresh() ;
}
protected override bool ProcessCmdKey(ref Message m,Keys keyData)
{
const int WM_KEYDOWN = 0x0100;
if ( m.Msg == WM_KEYDOWN )
{
if ( keyData == Keys.Tab )
{
LeaveTextBox() ;
currentColumnIndex ++ ;
if ( currentColumnIndex >= columns )
{
currentColumnIndex = 0 ;
currentRowIndex ++ ;
}
if ( currentRowIndex >= rows )
{
currentColumnIndex = columns - 1 ;
currentRowIndex = rows - 1 ;
}
EnterTextBox() ;
return true ;
}
if ( keyData == (Keys.Shift | Keys.Tab) )
{
LeaveTextBox() ;
currentColumnIndex -- ;
if ( currentColumnIndex < 0 )
{
currentColumnIndex = columns - 1 ;
currentRowIndex -- ;
}
if ( currentRowIndex < 0 )
{
currentRowIndex = 0 ;
}
EnterTextBox() ;
return true ;
}
}
return base.ProcessCmdKey(ref m,keyData) ;
}
}
}
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace SpreadTest
{
/// <summary>
/// Form1 の概要の説明です。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private SpreadControl.SpreadControl spreadControl1;
/// <summary>
/// 必要なデザイナ変数です。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows フォーム デザイナ サポートに必要です。
//
InitializeComponent();
//
// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
//
spreadControl1[1,1] = "a" ;
}
/// <summary>
/// 使用されているリソースに後処理を実行します。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows フォーム デザイナで生成されたコード
/// <summary>
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.spreadControl1 = new SpreadControl.SpreadControl();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(304, 112);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// spreadControl1
//
this.spreadControl1.Columns = 5;
this.spreadControl1.Location = new System.Drawing.Point(8, 8);
this.spreadControl1.Name = "spreadControl1";
this.spreadControl1.Rows = 5;
this.spreadControl1.Size = new System.Drawing.Size(376, 80);
this.spreadControl1.TabIndex = 3;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(400, 174);
this.Controls.Add(this.spreadControl1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
for (int i=0 ; i<5 ;i++ )
{
for (int j=0 ; j<5 ; j++ )
{
System.Diagnostics.Debug.WriteLine(spreadControl1[i,j]);
}
}
}
}
}