Good day, dear. On the form are TextBox and Button. Elements created by software. When you click on Button, OpenDialog is called. I am trying to transfer the value of OpenDialog to a TextBox, but I seem to be confused by the visibility of form components. If you specify the Click button in this procedure or the form is not available. Please tell me how to pass the value of the OpenDialog selection result to the TextBox on the form.

public void databaseoptionItem_Click(object sender, EventArgs e) { Form optionForm = new Form(); TextBox databasePathTextBox = new TextBox(); databasePathTextBox.Parent = optionForm; optionForm.Controls.Add(databasePathTextBox); Button databasePathButton = new Button(); databasePathButton.Parent = optionForm; databasePathButton.Click += new EventHandler(databasePathButton_Click); optionForm.Show(); } public void databasePathButton_Click(object sender, EventArgs e) { OpenFileDialog databaseOpenFileDialog = new OpenFileDialog(); if (databaseOpenFileDialog.ShowDialog() == DialogResult.OK) { optionForm. ?(не вижу ни формы ни объектов) } } 
  • 2
    make optionForm not a local method variable, but a private field of a form class - Bulson
  • Thank you very much, Bulson. Indeed, when creating a private field of a class and then assigning the created element to it, it appears within the form. Thank you for your help. - alessi-savage

1 answer 1

Brought the TextBox field to the private field of the class (made visible for the entire class).

 private TextBox _databasePathTextBox; public void databaseoptionItem_Click(object sender, EventArgs e) { Form optionForm = new Form(); _databasePathTextBox = new TextBox(); databasePathTextBox.Parent = optionForm; optionForm.Controls.Add(databasePathTextBox); Button databasePathButton = new Button(); databasePathButton.Parent = optionForm; databasePathButton.Click += new EventHandler(databasePathButton_Click); optionForm.Show(); } public void databasePathButton_Click(object sender, EventArgs e) { OpenFileDialog databaseOpenFileDialog = new OpenFileDialog(); if (databaseOpenFileDialog.ShowDialog() == DialogResult.OK) { _databasePathTextBox.Text = optionForm.FileName; } } 
  • thanks, Murad, everything worked out. - alessi-savage