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. textbox control

textbox control

Scheduled Pinned Locked Moved C#
questionlearning
7 Posts 4 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.
  • J Offline
    J Offline
    John L DeVito
    wrote on last edited by
    #1

    Heyas all, I have project that has a mainform and on it is a read-only textbox control. I have another source file that hold a class I made myself. I'm trying to access the textbox control that is on the mainform from my class file, and I keep getting erros that it's inacceble. The code wizard automatically creates the control as private. I have a method in my class that simply does this: void vWriteToTextbox(void) { // write to textbox on mainform I'm trying somsthing like... // MainForm.textbox1.Text = "Hello"; } and like I said it's not working. How can I access the control from my file (which of course is in the same project)? Thanks, John

    L C 2 Replies Last reply
    0
    • J John L DeVito

      Heyas all, I have project that has a mainform and on it is a read-only textbox control. I have another source file that hold a class I made myself. I'm trying to access the textbox control that is on the mainform from my class file, and I keep getting erros that it's inacceble. The code wizard automatically creates the control as private. I have a method in my class that simply does this: void vWriteToTextbox(void) { // write to textbox on mainform I'm trying somsthing like... // MainForm.textbox1.Text = "Hello"; } and like I said it's not working. How can I access the control from my file (which of course is in the same project)? Thanks, John

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

      John L. DeVito wrote:

      How can I access the control from my file

      Why would you want to do this? You can either make the control varialbe "textbox1" public, or create a get-function (or property) to return the textbox variable like this: public TextBox box { get { return textbox1; } } You also need to have a reference to your main form, you can't just do MainForm.something unless something is declared static regards

      1 Reply Last reply
      0
      • J John L DeVito

        Heyas all, I have project that has a mainform and on it is a read-only textbox control. I have another source file that hold a class I made myself. I'm trying to access the textbox control that is on the mainform from my class file, and I keep getting erros that it's inacceble. The code wizard automatically creates the control as private. I have a method in my class that simply does this: void vWriteToTextbox(void) { // write to textbox on mainform I'm trying somsthing like... // MainForm.textbox1.Text = "Hello"; } and like I said it's not working. How can I access the control from my file (which of course is in the same project)? Thanks, John

        C Offline
        C Offline
        Curtis Schlak
        wrote on last edited by
        #3

        Okay, the easy way to fix this is select the TextBox in the form designer and change the value of the Design >> Modifiers property to public. While that's the easy way, it breaks the encapsulation principle of OOD. May I suggest creating a property in your main form that exposes the value of the TextBox? For example, in your main form's class declaration, provide the following declaration.

        public string MyInterestingTextBoxValue
        {
        set
        {
        this.textBox1.Text = value;
        }
        }

        Then, you can change your method above to

        private void vWriteToTextbox()
        {
        // Write to textbox on main form
        MainForm.MyInterestingTextBoxValue = "Hello";
        }

        "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty

        J 2 Replies Last reply
        0
        • C Curtis Schlak

          Okay, the easy way to fix this is select the TextBox in the form designer and change the value of the Design >> Modifiers property to public. While that's the easy way, it breaks the encapsulation principle of OOD. May I suggest creating a property in your main form that exposes the value of the TextBox? For example, in your main form's class declaration, provide the following declaration.

          public string MyInterestingTextBoxValue
          {
          set
          {
          this.textBox1.Text = value;
          }
          }

          Then, you can change your method above to

          private void vWriteToTextbox()
          {
          // Write to textbox on main form
          MainForm.MyInterestingTextBoxValue = "Hello";
          }

          "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty

          J Offline
          J Offline
          John L DeVito
          wrote on last edited by
          #4

          Thanks Curtis I like the idea of wrapping it in a property. I know I could have changed the access identifier but I really REALLY didn't want to do that. Appreciate the time, works great. Thanks again. :) Thanks, John

          C 1 Reply Last reply
          0
          • J John L DeVito

            Thanks Curtis I like the idea of wrapping it in a property. I know I could have changed the access identifier but I really REALLY didn't want to do that. Appreciate the time, works great. Thanks again. :) Thanks, John

            C Offline
            C Offline
            Curtis Schlak
            wrote on last edited by
            #5

            John, my pleasure. Hope all turns out well. Happy coding! "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty

            1 Reply Last reply
            0
            • C Curtis Schlak

              Okay, the easy way to fix this is select the TextBox in the form designer and change the value of the Design >> Modifiers property to public. While that's the easy way, it breaks the encapsulation principle of OOD. May I suggest creating a property in your main form that exposes the value of the TextBox? For example, in your main form's class declaration, provide the following declaration.

              public string MyInterestingTextBoxValue
              {
              set
              {
              this.textBox1.Text = value;
              }
              }

              Then, you can change your method above to

              private void vWriteToTextbox()
              {
              // Write to textbox on main form
              MainForm.MyInterestingTextBoxValue = "Hello";
              }

              "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty

              J Offline
              J Offline
              John L DeVito
              wrote on last edited by
              #6

              Heyas Curtis, I've actually come across a bit of a problem, VS is saying that in order for me to set this property value I must have an instantiated object. I don't understand that. the MainForm is going to be instantied when it runs. Here's my code: In MainForms Class description I have... internal String txtStatusValue{ set{ this.txtStatus.Text = value;}} and then in my class file I have.. internal void vMyFunction() { MainForm.txtStatusValue = "Blah Blah Blah"; } I don't get it, could I trouble you for but more assistance? Thanks, John

              T 1 Reply Last reply
              0
              • J John L DeVito

                Heyas Curtis, I've actually come across a bit of a problem, VS is saying that in order for me to set this property value I must have an instantiated object. I don't understand that. the MainForm is going to be instantied when it runs. Here's my code: In MainForms Class description I have... internal String txtStatusValue{ set{ this.txtStatus.Text = value;}} and then in my class file I have.. internal void vMyFunction() { MainForm.txtStatusValue = "Blah Blah Blah"; } I don't get it, could I trouble you for but more assistance? Thanks, John

                T Offline
                T Offline
                TheGreatAndPowerfulOz
                wrote on last edited by
                #7

                sounds like your calling the txtStatusValue before the mainform is instantiated or before the variable you're using to call the mainform propery is initialized. do something like this: public class MainForm: Form { public static MainForm myMainForm = null; public MainForm() { myMainForm = this; // first line of code in ctor (before InitializeComponent() and before other objects get ctor'd } internal public string txtStatusValue { set { txtStatus.Text = value; } } } public MyClass { public void someFunc() { MainForm.myMainForm.txtStatusValue = "foo"; } } hope this helps...

                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