(2)の続き。
///////////////////////
//入力・編集ダイアログ
///////////////////////
class AddEditDlg : Form
{
//追加データの受け渡し用idItemインスタンス
public idItem iditem;
//編集対象となるDataGridViewRow
DataGridViewRow target;
//ダイアログのコントロール
Button BtnOK, BtnCancel;
Label MsLabel, IdLabel, PwLabel, RmLabel;
TextBox MsBox, IdBox, PwBox, RmBox;
public AddEditDlg(DataGridViewRow dgvr = null)
{
//対象行を保存(解説:呼び出したidListクラスの行情報です。)
target = dgvr;
//ダイアログの属性設定
this.Text = ReferenceEquals(target, null) ? "データの追加" : "データの編集"; //解説:行情報がnullであれば追加、データがあれば編集になります。
this.ClientSize = new Size(420, 240);
this.ControlBox = false; //システムボタン無効
this.ShowInTaskbar = false; //タスクバー上非表示
this.FormBorderStyle = FormBorderStyle.FixedDialog; //境界のスタイル
this.StartPosition = FormStartPosition.CenterParent; //親ダイアログ中央に配置
this.Load += Form_Load;
}
//OnLoad処理
private void Form_Load(object sender, EventArgs e)
{
BtnOK = new Button(); //解説:OKボタンです。
BtnOK.Location = new Point(ClientSize.Width - BtnOK.Width - 30, ClientSize.Height - BtnOK.Height - 10);
BtnOK.Text = "OK";
BtnOK.AutoSize = true;
BtnOK.DialogResult = DialogResult.OK;
BtnOK.Click += new EventHandler(OnOK_Click);
this.AcceptButton = BtnOK;
this.Controls.Add(BtnOK);
BtnCancel = new Button(); //解説:キャンセルボタンです。
BtnCancel.Location = new Point(30, ClientSize.Height - BtnCancel.Height - 10);
BtnCancel.Text = "Cancel";
BtnCancel.AutoSize = true;
BtnCancel.DialogResult = DialogResult.Cancel;
BtnCancel.Click += new EventHandler(OnCancel_Click);
this.CancelButton = BtnCancel;
this.Controls.Add(BtnCancel);
//解説:以下入力用のラベルとTextBoxの対になっています。
MsLabel = new Label();
MsLabel.Size = new Size(120, 20);
MsLabel.Location = new Point(10, 10);
MsLabel.BorderStyle = BorderStyle.Fixed3D;
MsLabel.TextAlign = ContentAlignment.MiddleCenter;
MsLabel.Text = "メンバーシップ";
this.Controls.Add(MsLabel);
MsBox = new TextBox();
MsBox.Size = new Size(270, 24);
MsBox.Location = new Point(140, 10);
MsBox.Text = ReferenceEquals(target, null) ? String.Empty : target.Cells[0].Value.ToString(); //解説:行情報がnullであれば空文字列、行情報があればCells[0]の値を文字列として表示します。以下同じ。
this.Controls.Add(MsBox);
IdLabel = new Label();
IdLabel.Size = new Size(120, 20);
IdLabel.Location = new Point(10, MsLabel.Height + 15);
IdLabel.BorderStyle = BorderStyle.Fixed3D;
IdLabel.TextAlign = ContentAlignment.MiddleCenter;
IdLabel.Text = "ログインID";
this.Controls.Add(IdLabel);
IdBox = new TextBox();
IdBox.Size = new Size(270, 24);
IdBox.Location = new Point(140, MsLabel.Height + 15);
IdBox.Text = ReferenceEquals(target, null) ? String.Empty : target.Cells[1].Value.ToString(); //解説:Ditto.
this.Controls.Add(IdBox);
PwLabel = new Label();
PwLabel.Size = new Size(120, 20);
PwLabel.Location = new Point(10, MsLabel.Height + IdLabel.Height + 20);
PwLabel.BorderStyle = BorderStyle.Fixed3D;
PwLabel.TextAlign = ContentAlignment.MiddleCenter;
PwLabel.Text = "パスワード";
this.Controls.Add(PwLabel);
PwBox = new TextBox();
PwBox.Size = new Size(270, 24);
PwBox.Location = new Point(140, MsLabel.Height + IdLabel.Height + 20);
PwBox.Text = ReferenceEquals(target, null) ? String.Empty : target.Cells[2].Value.ToString(); //解説:Ditto.
this.Controls.Add(PwBox);
RmLabel = new Label();
RmLabel.Size = new Size(120, 110);
RmLabel.Location = new Point(10, MsLabel.Height + IdLabel.Height + PwLabel.Height + 25);
RmLabel.BorderStyle = BorderStyle.Fixed3D;
RmLabel.TextAlign = ContentAlignment.MiddleCenter;
RmLabel.Text = "備考";
this.Controls.Add(RmLabel);
RmBox = new TextBox();
RmBox.Size = new Size(270, 114);
RmBox.Location = new Point(140, MsLabel.Height + IdLabel.Height + PwLabel.Height + 25);
RmBox.Multiline = true;
RmBox.AcceptsReturn = true;
RmBox.Text = ReferenceEquals(target, null) ? String.Empty : target.Cells[3].Value.ToString(); //解説:Ditto.
this.Controls.Add(RmBox);
}
//OKボタン処理
protected void OnOK_Click(object sender, EventArgs e)
{
if(ReferenceEquals(target, null)) //追加処理
{ //解説:戻り値用のiditemクラスインスタンスに4つのTextBoxの文字列の値を代入します。
iditem = new idItem() {Membership = MsBox.Text, ID = IdBox.Text, Password = PwBox.Text, Remark = RmBox.Text};
}
else //編集処理
{ //解説:引数の行情報からそのCellsの値に、4つのTextBoxの文字列の値を代入して編集します。
target.Cells[0].Value = MsBox.Text;
target.Cells[1].Value = IdBox.Text;
target.Cells[2].Value = PwBox.Text;
target.Cells[3].Value = RmBox.Text;
}
Close();
}
//Cancelボタン処理
protected void OnCancel_Click(object sender, EventArgs e)
{
Close();
}
}
/////////////////
//検索ダイアログ
/////////////////
class FindDlg : Form
{
//対象となるDataGridView
DataGridView target;
//ダイアログのコントロール
Button BtnOK;
Label label;
TextBox tBox;
public FindDlg(DataGridView dgv)
{
//ダイアログの属性設定
this.Text = "検索";
this.ClientSize = new Size(160, 100);
this.ControlBox = false; //システムボタン無効
this.ShowInTaskbar = false; //タスクバー上非表示
this.FormBorderStyle = FormBorderStyle.FixedDialog; //境界のスタイル
this.StartPosition = FormStartPosition.CenterParent; //親ダイアログ中央に配置
this.Load += Form_Load;
target = dgv; //DataGridViewを記録(解説:呼び出したidList(DataGridView)の行を選択する必要があるので、引数で取得します。)
}
//OnLoad処理
private void Form_Load(object sender, EventArgs e)
{
//コントロールの属性設定
tBox = new TextBox();
tBox.Size = new Size(140, 24);
tBox.Location = new Point((ClientSize.Width - tBox.Width) / 2, 30);
this.Controls.Add(tBox);
BtnOK = new Button();
BtnOK.Location = new Point((ClientSize.Width - BtnOK.Width) / 2, ClientSize.Height - BtnOK.Height - 10);
BtnOK.Text = "OK";
BtnOK.AutoSize = true;
BtnOK.DialogResult = DialogResult.OK;
BtnOK.Click += new EventHandler(OnOK_Click);
this.Controls.Add(BtnOK);
label = new Label();
label.Size = new Size(140, 20);
label.Location = new Point((ClientSize.Width - label.Width) / 2, 4);
label.Text = "検索する文字列";
this.Controls.Add(label);
this.AcceptButton = this.BtnOK;
this.KeyPreview = true;
}
//OKボタン処理
protected void OnOK_Click(object sender, EventArgs e)
{
if(tBox.Text == String.Empty) //解説:エラーメッセージは出しますが、終了することにしました。
MessageBox.Show("検索文字列が入力されていません。", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
{
//選択を一旦クリア
target.ClearSelection(); //解説:引数として取得したDataGridViewの現選択を一旦解除します。
bool found = false;
foreach(DataGridViewRow row in target.Rows) //解説:行配列を進めます。
{
foreach(DataGridViewCell cell in row.Cells) //解説:行のCells配列を進めます。
{
if(cell.Value != null && cell.Value.ToString().Contains(tBox.Text)) //解説:特定行の特定セルに検索文字列があれば
{
//セルを選択状態にする(解説:一行モードにしているので、実際にはセルを選択すると行が選択されます。)
cell.Selected = true;
//最初に見つかったセルにフォーカスを合わせる
if(!found)
{
target.CurrentCell = cell;
found = true;
}
}
}
}
Close();
}
}
}
///////////////////
//ソートダイアログ
///////////////////
class SortDlg : Form
{
//ダイアログのフィールド
public int ColIndex; //解説:ソートの対象となる列番号です。
public bool IsAscending; //解説:昇順、降順のフラグです。
//ダイアログのコントロール
Button BtnOK;
ComboBox ColList; //DropDownStyleプロパティをDropDownList
ComboBox ADList; //Ascending|Decending(昇順、降順指定)
public SortDlg()
{
//ダイアログの属性設定
this.Text = "データの並び替え";
this.ClientSize = new Size(160, 100);
this.ControlBox = false; //システムボタン無効
this.ShowInTaskbar = false; //タスクバー上非表示
this.FormBorderStyle = FormBorderStyle.FixedDialog; //境界のスタイル
this.StartPosition = FormStartPosition.CenterParent; //親ダイアログ中央に配置
this.Load += Form_Load;
}
//OnLoad処理
private void Form_Load(object sender, EventArgs e)
{
//コントロールの属性設定
BtnOK = new Button();
BtnOK.Location = new Point((ClientSize.Width - BtnOK.Width) / 2, ClientSize.Height - BtnOK.Height - 10);
BtnOK.Text = "完了";
BtnOK.AutoSize = true;
BtnOK.Click += new EventHandler(OnOK_Click);
this.Controls.Add(BtnOK);
ColList = new ComboBox();
ColList.DropDownStyle = ComboBoxStyle.DropDownList;
ColList.Location = new Point((ClientSize.Width - ColList.Width) / 2, 10);
//解説:列数4で"メンバーシップ"、"ログインID"、"パスワード"、"備考"に合わせてitemsに登録します。
ColList.MaxDropDownItems = 4;
ColList.Items.Add("メンバーシップ");
ColList.Items.Add("ログインID");
ColList.Items.Add("パスワード");
ColList.Items.Add("備考");
ColList.SelectedIndex = 0;
this.Controls.Add(ColList);
ADList = new ComboBox();
ADList.DropDownStyle = ComboBoxStyle.DropDownList;
ADList.Location = new Point((ClientSize.Width - ADList.Width) / 2, 40);
//解説:列数2で"昇順"、"降順"を登録します。
ADList.MaxDropDownItems = 2;
ADList.Items.Add("昇順");
ADList.Items.Add("降順");
ADList.SelectedIndex = 0;
this.Controls.Add(ADList);
}
//OKボタン処理
protected void OnOK_Click(object sender, EventArgs e)
{
if(ColList.SelectedIndex == -1)
MessageBox.Show("並び替えが選択されていません。", "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
else
{
//解説:戻り値を設定します。
ColIndex = ColList.SelectedIndex;
IsAscending = (ADList.SelectedIndex == 0);
Close();
}
}
}
}
このidListコントロールは以下のウィンドウイメージのクライアントエリア一杯に貼っている表となります。
最終回はBCCForm and BCCSkeltonパッケージのファイルのコンパイルの方法と作成および動作試験の講評で締めましょうか。
