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. Geting variables from UserControl

Geting variables from UserControl

Scheduled Pinned Locked Moved C#
questiondata-structures
5 Posts 2 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.
  • M Offline
    M Offline
    Muntyness
    wrote on last edited by
    #1

    As the subject says, how do I get a variable out of a user control? I have a user control in my main form, which contains a text box. On pressing a button on the main form, I want a message box to pop up with whatever I put into the textbox. So how do you do this? (I can do everything but get the text from the contrl to the messagebox in my main form) Just a note, I need it this way, because later on the program is going to take a bunch of variables from multiple UserControls, and pass it into a global array. - Munty

    R 1 Reply Last reply
    0
    • M Muntyness

      As the subject says, how do I get a variable out of a user control? I have a user control in my main form, which contains a text box. On pressing a button on the main form, I want a message box to pop up with whatever I put into the textbox. So how do you do this? (I can do everything but get the text from the contrl to the messagebox in my main form) Just a note, I need it this way, because later on the program is going to take a bunch of variables from multiple UserControls, and pass it into a global array. - Munty

      R Offline
      R Offline
      Robert Rohde
      wrote on last edited by
      #2

      Hi, define a property in your user control and access it from your main form:

      //in the user control
      public string MtyTextBoxText {
      get { return myTextBox.Text; }
      }

      //in the main form
      public void DoSomething() {
      MessageBox.Show(myUserControl.MyTextBoxText);
      }

      Robert

      M 1 Reply Last reply
      0
      • R Robert Rohde

        Hi, define a property in your user control and access it from your main form:

        //in the user control
        public string MtyTextBoxText {
        get { return myTextBox.Text; }
        }

        //in the main form
        public void DoSomething() {
        MessageBox.Show(myUserControl.MyTextBoxText);
        }

        Robert

        M Offline
        M Offline
        Muntyness
        wrote on last edited by
        #3

        That worked perfectly, thanks. :) Now for another (probably silly) question. The user control gets created by a button press. It gets added to an array. Now I want a button in the UserControl that can remove that particular UserControl from the GUI (and stop the code from seeing it) Now I can remove it from the GUI with the button inside the UserControl, but I'm not sure how (or if) you can remove the user control from the array in the main form, from the button in the user control. So is this possible? (I'm assuming some sort of event handler could be used) If not, I can work around the problem, but it would be better (much better) to remove the control from the array. - Munty

        R 1 Reply Last reply
        0
        • M Muntyness

          That worked perfectly, thanks. :) Now for another (probably silly) question. The user control gets created by a button press. It gets added to an array. Now I want a button in the UserControl that can remove that particular UserControl from the GUI (and stop the code from seeing it) Now I can remove it from the GUI with the button inside the UserControl, but I'm not sure how (or if) you can remove the user control from the array in the main form, from the button in the user control. So is this possible? (I'm assuming some sort of event handler could be used) If not, I can work around the problem, but it would be better (much better) to remove the control from the array. - Munty

          R Offline
          R Offline
          Robert Rohde
          wrote on last edited by
          #4

          Hi, yes I would use an event in this case and also let the form remove the control instead of letting the control remove itself:

          //in your userControl
          public event EventHandler RemoveButtonPressed;

          //in the button event handler in the user control
          if (RemoveButtonPressed != null)
          RemoveButtonPressed(this, EventArgs.Empty);

          //in your main form when adding the control
          myUserControl.RemoveButtonPressed += new EventHandler(OnRemoveButtonPressed);

          //and the appropriate event handler
          private void OnRemoveButtonPressed(object sender, EventArgs ea) {
          MyUserControl ctrl = (MyUserControl)sender;

          //don't forget to unbind the event
          ctrl.RemoveButtonPressed -= new EventHandler(OnRemoveButtonPressed);

          //remove from array and from form
          }

          Robert

          M 1 Reply Last reply
          0
          • R Robert Rohde

            Hi, yes I would use an event in this case and also let the form remove the control instead of letting the control remove itself:

            //in your userControl
            public event EventHandler RemoveButtonPressed;

            //in the button event handler in the user control
            if (RemoveButtonPressed != null)
            RemoveButtonPressed(this, EventArgs.Empty);

            //in your main form when adding the control
            myUserControl.RemoveButtonPressed += new EventHandler(OnRemoveButtonPressed);

            //and the appropriate event handler
            private void OnRemoveButtonPressed(object sender, EventArgs ea) {
            MyUserControl ctrl = (MyUserControl)sender;

            //don't forget to unbind the event
            ctrl.RemoveButtonPressed -= new EventHandler(OnRemoveButtonPressed);

            //remove from array and from form
            }

            Robert

            M Offline
            M Offline
            Muntyness
            wrote on last edited by
            #5

            Cool, thanks! I'll try that out tommorow then. :) - Munty

            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