Find control by name from Windows Forms controls
up vote
33
down vote
favorite
I have a list of my textbox names, and I want to find a control by name. How is it possible?
c# .net winforms
add a comment |
up vote
33
down vote
favorite
I have a list of my textbox names, and I want to find a control by name. How is it possible?
c# .net winforms
add a comment |
up vote
33
down vote
favorite
up vote
33
down vote
favorite
I have a list of my textbox names, and I want to find a control by name. How is it possible?
c# .net winforms
I have a list of my textbox names, and I want to find a control by name. How is it possible?
c# .net winforms
c# .net winforms
edited Dec 9 '15 at 9:17
Shadow Wizard
56.9k18107173
56.9k18107173
asked Oct 10 '10 at 1:21
krunal shah
10.4k2180128
10.4k2180128
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
84
down vote
accepted
Use Control.ControlCollection.Find.
TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
tbx.Text = "found!";
EDIT for asker:
Control tbxs = this.Controls.Find(txtbox_and_message[0,0], true);
if (tbxs != null && tbxs.Length > 0)
{
tbxs[0].Text = "Found!";
}
TextBox tBox = this.Controls.Find(txtbox_and_message[0, 0], true).FirstOrDefault() as TextBox; Is it OK?
– krunal shah
Oct 10 '10 at 1:37
Getting this error.. .net framework 2.0.. 'System.Array' does not contain a definition for 'FirstOrDefault' and no extension method 'FirstOrDefault' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
– krunal shah
Oct 10 '10 at 1:43
Are you dynamically adding textbox into your form during runtime? If this is the case, you can assign a unique name to each textbox, and use controls.find to find the textbox with its unique name.
– bla
Oct 10 '10 at 1:45
No i am not adding dynamically.. I have already list of textboxes .. And i have stored names of all taxboxes in one array. From that array i am fetching names and than finding textboxes and than want to fetch text from the specific textbox ..
– krunal shah
Oct 10 '10 at 1:49
3
TextBox foundTbx = this.Controls.Find(textBoxes[5], true)[0] as TextBox;
– bla
Oct 10 '10 at 1:56
|
show 2 more comments
up vote
9
down vote
You can use:
f.Controls[name];
Where f
is your form variable. That gives you the control with name name
.
5
Note that this doesn't work if the control is nested (you'll only find controls present at that level in the control hierarchy).
– Michael Petrotta
Oct 10 '10 at 1:30
@Michael: That is correct.
– CesarGon
Oct 10 '10 at 1:32
add a comment |
up vote
3
down vote
TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
tbx.Text = "found!";
If Controls.Find is not found "textBox1" => error. You must add code.
If(tbx != null)
Edit:
TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
If(tbx != null)
tbx.Text = "found!";
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
84
down vote
accepted
Use Control.ControlCollection.Find.
TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
tbx.Text = "found!";
EDIT for asker:
Control tbxs = this.Controls.Find(txtbox_and_message[0,0], true);
if (tbxs != null && tbxs.Length > 0)
{
tbxs[0].Text = "Found!";
}
TextBox tBox = this.Controls.Find(txtbox_and_message[0, 0], true).FirstOrDefault() as TextBox; Is it OK?
– krunal shah
Oct 10 '10 at 1:37
Getting this error.. .net framework 2.0.. 'System.Array' does not contain a definition for 'FirstOrDefault' and no extension method 'FirstOrDefault' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
– krunal shah
Oct 10 '10 at 1:43
Are you dynamically adding textbox into your form during runtime? If this is the case, you can assign a unique name to each textbox, and use controls.find to find the textbox with its unique name.
– bla
Oct 10 '10 at 1:45
No i am not adding dynamically.. I have already list of textboxes .. And i have stored names of all taxboxes in one array. From that array i am fetching names and than finding textboxes and than want to fetch text from the specific textbox ..
– krunal shah
Oct 10 '10 at 1:49
3
TextBox foundTbx = this.Controls.Find(textBoxes[5], true)[0] as TextBox;
– bla
Oct 10 '10 at 1:56
|
show 2 more comments
up vote
84
down vote
accepted
Use Control.ControlCollection.Find.
TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
tbx.Text = "found!";
EDIT for asker:
Control tbxs = this.Controls.Find(txtbox_and_message[0,0], true);
if (tbxs != null && tbxs.Length > 0)
{
tbxs[0].Text = "Found!";
}
TextBox tBox = this.Controls.Find(txtbox_and_message[0, 0], true).FirstOrDefault() as TextBox; Is it OK?
– krunal shah
Oct 10 '10 at 1:37
Getting this error.. .net framework 2.0.. 'System.Array' does not contain a definition for 'FirstOrDefault' and no extension method 'FirstOrDefault' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
– krunal shah
Oct 10 '10 at 1:43
Are you dynamically adding textbox into your form during runtime? If this is the case, you can assign a unique name to each textbox, and use controls.find to find the textbox with its unique name.
– bla
Oct 10 '10 at 1:45
No i am not adding dynamically.. I have already list of textboxes .. And i have stored names of all taxboxes in one array. From that array i am fetching names and than finding textboxes and than want to fetch text from the specific textbox ..
– krunal shah
Oct 10 '10 at 1:49
3
TextBox foundTbx = this.Controls.Find(textBoxes[5], true)[0] as TextBox;
– bla
Oct 10 '10 at 1:56
|
show 2 more comments
up vote
84
down vote
accepted
up vote
84
down vote
accepted
Use Control.ControlCollection.Find.
TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
tbx.Text = "found!";
EDIT for asker:
Control tbxs = this.Controls.Find(txtbox_and_message[0,0], true);
if (tbxs != null && tbxs.Length > 0)
{
tbxs[0].Text = "Found!";
}
Use Control.ControlCollection.Find.
TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
tbx.Text = "found!";
EDIT for asker:
Control tbxs = this.Controls.Find(txtbox_and_message[0,0], true);
if (tbxs != null && tbxs.Length > 0)
{
tbxs[0].Text = "Found!";
}
edited Nov 5 '13 at 17:52
Carsten
9,05353154
9,05353154
answered Oct 10 '10 at 1:27
bla
4,21711924
4,21711924
TextBox tBox = this.Controls.Find(txtbox_and_message[0, 0], true).FirstOrDefault() as TextBox; Is it OK?
– krunal shah
Oct 10 '10 at 1:37
Getting this error.. .net framework 2.0.. 'System.Array' does not contain a definition for 'FirstOrDefault' and no extension method 'FirstOrDefault' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
– krunal shah
Oct 10 '10 at 1:43
Are you dynamically adding textbox into your form during runtime? If this is the case, you can assign a unique name to each textbox, and use controls.find to find the textbox with its unique name.
– bla
Oct 10 '10 at 1:45
No i am not adding dynamically.. I have already list of textboxes .. And i have stored names of all taxboxes in one array. From that array i am fetching names and than finding textboxes and than want to fetch text from the specific textbox ..
– krunal shah
Oct 10 '10 at 1:49
3
TextBox foundTbx = this.Controls.Find(textBoxes[5], true)[0] as TextBox;
– bla
Oct 10 '10 at 1:56
|
show 2 more comments
TextBox tBox = this.Controls.Find(txtbox_and_message[0, 0], true).FirstOrDefault() as TextBox; Is it OK?
– krunal shah
Oct 10 '10 at 1:37
Getting this error.. .net framework 2.0.. 'System.Array' does not contain a definition for 'FirstOrDefault' and no extension method 'FirstOrDefault' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
– krunal shah
Oct 10 '10 at 1:43
Are you dynamically adding textbox into your form during runtime? If this is the case, you can assign a unique name to each textbox, and use controls.find to find the textbox with its unique name.
– bla
Oct 10 '10 at 1:45
No i am not adding dynamically.. I have already list of textboxes .. And i have stored names of all taxboxes in one array. From that array i am fetching names and than finding textboxes and than want to fetch text from the specific textbox ..
– krunal shah
Oct 10 '10 at 1:49
3
TextBox foundTbx = this.Controls.Find(textBoxes[5], true)[0] as TextBox;
– bla
Oct 10 '10 at 1:56
TextBox tBox = this.Controls.Find(txtbox_and_message[0, 0], true).FirstOrDefault() as TextBox; Is it OK?
– krunal shah
Oct 10 '10 at 1:37
TextBox tBox = this.Controls.Find(txtbox_and_message[0, 0], true).FirstOrDefault() as TextBox; Is it OK?
– krunal shah
Oct 10 '10 at 1:37
Getting this error.. .net framework 2.0.. 'System.Array' does not contain a definition for 'FirstOrDefault' and no extension method 'FirstOrDefault' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
– krunal shah
Oct 10 '10 at 1:43
Getting this error.. .net framework 2.0.. 'System.Array' does not contain a definition for 'FirstOrDefault' and no extension method 'FirstOrDefault' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
– krunal shah
Oct 10 '10 at 1:43
Are you dynamically adding textbox into your form during runtime? If this is the case, you can assign a unique name to each textbox, and use controls.find to find the textbox with its unique name.
– bla
Oct 10 '10 at 1:45
Are you dynamically adding textbox into your form during runtime? If this is the case, you can assign a unique name to each textbox, and use controls.find to find the textbox with its unique name.
– bla
Oct 10 '10 at 1:45
No i am not adding dynamically.. I have already list of textboxes .. And i have stored names of all taxboxes in one array. From that array i am fetching names and than finding textboxes and than want to fetch text from the specific textbox ..
– krunal shah
Oct 10 '10 at 1:49
No i am not adding dynamically.. I have already list of textboxes .. And i have stored names of all taxboxes in one array. From that array i am fetching names and than finding textboxes and than want to fetch text from the specific textbox ..
– krunal shah
Oct 10 '10 at 1:49
3
3
TextBox foundTbx = this.Controls.Find(textBoxes[5], true)[0] as TextBox;
– bla
Oct 10 '10 at 1:56
TextBox foundTbx = this.Controls.Find(textBoxes[5], true)[0] as TextBox;
– bla
Oct 10 '10 at 1:56
|
show 2 more comments
up vote
9
down vote
You can use:
f.Controls[name];
Where f
is your form variable. That gives you the control with name name
.
5
Note that this doesn't work if the control is nested (you'll only find controls present at that level in the control hierarchy).
– Michael Petrotta
Oct 10 '10 at 1:30
@Michael: That is correct.
– CesarGon
Oct 10 '10 at 1:32
add a comment |
up vote
9
down vote
You can use:
f.Controls[name];
Where f
is your form variable. That gives you the control with name name
.
5
Note that this doesn't work if the control is nested (you'll only find controls present at that level in the control hierarchy).
– Michael Petrotta
Oct 10 '10 at 1:30
@Michael: That is correct.
– CesarGon
Oct 10 '10 at 1:32
add a comment |
up vote
9
down vote
up vote
9
down vote
You can use:
f.Controls[name];
Where f
is your form variable. That gives you the control with name name
.
You can use:
f.Controls[name];
Where f
is your form variable. That gives you the control with name name
.
answered Oct 10 '10 at 1:26
CesarGon
12k54678
12k54678
5
Note that this doesn't work if the control is nested (you'll only find controls present at that level in the control hierarchy).
– Michael Petrotta
Oct 10 '10 at 1:30
@Michael: That is correct.
– CesarGon
Oct 10 '10 at 1:32
add a comment |
5
Note that this doesn't work if the control is nested (you'll only find controls present at that level in the control hierarchy).
– Michael Petrotta
Oct 10 '10 at 1:30
@Michael: That is correct.
– CesarGon
Oct 10 '10 at 1:32
5
5
Note that this doesn't work if the control is nested (you'll only find controls present at that level in the control hierarchy).
– Michael Petrotta
Oct 10 '10 at 1:30
Note that this doesn't work if the control is nested (you'll only find controls present at that level in the control hierarchy).
– Michael Petrotta
Oct 10 '10 at 1:30
@Michael: That is correct.
– CesarGon
Oct 10 '10 at 1:32
@Michael: That is correct.
– CesarGon
Oct 10 '10 at 1:32
add a comment |
up vote
3
down vote
TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
tbx.Text = "found!";
If Controls.Find is not found "textBox1" => error. You must add code.
If(tbx != null)
Edit:
TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
If(tbx != null)
tbx.Text = "found!";
add a comment |
up vote
3
down vote
TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
tbx.Text = "found!";
If Controls.Find is not found "textBox1" => error. You must add code.
If(tbx != null)
Edit:
TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
If(tbx != null)
tbx.Text = "found!";
add a comment |
up vote
3
down vote
up vote
3
down vote
TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
tbx.Text = "found!";
If Controls.Find is not found "textBox1" => error. You must add code.
If(tbx != null)
Edit:
TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
If(tbx != null)
tbx.Text = "found!";
TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
tbx.Text = "found!";
If Controls.Find is not found "textBox1" => error. You must add code.
If(tbx != null)
Edit:
TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
If(tbx != null)
tbx.Text = "found!";
answered Mar 30 '15 at 4:04
Nguyen Ngoc Quyen
391
391
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3898588%2ffind-control-by-name-from-windows-forms-controls%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown