これは最近私にとって本当に苦労しています。ログインフォームに2つのテキストボックスを動的に作成し、ログインボタンを押すとテキストボックスから値を取得したい。私は可能な値についてインターネット上で検索しましたが、どれも私のために働くようです。ボタンとテキストボックスは別のクラスで作られており、それは私にとって痛い部分です。フォーム上のテキストボックスから、ボタンが作成されたクラスのイベントハンドラに値を取得できません。誰かが私にこの手助けをすることができれば、私は自分のプロジェクトに進み、すぐにそれを終えることができます。WinFormアプリケーション動的テキストボックスから値を取得
コードの一部も残します。これらの部分は、ボタンやテキストボックス、メインフォームのクラスになります。
メインフォーム
x_loc = 0;
y_loc = 80;
x_size = 100;
y_size = 20;
foreach(string item in lon.loginLbls())
{
clsLbl = new Labels(pnlMenu, x_loc, y_loc, x_size, y_size, item);
y_loc += 50;
}
x_loc = 0;
y_loc = 100;
x_size = 200;
y_size = 30;
foreach(string item in lon.loginForm())
{
clsTxt = new Textboxes(pnlMenu, x_loc, y_loc, x_size, y_size, item);
y_loc += 50;
}
x_loc = 0;
y_loc = 200;
x_size = 200;
y_size = 30;
foreach(string item in lon.loginBtns())
{
clsBtn = new Buttons(pnlMenu, x_loc, y_loc, x_size, y_size, item);
y_loc += 50;
}
Buttonクラス
Button btn;
public Buttons(Panel parent, int x_loc, int y_loc, int x_size, int y_size, string name)
{
btn = new Button();
btn.Location = new Point(x_loc, y_loc);
btn.Size = new Size(x_size, y_size);
btn.Text = name;
btn.Name = name;
btn.Click += new EventHandler(btnHandler);
btn.ForeColor = Color.Black;
btn.BackColor = Color.White;
parent.Controls.Add(btn);
}
private void btnHandler(object sender, EventArgs e)
{
Button btn = sender as Button;
switch (btn.Text)
{
case "Login":
Login lgn = new Login();
break;
case "Afsluiten":
Application.Exit();
break;
}
}
テキストボックスクラス
のTextBox tb;
public Textboxes(Panel parent, int x_loc, int y_loc, int x_size, int y_size, string name)
{
tb = new TextBox();
tb.Location = new Point(x_loc, y_loc);
tb.Size = new Size(x_size, y_size);
tb.Name = name;
tb.ForeColor = Color.Black;
tb.BackColor = Color.White;
parent.Controls.Add(tb);
}
リスト
public List<string> loginBtns()
{
List<string> lgnBtns = new List<string>();
lgnBtns.Add("Login");
return lgnBtns;
}
public List<string> loginForm()
{
List<string> lgnForm = new List<string>();
lgnForm.Add("username");
lgnForm.Add("password");
return lgnForm;
}
public List<string> loginLbls()
{
List<string> lgnLbl = new List<string>();
lgnLbl.Add("Code");
lgnLbl.Add("Wachtwoord");
return lgnLbl;
}
注:これらのすべてのメソッドは別のクラスに位置しています。
WinFormsライブラリのクラスであるかのように振る舞うクラスを作成しないでください。 –
少し詳しく説明してください。 – remboro