Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Change the properties of the controls

Change the properties of the controls

Scheduled Pinned Locked Moved C#
csharp
15 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    thomforum
    wrote on last edited by
    #1

    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

    K OriginalGriffO L 4 Replies Last reply
    0
    • T thomforum

      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

      K Offline
      K Offline
      kadaoui el mehdi
      wrote on last edited by
      #2

      the 4 textboxex are static or dynamic ? i mean you will have always those 4 textboxs or maybe change on 3 or 5 ?

      1 Reply Last reply
      0
      • T thomforum

        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

        K Offline
        K Offline
        kadaoui el mehdi
        wrote on last edited by
        #3

        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;
        }

            }
        
        1 Reply Last reply
        0
        • T thomforum

          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

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          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.

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          1 Reply Last reply
          0
          • T thomforum

            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

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            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.

            OriginalGriffO T 2 Replies Last reply
            0
            • L Luc Pattyn

              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.

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              :doh: I'd forgotten that...

              Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              L 1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                :doh: I'd forgotten that...

                Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                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.

                L 1 Reply Last reply
                0
                • L Luc Pattyn

                  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.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  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

                  OriginalGriffO 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    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.

                    T Offline
                    T Offline
                    thomforum
                    wrote on last edited by
                    #9

                    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

                    OriginalGriffO 1 Reply Last reply
                    0
                    • T thomforum

                      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

                      OriginalGriffO Offline
                      OriginalGriffO Offline
                      OriginalGriff
                      wrote on last edited by
                      #10

                      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.

                      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                      L 1 Reply Last reply
                      0
                      • L Lost User

                        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

                        OriginalGriffO Offline
                        OriginalGriffO Offline
                        OriginalGriff
                        wrote on last edited by
                        #11

                        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.

                        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                        1 Reply Last reply
                        0
                        • OriginalGriffO OriginalGriff

                          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.

                          L Offline
                          L Offline
                          Luc Pattyn
                          wrote on last edited by
                          #12

                          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.

                          T 1 Reply Last reply
                          0
                          • L Luc Pattyn

                            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.

                            T Offline
                            T Offline
                            thomforum
                            wrote on last edited by
                            #13

                            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

                            L 1 Reply Last reply
                            0
                            • T thomforum

                              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

                              L Offline
                              L Offline
                              Luc Pattyn
                              wrote on last edited by
                              #14

                              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.

                              T 1 Reply Last reply
                              0
                              • L Luc Pattyn

                                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.

                                T Offline
                                T Offline
                                thomforum
                                wrote on last edited by
                                #15

                                Luc -- My apologies, on not seeing your first post. Well, that covers my requirement. cheers

                                1 Reply Last reply
                                0
                                Reply
                                • Reply as topic
                                Log in to reply
                                • Oldest to Newest
                                • Newest to Oldest
                                • Most Votes


                                • Login

                                • Don't have an account? Register

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • World
                                • Users
                                • Groups