[英]How can i use informations after create textBox in C#
我創建了手動添加textBox和Button的函數。
這是我的實際腳本:
private void OpenFileDialogButton_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string souborFilename = openFileDialog1.FileName;
filePathText.Text = souborFilename;
}
}
private void nextDialog_Click(object sender, EventArgs e)
{
if (calculate <= 7)
{
TextBox text = new TextBox();
text.Location = new Point(filePathText.Location.X, filePathText.Location.Y + y);
text.Size = new Size(194, 20);
text.ReadOnly = true;
text.Name = "filePathText" + "{calculate}";
//MessageBox.Show(text.Name);
this.Controls.Add(text);
Button button = new Button();
button.Location = new Point(OpenFileDialogButton.Location.X, OpenFileDialogButton.Location.Y + y);
button.Size = new Size(33, 24);
button.Text = "...";
button.Click += new EventHandler(OpenFileDialogButton_Click);
this.Controls.Add(button);
this.nextDialog.Location = new Point(22, 49 + y);
}
else
{
this.nextDialog.Controls.Remove(nextDialog);
this.nextDialog.Dispose();
MessageBox.Show("Maximální možnost počtů přidaných souborů byla dosažena!");
}
y = y + 28;
calculate++;
}
當用戶單擊nextDialog按鈕以使其創建正確的下一個按鈕和textboxt但所有按鈕具有相同的功能。 每個按鈕都有自己的textBox。
問題是在使用openFileDialog之后,每個按鈕仍然會改變相同的textBox。 我需要每個按鈕只更改自己的textBox。
所以我需要幫助功能“OpenFileDialogButton_Click” 。
正是這部分:
filePathText.Text =這是我默認的TextBox名稱,比我開始使用函數手動添加文本框和按鈕。 有必要使它充滿活力。
filePathText.Text = souborFilename;
這是我的問題的圖片:
http://i.imgur.com/z698zAz.jpg http://i.imgur.com/PGJgvjl.jpg
最簡單的方法是使用TextBox和Button創建自定義控件,並在nextDialog Click上添加該控件。
您的按鈕具有可以獲取Object的Tag
屬性,嘗試將相關的TextBox名稱放入其中,然后使用Controls.Find
方法找到具有該名稱的TextBox。 像這樣的東西。
您修改的NextDialog方法:
private void nextDialog_Click(object sender, EventArgs e)
{
if (calculate <= 7)
{
TextBox text = new TextBox();
text.Location = new Point(filePathText.Location.X, filePathText.Location.Y + y);
text.Size = new Size(194, 20);
text.ReadOnly = true;
text.Name = "filePathText" + "{calculate}";
//MessageBox.Show(text.Name);
this.Controls.Add(text);
Button button = new Button();
button.Location = new Point(OpenFileDialogButton.Location.X, OpenFileDialogButton.Location.Y + y);
button.Size = new Size(33, 24);
button.Text = "...";
button.Tag = text.Name; //Name of associated TextBox added to Tag Property
button.Click += new EventHandler(OpenFileDialogButton_Click);
this.Controls.Add(button);
this.nextDialog.Location = new Point(22, 49 + y);
}
else
{
this.nextDialog.Controls.Remove(nextDialog);
this.nextDialog.Dispose();
MessageBox.Show("Maximální možnost počtů přidaných souborů byla dosažena!");
}
y = y + 28;
calculate++;
}
你的事件處理程序:
private void OpenFileDialogButton_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Button btn = sender as Button; //Get the Button that was clicked
string souborFilename = openFileDialog1.FileName;
this.Controls.Find((string)btn.Tag), true )[0].Text = souborFilename; //Find textbox that matches stored name
//since method returns an array you will
//have to access it threw an index.
}
}
我建議你給你正在創建的新按鈕命名,並根據“OpenFileDialogButton_Click”中的發件人名稱指定文本框。
button.Name = "btn";
private void OpenFileDialogButton_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string souborFilename = openFileDialog1.FileName;
string s = ((Button)sender).Name;
if (s == "btn")
{
Newtextbox.Text = "";
}
else
filePathText.Text = souborFilename;
}
}
你可以讓你的功能綁定像 -
button.Click += OpenFileDialogButton_Click(text);
你的功能可以像 -
private EventHandler OpenFileDialogButton_Click(TextBox txt){ //Your code }
它將方便您每次傳遞新創建的文本框對象。
嘗試更改動態OpenFileDialogButton按鈕的事件處理程序,如下所示:
private void nextDialog_Click(object sender, EventArgs e)
{
...
TextBox text = new TextBox();
text.Location = new Point(filePathText.Location.X, filePathText.Location.Y + y);
text.Size = new Size(194, 20);
text.ReadOnly = true;
text.Name = "filePathText" + "{calculate}";
//MessageBox.Show(text.Name);
this.Controls.Add(text);
Button button = new Button();
button.Location = new Point(OpenFileDialogButton.Location.X, OpenFileDialogButton.Location.Y + y);
button.Size = new Size(33, 24);
button.Text = "...";
// -- start changes
button.Click += (o, args) => {
using (var opf = new OpenFileDialog()) {
if (opf.ShowDialog() == DialogResult.OK) {
var souborFilename = opf.FileName;
text.Text = souborFilename;
}
}
};
// -- end changes
this.Controls.Add(button);
this.nextDialog.Location = new Point(22, 49 + y);
...
}
private void OpenFileDialogButton_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string souborFilename = openFileDialog1.FileName;
foreach (var control in this.Controls)
{
if (control is TextBox)
{
TextBox tb = control as TextBox;
Button b = sender as Button;
if(b != null && tb.Name.Equals("filePathText" + b.Name.Substring(b.Name.Count()-1,1)))
{
tb.Text = souborFilename;
}
}
}
filePathText.Text = souborFilename;
}
}
private void nextDialog_Click(object sender, EventArgs e)
{
if (calculate <= 7)
{
TextBox text = new TextBox();
text.Location = new Point(filePathText.Location.X, filePathText.Location.Y + y);
text.Size = new Size(194, 20);
text.ReadOnly = true;
text.Name = "filePathText" + calculate.ToString();
//MessageBox.Show(text.Name);
this.Controls.Add(text);
Button button = new Button();
button.Location = new Point(OpenFileDialogButton.Location.X, OpenFileDialogButton.Location.Y + y);
button.Size = new Size(33, 24);
button.Text = "...";
button.Click += new EventHandler(OpenFileDialogButton_Click);
button.Name = "filePathButton" + calculate.ToString();
this.Controls.Add(button);
this.nextDialog.Location = new Point(22, 49 + y);
}
else
{
this.nextDialog.Controls.Remove(nextDialog);
this.nextDialog.Dispose();
MessageBox.Show("Maximální možnost počtů přidaných souborů byla dosažena!");
}
y = y + 28;
calculate++;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.