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
  1. Home
  2. General Programming
  3. C#
  4. Can I access a user form object programmatically?

Can I access a user form object programmatically?

Scheduled Pinned Locked Moved C#
csharptutorialquestion
6 Posts 3 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.
  • H Offline
    H Offline
    humblepgmr
    wrote on last edited by
    #1

    I did this in VB and would like to do this in C# now. UserForm1.Controls("chkbx_ManProp_" & CStr(j)).Value = True any ideas how to convert this to C# in a windows form application? Thanks

    A 1 Reply Last reply
    0
    • H humblepgmr

      I did this in VB and would like to do this in C# now. UserForm1.Controls("chkbx_ManProp_" & CStr(j)).Value = True any ideas how to convert this to C# in a windows form application? Thanks

      A Offline
      A Offline
      Anthony Mushrow
      wrote on last edited by
      #2

      Crazy VB code, so you want to access the property of a control on another form? If so, add a method to the forms class that will modify the control. Then you can call the method.

      My current favourite word is: Waffle Cheese is still good though.

      H 1 Reply Last reply
      0
      • A Anthony Mushrow

        Crazy VB code, so you want to access the property of a control on another form? If so, add a method to the forms class that will modify the control. Then you can call the method.

        My current favourite word is: Waffle Cheese is still good though.

        H Offline
        H Offline
        humblepgmr
        wrote on last edited by
        #3

        I wrote the VB app in a short amount of time, so that snippet was pretty much done on the fly ;o the property of the control(s) that I want to access is within the same form. Unfortunately, there isn't a direct correlation of VB and C# methods so I'm not sure where to look. I was thinking about something like this: start a loop Application.getControlbyName("object_name_as_string" & integer_variable).value = false integer_variable++; end loop in essance what this should do is programmatically loop through all the controls with similar naming conventions, and set its member value to false. Any suggestions / alternate ideas on how to accomplish this in c#??? Thanks in advance Humble.

        M 1 Reply Last reply
        0
        • H humblepgmr

          I wrote the VB app in a short amount of time, so that snippet was pretty much done on the fly ;o the property of the control(s) that I want to access is within the same form. Unfortunately, there isn't a direct correlation of VB and C# methods so I'm not sure where to look. I was thinking about something like this: start a loop Application.getControlbyName("object_name_as_string" & integer_variable).value = false integer_variable++; end loop in essance what this should do is programmatically loop through all the controls with similar naming conventions, and set its member value to false. Any suggestions / alternate ideas on how to accomplish this in c#??? Thanks in advance Humble.

          M Offline
          M Offline
          Michael Potter
          wrote on last edited by
          #4
          ControlName.PropertyName = NewValue;
          

          Not sure why you want to loop through the control list when the above template should work within the same form. Of course, you could do the looping but it takes a bit more skill and would look something like the code below. I would stick with The Undefeated's advice.

          public static Control FindControl(Control root, string nameToFind)
          {
              Control retval = null;
              foreach (Control c in root.Controls)
              {
                  if (c.Name == NameToFind)
                  {
                      retval  = c;
                      break;
                  }
                  retval = FindControl(c, nameToFind);
                  if (retval != null)
                  {
                      break;
                  }
              }
              return retval;
          }
          
          H 1 Reply Last reply
          0
          • M Michael Potter
            ControlName.PropertyName = NewValue;
            

            Not sure why you want to loop through the control list when the above template should work within the same form. Of course, you could do the looping but it takes a bit more skill and would look something like the code below. I would stick with The Undefeated's advice.

            public static Control FindControl(Control root, string nameToFind)
            {
                Control retval = null;
                foreach (Control c in root.Controls)
                {
                    if (c.Name == NameToFind)
                    {
                        retval  = c;
                        break;
                    }
                    retval = FindControl(c, nameToFind);
                    if (retval != null)
                    {
                        break;
                    }
                }
                return retval;
            }
            
            H Offline
            H Offline
            humblepgmr
            wrote on last edited by
            #5

            Heh. Agreed that it's easier just to assign a value to a control. However, I've got 20 controls that need to be assigned the same value. I declared the control names with the express notion that I would be able to loop through all of them and assign with the same value. I guess I can modularize it and put it in a method, but I'm slightly surprised I"m not able to do what I could do with VBA code! :P

            M 1 Reply Last reply
            0
            • H humblepgmr

              Heh. Agreed that it's easier just to assign a value to a control. However, I've got 20 controls that need to be assigned the same value. I declared the control names with the express notion that I would be able to loop through all of them and assign with the same value. I guess I can modularize it and put it in a method, but I'm slightly surprised I"m not able to do what I could do with VBA code! :P

              M Offline
              M Offline
              Michael Potter
              wrote on last edited by
              #6

              That is because it is inefficient. There is little reason to loop through all controls everytime you want to update a specific one. VB tends to save lines of code not cpu cycles.

              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