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. display text in textbox

display text in textbox

Scheduled Pinned Locked Moved C#
databasequestioncsharp
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.
  • A Offline
    A Offline
    Abdul Rahman Hamidy
    wrote on last edited by
    #1

    i have developed a very simple application which contains a single win form and two classes. i would like provide or show some text from those classess into my win textbox form. how can i achieve it? what I want excatly sames as "View Syn status" in MY SQL Replication. i am trying to achieve from c#.

    Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

    OriginalGriffO 1 Reply Last reply
    0
    • A Abdul Rahman Hamidy

      i have developed a very simple application which contains a single win form and two classes. i would like provide or show some text from those classess into my win textbox form. how can i achieve it? what I want excatly sames as "View Syn status" in MY SQL Replication. i am trying to achieve from c#.

      Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

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

      Use

      string s = "Hello";
      textBox.Text = s;

      for a single line TextBox, or

      string[] as = new string[n];
      ...
      TextBox.Lines = as;

      for a multiline.

      No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

      "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

      A 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Use

        string s = "Hello";
        textBox.Text = s;

        for a single line TextBox, or

        string[] as = new string[n];
        ...
        TextBox.Lines = as;

        for a multiline.

        No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

        A Offline
        A Offline
        Abdul Rahman Hamidy
        wrote on last edited by
        #3

        my friend. i am asking about how can i access from other classess not from my win form. from win form that is ok, but if i want to access it from other classes how can i access it and set the text to "hello" or something else?

        Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

        A OriginalGriffO 2 Replies Last reply
        0
        • A Abdul Rahman Hamidy

          my friend. i am asking about how can i access from other classess not from my win form. from win form that is ok, but if i want to access it from other classes how can i access it and set the text to "hello" or something else?

          Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

          A Offline
          A Offline
          AhsanS
          wrote on last edited by
          #4

          Set a public property of the form to set the text like

          public string SetText
          {
          get
          {
          return textbox1.Text;
          }
          set
          {
          textbox1.Text = value;
          }
          }

          And call it from your class by accessing it through the instance of the form on which the text box resides.

          Ahsan Ullah Senior Software Engineer MCTS 2.0

          1 Reply Last reply
          0
          • A Abdul Rahman Hamidy

            my friend. i am asking about how can i access from other classess not from my win form. from win form that is ok, but if i want to access it from other classes how can i access it and set the text to "hello" or something else?

            Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

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

            If I understand you correctly, you have a form with a textbox, and a separate class which does some work and is to display text in the textbox? If yes, then either: 1) the class needs to be given the instance of the form. or 2) the class needs to signal to the form that data is ready for display. The first way is pretty trivial, the second is better practice. Assuming:

            myForm formMine = new myForm();
            myClass classMine = new myClass();

            In the first case, create a field in myClass:

            public myForm formDisplayResult;

            and assign it after the form and class creation:

            myForm formMine = new myForm();
            myClass classMine = new myClass();
            classMine.formDisplayResult = formMine;

            then access the textbox from your class:

            formDisplayResult.tbResultsGoHere.Text = "Hello";

            In the second case, it is a little more complex: In myClass:

             public partial class myClass
                {
                // Signal file change happened
                public delegate void ChangeHandler(object sender, EventArgs e);
                public event ChangeHandler Changed;
            
                protected virtual void OnChanged(EventArgs e)
                   {
                   ChangeHandler ch = Changed;
                   if (ch != null)
                      {
                      ch(this, e);
                      }
                   }
                   
                private void DoSomethingToChangeData()
                   {
                   OnChanged(null); 
                   }
                }
            

            ----- The asign to ch is in case the handler changes between null check ----- and exec. ----- (unlikely, but possible) ----- The null check is to ensure there is a handler. If not, better to ----- ignore it gracefully, than to rely on the thrown exception ----- (NullReferenceException) In myForm:

                myClass classMine = new myClass();
                public myForm()
                    {
                    classMine.Change += new myClass.ChangeHandler(Changed);
                    }
            
                //
                // Fired when the results are available
                //
                private void Changed(object sender, EventArgs e)
                    {
                    tbResultsGoHere.Text = classMine.strResults;
                    }
            

            It is easy really - you just have to get your head around it!

            No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

            "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

            A D 3 Replies Last reply
            0
            • OriginalGriffO OriginalGriff

              If I understand you correctly, you have a form with a textbox, and a separate class which does some work and is to display text in the textbox? If yes, then either: 1) the class needs to be given the instance of the form. or 2) the class needs to signal to the form that data is ready for display. The first way is pretty trivial, the second is better practice. Assuming:

              myForm formMine = new myForm();
              myClass classMine = new myClass();

              In the first case, create a field in myClass:

              public myForm formDisplayResult;

              and assign it after the form and class creation:

              myForm formMine = new myForm();
              myClass classMine = new myClass();
              classMine.formDisplayResult = formMine;

              then access the textbox from your class:

              formDisplayResult.tbResultsGoHere.Text = "Hello";

              In the second case, it is a little more complex: In myClass:

               public partial class myClass
                  {
                  // Signal file change happened
                  public delegate void ChangeHandler(object sender, EventArgs e);
                  public event ChangeHandler Changed;
              
                  protected virtual void OnChanged(EventArgs e)
                     {
                     ChangeHandler ch = Changed;
                     if (ch != null)
                        {
                        ch(this, e);
                        }
                     }
                     
                  private void DoSomethingToChangeData()
                     {
                     OnChanged(null); 
                     }
                  }
              

              ----- The asign to ch is in case the handler changes between null check ----- and exec. ----- (unlikely, but possible) ----- The null check is to ensure there is a handler. If not, better to ----- ignore it gracefully, than to rely on the thrown exception ----- (NullReferenceException) In myForm:

                  myClass classMine = new myClass();
                  public myForm()
                      {
                      classMine.Change += new myClass.ChangeHandler(Changed);
                      }
              
                  //
                  // Fired when the results are available
                  //
                  private void Changed(object sender, EventArgs e)
                      {
                      tbResultsGoHere.Text = classMine.strResults;
                      }
              

              It is easy really - you just have to get your head around it!

              No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

              A Offline
              A Offline
              Abdul Rahman Hamidy
              wrote on last edited by
              #6

              thx alot, I really appreciate the second one, the first one might be bad habbit :) but one thing, does the class inherits from some thing, or its simply public class className { //cnt and others {

              Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

              H OriginalGriffO 2 Replies Last reply
              0
              • A Abdul Rahman Hamidy

                thx alot, I really appreciate the second one, the first one might be bad habbit :) but one thing, does the class inherits from some thing, or its simply public class className { //cnt and others {

                Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

                H Offline
                H Offline
                Henry Minute
                wrote on last edited by
                #7

                Either. It makes no real difference for your example.

                Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                1 Reply Last reply
                0
                • A Abdul Rahman Hamidy

                  thx alot, I really appreciate the second one, the first one might be bad habbit :) but one thing, does the class inherits from some thing, or its simply public class className { //cnt and others {

                  Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

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

                  As Henry said, it doesn't matter - any class (derived or not) can generate events. Sorry for the late reply, I was busy making a bulk supply of meatballs...

                  No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                  "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

                    If I understand you correctly, you have a form with a textbox, and a separate class which does some work and is to display text in the textbox? If yes, then either: 1) the class needs to be given the instance of the form. or 2) the class needs to signal to the form that data is ready for display. The first way is pretty trivial, the second is better practice. Assuming:

                    myForm formMine = new myForm();
                    myClass classMine = new myClass();

                    In the first case, create a field in myClass:

                    public myForm formDisplayResult;

                    and assign it after the form and class creation:

                    myForm formMine = new myForm();
                    myClass classMine = new myClass();
                    classMine.formDisplayResult = formMine;

                    then access the textbox from your class:

                    formDisplayResult.tbResultsGoHere.Text = "Hello";

                    In the second case, it is a little more complex: In myClass:

                     public partial class myClass
                        {
                        // Signal file change happened
                        public delegate void ChangeHandler(object sender, EventArgs e);
                        public event ChangeHandler Changed;
                    
                        protected virtual void OnChanged(EventArgs e)
                           {
                           ChangeHandler ch = Changed;
                           if (ch != null)
                              {
                              ch(this, e);
                              }
                           }
                           
                        private void DoSomethingToChangeData()
                           {
                           OnChanged(null); 
                           }
                        }
                    

                    ----- The asign to ch is in case the handler changes between null check ----- and exec. ----- (unlikely, but possible) ----- The null check is to ensure there is a handler. If not, better to ----- ignore it gracefully, than to rely on the thrown exception ----- (NullReferenceException) In myForm:

                        myClass classMine = new myClass();
                        public myForm()
                            {
                            classMine.Change += new myClass.ChangeHandler(Changed);
                            }
                    
                        //
                        // Fired when the results are available
                        //
                        private void Changed(object sender, EventArgs e)
                            {
                            tbResultsGoHere.Text = classMine.strResults;
                            }
                    

                    It is easy really - you just have to get your head around it!

                    No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                    D Offline
                    D Offline
                    DaveyM69
                    wrote on last edited by
                    #9

                    Good answer! For future reference, there is no need to create a delegate if using 2.0 or above. public event ChangeHandler Changed; can be written as public event EventHandler Changed; If custom event args are needed, there is a generic event handler public event EventHandler<ChangedEventArgs> Changed; Yay, no more delegate writing when using events :-D

                    Dave
                    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                    Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
                    Why are you using VB6? Do you hate yourself? (Christian Graus)

                    OriginalGriffO 1 Reply Last reply
                    0
                    • OriginalGriffO OriginalGriff

                      If I understand you correctly, you have a form with a textbox, and a separate class which does some work and is to display text in the textbox? If yes, then either: 1) the class needs to be given the instance of the form. or 2) the class needs to signal to the form that data is ready for display. The first way is pretty trivial, the second is better practice. Assuming:

                      myForm formMine = new myForm();
                      myClass classMine = new myClass();

                      In the first case, create a field in myClass:

                      public myForm formDisplayResult;

                      and assign it after the form and class creation:

                      myForm formMine = new myForm();
                      myClass classMine = new myClass();
                      classMine.formDisplayResult = formMine;

                      then access the textbox from your class:

                      formDisplayResult.tbResultsGoHere.Text = "Hello";

                      In the second case, it is a little more complex: In myClass:

                       public partial class myClass
                          {
                          // Signal file change happened
                          public delegate void ChangeHandler(object sender, EventArgs e);
                          public event ChangeHandler Changed;
                      
                          protected virtual void OnChanged(EventArgs e)
                             {
                             ChangeHandler ch = Changed;
                             if (ch != null)
                                {
                                ch(this, e);
                                }
                             }
                             
                          private void DoSomethingToChangeData()
                             {
                             OnChanged(null); 
                             }
                          }
                      

                      ----- The asign to ch is in case the handler changes between null check ----- and exec. ----- (unlikely, but possible) ----- The null check is to ensure there is a handler. If not, better to ----- ignore it gracefully, than to rely on the thrown exception ----- (NullReferenceException) In myForm:

                          myClass classMine = new myClass();
                          public myForm()
                              {
                              classMine.Change += new myClass.ChangeHandler(Changed);
                              }
                      
                          //
                          // Fired when the results are available
                          //
                          private void Changed(object sender, EventArgs e)
                              {
                              tbResultsGoHere.Text = classMine.strResults;
                              }
                      

                      It is easy really - you just have to get your head around it!

                      No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                      A Offline
                      A Offline
                      Abdul Rahman Hamidy
                      wrote on last edited by
                      #10

                      Well, i think i need more help for (int i=0; iAbdul Rahaman Hamidy Database Developer Kabul, Afghanistan

                      OriginalGriffO 1 Reply Last reply
                      0
                      • A Abdul Rahman Hamidy

                        Well, i think i need more help for (int i=0; iAbdul Rahaman Hamidy Database Developer Kabul, Afghanistan

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

                        I think you mean that your class generates a sequence of results, rather than a single result? If so, then I would output them to a list and signal to the higher level that there were available. Specifically, I would set up a queue in your class:

                        public Queue<string> qsOutputResults = new Queue<string>(100);

                        (Always create a queue with a minimum size or it gets extended the first time you use it) Then when your results are available:

                        qsOutputResults.Enqueue(i.ToString());
                        OnChanged(null);

                        This adds the results to the queue and signals the results are ready. In your form:

                        private void Changed(object sender, EventArgs e)
                        {
                        while (classMine.qsOutputResults.Count > 0)
                        {
                        tbResultsGoHere.AppendText(classMine.qsOutputResults.Dequeue());
                        }
                        }

                        Using the queue means they come off in the same order they went on. If your form is too busy, they will just wait on the queue until you are ready. You can replace the string with whatever is sensible for your results (of course). You probably don't want to AppendText (it depends on your app) it was just for example.

                        No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                        "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

                        A 1 Reply Last reply
                        0
                        • D DaveyM69

                          Good answer! For future reference, there is no need to create a delegate if using 2.0 or above. public event ChangeHandler Changed; can be written as public event EventHandler Changed; If custom event args are needed, there is a generic event handler public event EventHandler<ChangedEventArgs> Changed; Yay, no more delegate writing when using events :-D

                          Dave
                          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                          Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
                          Why are you using VB6? Do you hate yourself? (Christian Graus)

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

                          Thanks! I didn't know that, I'll give it a try sometime soon. Would be nice to simplify delegate / handler construction a bit...

                          No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                          "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

                            I think you mean that your class generates a sequence of results, rather than a single result? If so, then I would output them to a list and signal to the higher level that there were available. Specifically, I would set up a queue in your class:

                            public Queue<string> qsOutputResults = new Queue<string>(100);

                            (Always create a queue with a minimum size or it gets extended the first time you use it) Then when your results are available:

                            qsOutputResults.Enqueue(i.ToString());
                            OnChanged(null);

                            This adds the results to the queue and signals the results are ready. In your form:

                            private void Changed(object sender, EventArgs e)
                            {
                            while (classMine.qsOutputResults.Count > 0)
                            {
                            tbResultsGoHere.AppendText(classMine.qsOutputResults.Dequeue());
                            }
                            }

                            Using the queue means they come off in the same order they went on. If your form is too busy, they will just wait on the queue until you are ready. You can replace the string with whatever is sensible for your results (of course). You probably don't want to AppendText (it depends on your app) it was just for example.

                            No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                            A Offline
                            A Offline
                            Abdul Rahman Hamidy
                            wrote on last edited by
                            #13

                            thx dude, that is really appreciated. the thing which i got concern with, My messsage will not increase than 1000 lines, in each 1000 lines i will clear the textbox contents, well do i need to use queue for my case, or i should use some other collection for good performance? and if the size of queue increase will it affect the speed and performance of computer?

                            Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

                            OriginalGriffO 1 Reply Last reply
                            0
                            • A Abdul Rahman Hamidy

                              thx dude, that is really appreciated. the thing which i got concern with, My messsage will not increase than 1000 lines, in each 1000 lines i will clear the textbox contents, well do i need to use queue for my case, or i should use some other collection for good performance? and if the size of queue increase will it affect the speed and performance of computer?

                              Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

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

                              The performance of the queue should be pretty good anyway (except when the size is increased, at which point a new structure has to be created and populated with a copy of the original). Just a case of setting the initial size to a reasonable limit. If you use another collection (a List<T> for example) then you would have to take care of the FIFO ordering which a Queue guarantees. If you are displaying the latest results (as you seem to) then why re-invent the wheel? [edit]Removed word "not" from "should not be pretty good" above[/edit]

                              No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                              "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

                              A 1 Reply Last reply
                              0
                              • OriginalGriffO OriginalGriff

                                The performance of the queue should be pretty good anyway (except when the size is increased, at which point a new structure has to be created and populated with a copy of the original). Just a case of setting the initial size to a reasonable limit. If you use another collection (a List<T> for example) then you would have to take care of the FIFO ordering which a Queue guarantees. If you are displaying the latest results (as you seem to) then why re-invent the wheel? [edit]Removed word "not" from "should not be pretty good" above[/edit]

                                No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

                                A Offline
                                A Offline
                                Abdul Rahman Hamidy
                                wrote on last edited by
                                #15

                                thx alot, you helped me alot, thanks again :)

                                Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan

                                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