Your button has a Tag
Property that can take an Object, try putting your associated TextBox's name in it, then use the Controls.Find
Method to locate the TextBox that has that name. Something like this.
Your Modified NextDialog Method:
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++;
}
Your Event Handler:
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.
}
}