0

I have some controls in my form inside a panel. I want to get value of any control if I know the name of that control which is Inside that Panel. So I did following effort....

foreach (Control ch in pnl.Controls)
{
     if (ch.Name == "ControlName")
          MessageBox.Show(ch.Text);
}

But this code loop through all the controls inside that panel, that's not my exact requirement. I want to find that control without any loop.

May be my question looks like duplicate of this post, but that post is helpful when you already know that your desired control is TextBox. What if I don't know the type of my control ?

Community
  • 1
  • 1
Shaiwal Tripathi
  • 601
  • 1
  • 12
  • 32
  • @Reza Aghaei, That post is helpful when you already know that your control is TextBox. What if I don't know what is the type of my control ? – Shaiwal Tripathi Dec 02 '16 at 05:23
  • The type of control doesn't matter to find it. Use `var c = this.Controls["someControl"];` like [this answer](http://stackoverflow.com/a/3898599/3110834). Also in the accepted answer you can neglect the casing and just use `var c = this.Controls.Find("someControl", true).FirstOrDefault();` – Reza Aghaei Dec 02 '16 at 05:29
  • and how will I retrieve the value of var c ? – Shaiwal Tripathi Dec 02 '16 at 05:34
  • All controls have `Text` property like you used in your `foreach` example. – Reza Aghaei Dec 02 '16 at 05:35
  • One more thing..... Can you tell me where this line var c = this.Controls["someControl"]; in that post on the basis of which you marked my question duplicate ? – Shaiwal Tripathi Dec 02 '16 at 05:41
  • In my first comment I showed [this answer](http://stackoverflow.com/a/3898599/3110834) of duplicate post. While the accepted answer is completely OK and answers youre question, but you should read other posts as well. Also don't bother by mark as duplicates. I just tried to help and prevent new answers to an obvious duplicate, that's why I'm answering your comments while I believe the post obviously answers your question :) – Reza Aghaei Dec 02 '16 at 05:46
  • Actually I don't know how to retrieve value from `var c`. So I thought my question is different from that answer. – Shaiwal Tripathi Dec 02 '16 at 05:50
  • You learned to find the control. Your next question is to find its value. The `Value` is not a common property between controls. For example a `TextBox` has `Text`, a `CheckBox` has `Checked`, a `ComboBox` has `SelectedIndex`, `Text`, `SelectedItem` and so on. You can not have a generic way to get what you called value, unless you know you are working with what kind of control. – Reza Aghaei Dec 02 '16 at 05:55
  • I meant `value` in general term. I completely understand `TextBox` has `Text` property, `Checkbox` has `Checked` property and so on. But how will I know the type of `var c`. Will I have to cast `c` to `control` ? If Yes, then how ? – Shaiwal Tripathi Dec 02 '16 at 06:01
  • Please ask a new question. This question is specific to finding a control with name and you said you don't want to use a loop to find it. The answer to this question is given. Just ask about a generic way to get what you called `Value`. The only solution is checking if the found control is `TextBox` then do something and if is `CheckBox` then do something else and so on. – Reza Aghaei Dec 02 '16 at 06:07
  • Also when you don't know what the type of control is, it's strange that you want to find it. While your question says a different thing which you asked in comments. – Reza Aghaei Dec 02 '16 at 06:08
  • I want to insert values of my controls which is inside the panel to Database. So I search all the controls according to its name (database field name is the same as control's name) and if that control found i want to retrieve it's value (value in general term not c#) according to it's type. So what is strange here huh ? – Shaiwal Tripathi Dec 02 '16 at 06:20
  • I believe data-binding is what you are looking for. Never mind. Each question is dedicated to a single topic, finding the control is closed: *But this code loop through all the controls inside that panel, that's not my exact requirement. I want to find that control without any loop.*. Please ask a new question about your new requirement. I also will share answer if I has anything to share. Good luck :) – Reza Aghaei Dec 02 '16 at 06:24

0 Answers0