I’m struggling to get the right context with how to get a TextBox currently in my form.
Right now I have a button that when pressed will allow the user to choose a folder. I’d like to take that path and put it in a TextBox which is currently named installPath.
namespace CustomLauncher
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void browse_Click(object sender, EventArgs e)
{
//browse to select a folder
FolderBrowserDialog folder = new FolderBrowserDialog();
DialogResult result = folder.ShowDialog();
if (result == DialogResult.OK)
{
MessageBox.Show("You chose" + folder.SelectedPath);
}
else if (result == DialogResult.Cancel)
{
return;
}
}
I’ve seen various attempts like…
this.Controls.Find("installPath"); //visual studio didn’t like this
Control myControl1 = FindControl("installPath"); //didn’t like this either
I’ve also seen a few other ways of doing this. Though I can’t seem to find one that visual studio will accept. I feel like I’m missing something rather obvious/huge about the context of this event listener which is why I’m not able to figure out how to accomplish this.
Why not
this.installPath.Text = folder.SelectedPath? Is theTextBoxon another form?Btw, you’ve seen the
FindControlapproaches on ASP.NET sites.If you’re using .NET 2 or greater you can use the
Control.ControlCollection.FindMethod.