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. passing data to a textbox on a form using a method in a class

passing data to a textbox on a form using a method in a class

Scheduled Pinned Locked Moved C#
questionvisual-studio
9 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.
  • H Offline
    H Offline
    haz13
    wrote on last edited by
    #1

    I am trying to write data to a textbox I have on a form (Form1) from a method in a class. In my class I try writing - public class NumberProcessing { public static void UpdateNumArray() { Form1.textBox1.text = "hello"; } but the intellisense does not recognise anything past Form1. Why can I only access textbox1 when in public partial class Form1 : Form ? How can I access the textbox property of Form1 from other classes ? Haz

    E C 2 Replies Last reply
    0
    • H haz13

      I am trying to write data to a textbox I have on a form (Form1) from a method in a class. In my class I try writing - public class NumberProcessing { public static void UpdateNumArray() { Form1.textBox1.text = "hello"; } but the intellisense does not recognise anything past Form1. Why can I only access textbox1 when in public partial class Form1 : Form ? How can I access the textbox property of Form1 from other classes ? Haz

      E Offline
      E Offline
      Ennis Ray Lynch Jr
      wrote on last edited by
      #2

      Sorry, c# is not VB6 you need an object reference to Form1 in order to update it. If you insist on this pattern pass the reference to the form in the NumberProcessing contructor. However, I seriously recommend scrapping it and trying again. From what I see you are about to design something very unmaintainable. A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane

      H 1 Reply Last reply
      0
      • E Ennis Ray Lynch Jr

        Sorry, c# is not VB6 you need an object reference to Form1 in order to update it. If you insist on this pattern pass the reference to the form in the NumberProcessing contructor. However, I seriously recommend scrapping it and trying again. From what I see you are about to design something very unmaintainable. A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane

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

        Thanks for your reply. I am very new to C# coming from a C programming background. I do not fully follow what you mean as I understand constructors to be used when creating an instance of a class (i.e an object) here I am using static type which can not be initialized. If possible could you give me a short example of what you mean? Basically I want to know how I can get access to the textbox property from a class other than Form1? Or do I / should I always make reference to to the textbox property from within the Form1 class? Thanks in advance Haz

        E 1 Reply Last reply
        0
        • H haz13

          Thanks for your reply. I am very new to C# coming from a C programming background. I do not fully follow what you mean as I understand constructors to be used when creating an instance of a class (i.e an object) here I am using static type which can not be initialized. If possible could you give me a short example of what you mean? Basically I want to know how I can get access to the textbox property from a class other than Form1? Or do I / should I always make reference to to the textbox property from within the Form1 class? Thanks in advance Haz

          E Offline
          E Offline
          Ennis Ray Lynch Jr
          wrote on last edited by
          #4

          class SomeLogicClass{

          public static void DoSomething(Form form){
              //I can now modify any form at will
          }//end 
          
          public static void DoSomething(Control control){
              //I can now modify any control, at will
          }//end 
          
          public static void RegisterForm(Form form){
              //Hrmm maybe listen to an event on the form instead, to do something
          }
          

          }

          Also, you can create a singleton class. There are a lot of options. However, the path you are taking I have seen before and it will cause problems in the future. Look for some samples in a c# book as a good guide. A man said to the universe: "Sir I exist!" "However," replied the Universe, "The fact has not created in me A sense of obligation." -- Stephen Crane

          1 Reply Last reply
          0
          • H haz13

            I am trying to write data to a textbox I have on a form (Form1) from a method in a class. In my class I try writing - public class NumberProcessing { public static void UpdateNumArray() { Form1.textBox1.text = "hello"; } but the intellisense does not recognise anything past Form1. Why can I only access textbox1 when in public partial class Form1 : Form ? How can I access the textbox property of Form1 from other classes ? Haz

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            Trying for a more helpful version of the comment you got...

            haz13 wrote:

            Form1.textBox1.text = "hello";

            Form1 is a class. You need to access an instance of the class to pass a value through. What you're trying to do is a VB6 hack. The best way to do this is to create a delegate between the two classes. I assume delegates can be static, although I've never seen one. If not, you will need an instance of NumberProcessing inside Form1. The other way to do it is to create a property on Form1 which sets the text of textbox1, and pass a reference to the Form1 instance that's in use to NumberProcessing. This is less of a hack than what you're doing, but not as nice as the delegate. I trust the code you're showing is just pseudo code. There is no level on which textbox1 and form1 are acceptable variable names. Also, if your controls are public, change them to protected at least. That's just another poor design that you'll come to regret later. Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

            A H 3 Replies Last reply
            0
            • C Christian Graus

              Trying for a more helpful version of the comment you got...

              haz13 wrote:

              Form1.textBox1.text = "hello";

              Form1 is a class. You need to access an instance of the class to pass a value through. What you're trying to do is a VB6 hack. The best way to do this is to create a delegate between the two classes. I assume delegates can be static, although I've never seen one. If not, you will need an instance of NumberProcessing inside Form1. The other way to do it is to create a property on Form1 which sets the text of textbox1, and pass a reference to the Form1 instance that's in use to NumberProcessing. This is less of a hack than what you're doing, but not as nice as the delegate. I trust the code you're showing is just pseudo code. There is no level on which textbox1 and form1 are acceptable variable names. Also, if your controls are public, change them to protected at least. That's just another poor design that you'll come to regret later. Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

              A Offline
              A Offline
              AB7771
              wrote on last edited by
              #6

              Yes the use of delegates can do that, Declare a delegate event handler, implement that event handler in the Form1 class and invoke that event from the other class where you are trying to set the textbox value and pass the value as a parameter to the delegate. e.g public delegate void TextChangeEventHandler(string text); public delegate void ChangeText; Then implement this method : You can just write this.ChangeText += new TextChangeEventHandler(ChangeText); then the method Public void ChangeText(string Text) { textbox1.text = Text; } and in Your class u can just raise the event Form1.TextChangeEvent obj = new ... and obj.Invoke(); Hope that works for you Regards, Pramod

              1 Reply Last reply
              0
              • C Christian Graus

                Trying for a more helpful version of the comment you got...

                haz13 wrote:

                Form1.textBox1.text = "hello";

                Form1 is a class. You need to access an instance of the class to pass a value through. What you're trying to do is a VB6 hack. The best way to do this is to create a delegate between the two classes. I assume delegates can be static, although I've never seen one. If not, you will need an instance of NumberProcessing inside Form1. The other way to do it is to create a property on Form1 which sets the text of textbox1, and pass a reference to the Form1 instance that's in use to NumberProcessing. This is less of a hack than what you're doing, but not as nice as the delegate. I trust the code you're showing is just pseudo code. There is no level on which textbox1 and form1 are acceptable variable names. Also, if your controls are public, change them to protected at least. That's just another poor design that you'll come to regret later. Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                H Offline
                H Offline
                haz13
                wrote on last edited by
                #7

                Thanks for your more helpful version. I am new to this oop and slowly getting my head around it. I have gone away and read up on delegates but still fail to understand their use. I understand what you mean in your second method but am slightly confused about loading a reference into my NumberProcessing class. Surely when creating the instance Form1 which is public(of the form class) Form1 properties and methods should be available to other classes within the same namespace. I don't understand why I can't see them? OK - accepting you can't see the properties and methods of Form 1 in other classes, how do I load a reference of the Form1 object into my static NumberProcessing class? Here is my code - the problem is when trying to access Form1.SetFormData(); in the NumberProcessing class. namespace RouletteV1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btn1_Click(object sender, EventArgs e) { NumberAttributes.Load(1); } public void SetFormData() { //here is where I will set the textbox properties textBox1.Text = "Hello"; } } } -------------- namespace RouletteV1 { public static class NumberAttributes { public void Load(short Number) { // load the number data into global properties here NumberProcessing.UpdateNumArray(); } } --------------- namespace RouletteV1 { public static class NumberProcessing { public void UpdateNumArray() { // Here I want to call the SetFormData()function in Form1 but it won't recognise it - why??? Form1.SetFormData(); } } } Thanks in advance for your help - Please feel free to comment on what you think to the structure of the program - if you think I have made any other fundamental mistakes Haz

                1 Reply Last reply
                0
                • C Christian Graus

                  Trying for a more helpful version of the comment you got...

                  haz13 wrote:

                  Form1.textBox1.text = "hello";

                  Form1 is a class. You need to access an instance of the class to pass a value through. What you're trying to do is a VB6 hack. The best way to do this is to create a delegate between the two classes. I assume delegates can be static, although I've never seen one. If not, you will need an instance of NumberProcessing inside Form1. The other way to do it is to create a property on Form1 which sets the text of textbox1, and pass a reference to the Form1 instance that's in use to NumberProcessing. This is less of a hack than what you're doing, but not as nice as the delegate. I trust the code you're showing is just pseudo code. There is no level on which textbox1 and form1 are acceptable variable names. Also, if your controls are public, change them to protected at least. That's just another poor design that you'll come to regret later. Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                  H Offline
                  H Offline
                  haz13
                  wrote on last edited by
                  #8

                  Forget my last question. It has just clicked!!! Everything makes sense now :) Thanks

                  Haz

                  C 1 Reply Last reply
                  0
                  • H haz13

                    Forget my last question. It has just clicked!!! Everything makes sense now :) Thanks

                    Haz

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #9

                    *grin* No worries, glad we got there.

                    Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

                    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