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. How to change the text of a text box in form after the form is displayed to user?

How to change the text of a text box in form after the form is displayed to user?

Scheduled Pinned Locked Moved C#
csharphelptutorialquestion
9 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.
  • A Offline
    A Offline
    AmarjeetSinghMatharu
    wrote on last edited by
    #1

    Hello All, I need a guidance on following: 1. I want to crate a windows application with Form or dialog the application behaviour is such that, it shows a dialog or Form to user and simultaneously, 2. it process the data by listening to seria port and notifies on the form if a specific data is received. (I know how to implement in VC6) 3. when I open a VC#.Net application wizard it gives me a form class , but I am not able to find the place where to add a code for serial port because the form is run by application thread and is blocked once the Application.Run is called. 4. To simplyft points above , I want to change the text of a form text box after the form is displayed. Please see the below code for clarity: namespace WeighbridgeManager { static class WeighbridgeManager { /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); My Form WBScreen = new MyForm(); Application.Run(WBScreen); //WBScreen.txtSomeTextBox.Text = "Hello World!!!"; } } } The commented code never executes, I guess I am missing some threading logic here. Thanks. To simplyfy the problem, take it this way. I want to change the text in the Form commented part and where to place this text. La

    P L 2 Replies Last reply
    0
    • A AmarjeetSinghMatharu

      Hello All, I need a guidance on following: 1. I want to crate a windows application with Form or dialog the application behaviour is such that, it shows a dialog or Form to user and simultaneously, 2. it process the data by listening to seria port and notifies on the form if a specific data is received. (I know how to implement in VC6) 3. when I open a VC#.Net application wizard it gives me a form class , but I am not able to find the place where to add a code for serial port because the form is run by application thread and is blocked once the Application.Run is called. 4. To simplyft points above , I want to change the text of a form text box after the form is displayed. Please see the below code for clarity: namespace WeighbridgeManager { static class WeighbridgeManager { /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); My Form WBScreen = new MyForm(); Application.Run(WBScreen); //WBScreen.txtSomeTextBox.Text = "Hello World!!!"; } } } The commented code never executes, I guess I am missing some threading logic here. Thanks. To simplyfy the problem, take it this way. I want to change the text in the Form commented part and where to place this text. La

      P Offline
      P Offline
      PhilDanger
      wrote on last edited by
      #2

      Think of the Application.Run() command as starting a loop that continuously looks for updates and messages from inside the form that it is running. Any code after the .Run() will not execute until you close the form that is open. Thus, you must put code that you want implemented INSIDE of the the form that you created. So inside your WBScreen class, on your Form_Load (or constructor, or whenever you want it executed), place the code that you want to run.

      1 Reply Last reply
      0
      • A AmarjeetSinghMatharu

        Hello All, I need a guidance on following: 1. I want to crate a windows application with Form or dialog the application behaviour is such that, it shows a dialog or Form to user and simultaneously, 2. it process the data by listening to seria port and notifies on the form if a specific data is received. (I know how to implement in VC6) 3. when I open a VC#.Net application wizard it gives me a form class , but I am not able to find the place where to add a code for serial port because the form is run by application thread and is blocked once the Application.Run is called. 4. To simplyft points above , I want to change the text of a form text box after the form is displayed. Please see the below code for clarity: namespace WeighbridgeManager { static class WeighbridgeManager { /// /// The main entry point for the application. /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); My Form WBScreen = new MyForm(); Application.Run(WBScreen); //WBScreen.txtSomeTextBox.Text = "Hello World!!!"; } } } The commented code never executes, I guess I am missing some threading logic here. Thanks. To simplyfy the problem, take it this way. I want to change the text in the Form commented part and where to place this text. La

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, To add to what Phil told you, a Form normally is "event driven"; everything it does, is triggered by some signal that causes it to execute a "handler". Examples: when the form is first loaded (so it can become visible), it executes a Load event; if you add an appropriate OnLoad(...) method, that will run then (just once). when you add a Forms.Timer that will cause a particular method to be called periodically when you add a SerialPort in your form's constructor, that SerialPort has a DataReceived event, that could call one of your methods every time a specified amount of data has been received. A lot of the words in the above are keywords or even class/method names; look them up on MSDN to get all the details. I strongly suggest you get acquainted with Forms, Buttons, events and the like long before you start working with the SerialPort class ! :)

        Luc Pattyn


        try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


        A 1 Reply Last reply
        0
        • L Luc Pattyn

          Hi, To add to what Phil told you, a Form normally is "event driven"; everything it does, is triggered by some signal that causes it to execute a "handler". Examples: when the form is first loaded (so it can become visible), it executes a Load event; if you add an appropriate OnLoad(...) method, that will run then (just once). when you add a Forms.Timer that will cause a particular method to be called periodically when you add a SerialPort in your form's constructor, that SerialPort has a DataReceived event, that could call one of your methods every time a specified amount of data has been received. A lot of the words in the above are keywords or even class/method names; look them up on MSDN to get all the details. I strongly suggest you get acquainted with Forms, Buttons, events and the like long before you start working with the SerialPort class ! :)

          Luc Pattyn


          try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


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

          Hello Phil,Luc, Thankyou very much for the guidance. I did a brief study of what you sugested, article (http://www.codeproject.com/csharp/workerthread.asp ) states what you described. I am coming up following approach mainly based on the link I have given above: 1. Create a worker thread in the form class which will liten to serial port. 2. Keep on apending the serial port data as and when received by a deligate to form. 3. To get the text of a form's control, Call a deligate in worker thread which will return string by calling a function in form class to get the control's text. Please suggest. Thanks and Regards. Amarjeet.

          L 1 Reply Last reply
          0
          • A AmarjeetSinghMatharu

            Hello Phil,Luc, Thankyou very much for the guidance. I did a brief study of what you sugested, article (http://www.codeproject.com/csharp/workerthread.asp ) states what you described. I am coming up following approach mainly based on the link I have given above: 1. Create a worker thread in the form class which will liten to serial port. 2. Keep on apending the serial port data as and when received by a deligate to form. 3. To get the text of a form's control, Call a deligate in worker thread which will return string by calling a function in form class to get the control's text. Please suggest. Thanks and Regards. Amarjeet.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Sounds good to me, with one comment: "3. To get the text of a form's control, Call a deligate in worker thread which will return string by calling a function in form class to get the control's text." You could do the append logic in the delegate (and hence in the form class) itself, so to the worker thread it would mean: "here is new data, deal with it", and to form would take it to mean "I have got new data, I should append it to the previous data and update the GUI". Warning: the packets in which you will get data will be arbitrary, not necessarily entire lines of text, or entire units of whatever data structure they represent. :)

            Luc Pattyn


            try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


            A 1 Reply Last reply
            0
            • L Luc Pattyn

              Sounds good to me, with one comment: "3. To get the text of a form's control, Call a deligate in worker thread which will return string by calling a function in form class to get the control's text." You could do the append logic in the delegate (and hence in the form class) itself, so to the worker thread it would mean: "here is new data, deal with it", and to form would take it to mean "I have got new data, I should append it to the previous data and update the GUI". Warning: the packets in which you will get data will be arbitrary, not necessarily entire lines of text, or entire units of whatever data structure they represent. :)

              Luc Pattyn


              try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


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

              Hello Luc, Thanks for the prompt reply and validating the approach. What I understand from your comments in point 3 is: 3. I should keep sending the data from worker thread and let form append it?. got it. what I actually mean by point 3 beside this appending , I also needs to be quering on the form data For Ex: I have appended form text to 'Luc' and if textbox is not null and is 'luc' now do something else... My understanding for the above is , to know the text of a text box any point of time , call a delegate from worker thread returning string with calling function on the form. Is that right approach? Secondly: your warning comments mean what ? I did not follow that. please elobrate.

              L 2 Replies Last reply
              0
              • A AmarjeetSinghMatharu

                Hello Luc, Thanks for the prompt reply and validating the approach. What I understand from your comments in point 3 is: 3. I should keep sending the data from worker thread and let form append it?. got it. what I actually mean by point 3 beside this appending , I also needs to be quering on the form data For Ex: I have appended form text to 'Luc' and if textbox is not null and is 'luc' now do something else... My understanding for the above is , to know the text of a text box any point of time , call a delegate from worker thread returning string with calling function on the form. Is that right approach? Secondly: your warning comments mean what ? I did not follow that. please elobrate.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                Hi, what I meant was: make a clear split in functionality, try to define the work in the worker thread with a clear border; it could be "take care of serial communication", if so it would not know what will happen to the resulting data, it would not have to know about the GUI, etc. That's a clean approach; and later on you could reuse the worker thread's code. But it you give it some other definition, that can be fine too. The warning is this: suppose the target (the other side of your serial cable) sends two strings "aaaaaaaaaaaaaaa" and "11111111111111" one after the other. Then it would be wrong to think you will receive first "aaaaaaaaaaaaaaa" and then "11111111111111"; it might well turn out to be "aaaaaaaaaaaaaaa11111111111111" or "aaaaaaaa" and "aaaaaaa11111111111111" or "aaaaaaaa" and "aaaaaaa11" and "111111111111" That is, it is not because you are thinking about messages, that it will get received as complete messages, the serial port does not care about anything larger than a byte. :)

                Luc Pattyn


                try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


                A 1 Reply Last reply
                0
                • A AmarjeetSinghMatharu

                  Hello Luc, Thanks for the prompt reply and validating the approach. What I understand from your comments in point 3 is: 3. I should keep sending the data from worker thread and let form append it?. got it. what I actually mean by point 3 beside this appending , I also needs to be quering on the form data For Ex: I have appended form text to 'Luc' and if textbox is not null and is 'luc' now do something else... My understanding for the above is , to know the text of a text box any point of time , call a delegate from worker thread returning string with calling function on the form. Is that right approach? Secondly: your warning comments mean what ? I did not follow that. please elobrate.

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  Additional information on the messaging stuff: when operating in synchronous mode and expecting text, you can do ReadLine() to get an entire line of text; that's one way of dealing with textual messages; of course you could use the synchronous mode in a background thread. In case of binary data it seems you can't tell the system to return as soon as a particular byte value gets received (although that functionality exists in Win32). :)

                  Luc Pattyn


                  try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


                  1 Reply Last reply
                  0
                  • L Luc Pattyn

                    Hi, what I meant was: make a clear split in functionality, try to define the work in the worker thread with a clear border; it could be "take care of serial communication", if so it would not know what will happen to the resulting data, it would not have to know about the GUI, etc. That's a clean approach; and later on you could reuse the worker thread's code. But it you give it some other definition, that can be fine too. The warning is this: suppose the target (the other side of your serial cable) sends two strings "aaaaaaaaaaaaaaa" and "11111111111111" one after the other. Then it would be wrong to think you will receive first "aaaaaaaaaaaaaaa" and then "11111111111111"; it might well turn out to be "aaaaaaaaaaaaaaa11111111111111" or "aaaaaaaa" and "aaaaaaa11111111111111" or "aaaaaaaa" and "aaaaaaa11" and "111111111111" That is, it is not because you are thinking about messages, that it will get received as complete messages, the serial port does not care about anything larger than a byte. :)

                    Luc Pattyn


                    try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


                    A Offline
                    A Offline
                    AmarjeetSinghMatharu
                    wrote on last edited by
                    #9

                    Hello Luc, your points are well taken. I should rethink my approach as My serial port management is real complex, to share with you, it's a controlling two device circuits boards through two serial COM ports simultaneously. These device boards in turn needs to contol Weighbridges H/W like weighing scale, traffic lights, positioning beams etc. My confidence is getting better after discussing with you.:confused: Please stay tune. Very best Regards. Amarjeet.

                    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