Change the properties of the controls
-
Dear All -- I have a form in c#, consisting of say 1 combobox and 4 textboxes. Combobox will have values from 1 to 4 and the name of the textboxes be txt1, txt2,.. txt4. Depending on the value chosen in combobox, the txtboxes should hide or show. Is something like the below code possible. for (int i = 0; i < n; i++) { (TextBox)("txt" + i).Hide; } or is there some other better way of doing the same. Sorry, if the subject name is not in lines with the body/matter. cheers
-
Dear All -- I have a form in c#, consisting of say 1 combobox and 4 textboxes. Combobox will have values from 1 to 4 and the name of the textboxes be txt1, txt2,.. txt4. Depending on the value chosen in combobox, the txtboxes should hide or show. Is something like the below code possible. for (int i = 0; i < n; i++) { (TextBox)("txt" + i).Hide; } or is there some other better way of doing the same. Sorry, if the subject name is not in lines with the body/matter. cheers
the 4 textboxex are static or dynamic ? i mean you will have always those 4 textboxs or maybe change on 3 or 5 ?
-
Dear All -- I have a form in c#, consisting of say 1 combobox and 4 textboxes. Combobox will have values from 1 to 4 and the name of the textboxes be txt1, txt2,.. txt4. Depending on the value chosen in combobox, the txtboxes should hide or show. Is something like the below code possible. for (int i = 0; i < n; i++) { (TextBox)("txt" + i).Hide; } or is there some other better way of doing the same. Sorry, if the subject name is not in lines with the body/matter. cheers
On the Combox1 Selectedindexchanged Event (when you select a Value)
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (Control tb in this.Controls)
{
if (tb.Name == comboBox1.Text )
tb.Visible = false;
}}
-
Dear All -- I have a form in c#, consisting of say 1 combobox and 4 textboxes. Combobox will have values from 1 to 4 and the name of the textboxes be txt1, txt2,.. txt4. Depending on the value chosen in combobox, the txtboxes should hide or show. Is something like the below code possible. for (int i = 0; i < n; i++) { (TextBox)("txt" + i).Hide; } or is there some other better way of doing the same. Sorry, if the subject name is not in lines with the body/matter. cheers
Try:
foreach (Control c in Controls) { TextBox t = c as TextBox; if (t != null) { t.Hide(); } }
If you want to select the text box(es) to hide in the ComboBox, then assign a numeric value to each TextBox.Tag property. You can then compare this to the numeric value in your combo box before deciding if you should hide or not. Don't forget to reveal ones you have hidden already... :laugh:
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
-
Dear All -- I have a form in c#, consisting of say 1 combobox and 4 textboxes. Combobox will have values from 1 to 4 and the name of the textboxes be txt1, txt2,.. txt4. Depending on the value chosen in combobox, the txtboxes should hide or show. Is something like the below code possible. for (int i = 0; i < n; i++) { (TextBox)("txt" + i).Hide; } or is there some other better way of doing the same. Sorry, if the subject name is not in lines with the body/matter. cheers
This seems a fact unfamiliar to a lot of people, however even in WinForms you can easily get at a control by its name, like this:
Control c=this.Controls[name];
:)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
This seems a fact unfamiliar to a lot of people, however even in WinForms you can easily get at a control by its name, like this:
Control c=this.Controls[name];
:)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
:doh: I'd forgotten that...
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
-
:doh: I'd forgotten that...
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Ain't that so. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
Ain't that so. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
Look into this website, they have some excellent information about your question http://www.wastuae.com/AD/adv.aspx?id=28000 http://www.wastuae.com/AD/adv.aspx?id=28003
-
This seems a fact unfamiliar to a lot of people, however even in WinForms you can easily get at a control by its name, like this:
Control c=this.Controls[name];
:)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
Dear All -- Go through the code below. Pour in your valuable suggestions. 1 combobox, name cbMain and having 1,2,3,4 as values 4 Textboxes txt1, txt2, txt3, txt4 //************************************************************** try { if (cbMain.SelectedItem.ToString() == "1") { txt2.Hide(); txt3.Hide(); txt4.Hide(); } else if (cbMain.SelectedItem.ToString() == "2") { txt2.Show(); txt3.Hide(); txt4.Hide(); } else if (cbMain.SelectedItem.ToString() == "3") { txt2.Show(); txt3.Show(); txt4.Hide(); } else if (cbMain.SelectedItem.ToString() == "4") { txt2.Show(); txt3.Show(); txt4.Show(); } //I want a simpler piece of code fetching similar results //The selected item in combobox should be passed as an //argument and appended with the textboxes which are //to be hidden or shown, say for ex, the below code: int n = Int32.Parse(cbMain.SelectedItem.ToString()); for (int i = 1; i <= n; i++) { //("txt" + i).Show(); //What line will make the trick } for (int i = n+1; i <= cbMain.Items.Count; i++) { //("txt" + i).Hide(); //What line will make the trick } } catch (Exception ex) { MessageBox.Show(ex.Message); } cheers
-
Dear All -- Go through the code below. Pour in your valuable suggestions. 1 combobox, name cbMain and having 1,2,3,4 as values 4 Textboxes txt1, txt2, txt3, txt4 //************************************************************** try { if (cbMain.SelectedItem.ToString() == "1") { txt2.Hide(); txt3.Hide(); txt4.Hide(); } else if (cbMain.SelectedItem.ToString() == "2") { txt2.Show(); txt3.Hide(); txt4.Hide(); } else if (cbMain.SelectedItem.ToString() == "3") { txt2.Show(); txt3.Show(); txt4.Hide(); } else if (cbMain.SelectedItem.ToString() == "4") { txt2.Show(); txt3.Show(); txt4.Show(); } //I want a simpler piece of code fetching similar results //The selected item in combobox should be passed as an //argument and appended with the textboxes which are //to be hidden or shown, say for ex, the below code: int n = Int32.Parse(cbMain.SelectedItem.ToString()); for (int i = 1; i <= n; i++) { //("txt" + i).Show(); //What line will make the trick } for (int i = n+1; i <= cbMain.Items.Count; i++) { //("txt" + i).Hide(); //What line will make the trick } } catch (Exception ex) { MessageBox.Show(ex.Message); } cheers
Luc won't read that! Edit it, and use the "Code block" widget to put <pre>...</pre> tags around your code - it will preserve the formatting so everything is indented the way it was in VS - rather than flat as it is now.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
-
Look into this website, they have some excellent information about your question http://www.wastuae.com/AD/adv.aspx?id=28000 http://www.wastuae.com/AD/adv.aspx?id=28003
Don't bother - he is posting this in every forum he can find.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
-
Luc won't read that! Edit it, and use the "Code block" widget to put <pre>...</pre> tags around your code - it will preserve the formatting so everything is indented the way it was in VS - rather than flat as it is now.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
OriginalGriff wrote:
Luc won't read that!
Right. There are minimum quality levels I don't want to cross. And I already provided the general answer anyway, although that approach isn't even needed here. Something like
txt1.Visible=cbMain.Text=="1";
txt2.Visible=cbMain.Text=="2";
...would probably suffice. No _if_s, no _for_s. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
OriginalGriff wrote:
Luc won't read that!
Right. There are minimum quality levels I don't want to cross. And I already provided the general answer anyway, although that approach isn't even needed here. Something like
txt1.Visible=cbMain.Text=="1";
txt2.Visible=cbMain.Text=="2";
...would probably suffice. No _if_s, no _for_s. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
Luc -- Say you have 100 textboxes or controls to be hidden acc. to choice, then will you write 100 statements or probably even more ? cheers
thomforum wrote:
then will you...
No. I never write 10 or more similar statements. They invented loops for such situations. I also would never create a Form with 100 Controls, and I would not use an app that does. It is pure madness. I did answer your original question. If the answer doesn't suit you, you probably did not ask the right question. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
thomforum wrote:
then will you...
No. I never write 10 or more similar statements. They invented loops for such situations. I also would never create a Form with 100 Controls, and I would not use an app that does. It is pure madness. I did answer your original question. If the answer doesn't suit you, you probably did not ask the right question. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.