showing different text in a pannel at runtime

Hi !

I am new with Windows Forms. 

I want to make an application interface with different panels. 

In one panel I just want display different information along the time,  showing what the application is doing (keep changing the information according to what happens in the application). 

How can I do that   (I don't want to use the status bar). Can I keep changing the text in a textBox or should one panel write on the top of the other panel , or what else   I have no idea. 

Thank you very much, 

Bia. 


Answer this question

showing different text in a pannel at runtime

  • keith826

    I'm not sure how projects written in something other than VB or C# start up...  Normally I would just say to go to Project -> Properties on the menu bar and under common properties -> general, set the Startup Form to Form1.  Then put any initialization code in the Form_Load event.

    But I'm not sure if that is how it works for you...  The lines of code you posted look like things that are written for you by Visual Studio when you create a new form in a VB/C# project; and it's usually better not to try to modify the areas of pregenerated code. 

  • David Rosa

    Hi rkimble, 

    Thanks for the answer, but I am not sure that you understood what I am asking so I will try to explain: 

    I am using Visual Studio .net (C++). 

    I know how to contruct my form using the Toolbox. When I do that, the Visual Studio authomatically generates the code in the Form1.cpp "main": 

    int APIENTRY _tWinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPTSTR    lpCmdLine,
                         int       nCmdShow)
    {
    System::Threading::Thread::CurrentThread->ApartmentState = System::Threading::ApartmentState::STA;
    Application::Run(new Form1());
    return 0;



    This Application::Run(new Form1()) function makes it impossible for me to put more code placed under this function "Application::Run" in the "main" because "Application::Run" is an infinite loop. The code never reaches "return0". This way, the application becomes event oriented, that is: after all objects are constructed, nothing happens until there is an event: mouse click, button click ... 
    I want to be able to put some code in the "main" function or in another place in the code, so that the code can automatically keep changing the text in the user-interface, without having to wait for a user event: mouse click, button click ... It is hard to explain. Does anybody have any idea of what can I do   

    One idea was to put the code in the Form1 object constructor, but I have realized that the Form1 one just appears on the screen when the constructor finishes. I have tried to change the text label of the Form1 in the constructor (first to show "Hi", wait, and then show "How are you"), but all I get on the screen is the last text ("How are you") because the Form1 just appears on the screen when the constructor code finishes. 

    Thank you very much in advance : )

    Bia. 


  • Tim Allen

    Thank you very much for the answer : )


    What if I want to show not only new text but also a whole new panel (sometimes) with buttons for the user to interact with the information which is being displayed 

    For instance: 

    First text on the panel: 
    "Testing Network connection"
    Second text on the panel: 
    "Network Connection OK"
    Third text on the panel: 
    "Testing display and keyboard"
    Fourth text on the panel: 
    "Do you see the letters A, B and C on the product display  "

        -----         -----
         yes          no
        -----         -----

    (two buttons on the panel)

    p.s. I say panel because on the same screen I want to have other panels with fixed information. 

    Thank you very much, 

    Bia. 

  • Justin Henley

    You can edit the text of any control at runtime, you don't have to create a new one on top of it:
    label1.Text = "Step 1";
    label1.Text = "Step 2";

  • VMSSanthosh

    Hi Bia,

    Thanks for the in-depth explaination; indeed I did not at all understand what you were trying to do.

    Ok, one thing that will help you moving forward is to forget about the "behind-the-scenes" code.  What I mean is, you should not try to make use of the fact that your form is actually a loop of code that runs until the form closes.  Although this code is necessary to actually create the form, it is designer code and not part of the event driven programming model.  It is the events that this code creates that are the core of your program design.

    So with that said, what you are looking for is an event that fires on a periodic cycle.  So you need to create one.  You could use a timer component, that would fire an event at a regular interval.  

    Or you could use multi-threading.  This is probably the best answer for what you want. Sorry I can't give you a C++ example, but here's the gist of a routine that updates a label with the number of seconds the app has been opened:

    Create some class-wide variables:

      [Variable Name]     [Type]          [Initial Value]

      [iCount]                 [Integer]      [0]
      [dLast]                  [DateTime]   [System.DateTime.Now (on Form_Load)]
      [RunLoop]              [Boolean]     [False]

    'Declare Class-Wide Variables
    Dim iCount as Integer
    Dim dLast as DateTime
    Dim RunLoop as Boolean


    Create a subroutine in your Form1 Class.  The code should be something like:

    'Declare a subroutine called TimeTick
        Private Sub TimeTick()

    'Begin a loop that continues until RunLoop becomes False
            Do While RunLoop

    'Use If-Then statement to get the TotalSeconds passed since dLast
                If System.DateTime.Now.Subtract(dLast).TotalSeconds > 1 Then

    'If TotalSeconds passed is greater than 1 increment iCount,
    'update a label on the form, and set dLast to the current time
                    
    iCount += 1
                    Label1.Text = iCount.ToString & " Seconds Have Passed"
                    dLast = System.DateTime.Now

                End If

            Loop

        End Sub



    In the Form_Load event, set your variables, create the thread and start it:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'Set variables to get things started
            iCount = 0
            dLast = System.DateTime.Now
            RunLoop = True

    'Create a New System.Threading.Thread at the AddressOf the TimeTick subroutine        
    Dim t As New System.Threading.Thread(AddressOf TimeTick)

    'Name the Thread
            t.Name = "TimeTick"

    'Start the Thread
            t.Start()


    End Sub



    In the Form_Closing event, allow your thread to exit:

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

    'Set the RunLoop variable to False        
    RunLoop = False

    End Sub



    To test this, create a form and put a label control on it.  Again, sorry the example is in VB but with the comments you should be able to work through the logic.  You see that in the code block "Do While RunLoop" you can have the thread do whatever work you need.

    Hope this helps!

  • Craig Skibo

    Thank you very much again !!! You are being very helpful : )

    Just one last question: 

    The Form1 is started using the Application::Run() function: 

    int APIENTRY _tWinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPTSTR    lpCmdLine,
                         int       nCmdShow)
    {
    System::Threading::Thread::CurrentThread->ApartmentState = System::Threading::ApartmentState::STA;
    Application::Run(new Form1());
    return 0;
    }


    That means that after the constructur is run, my application will only continue in the presence of an event (mouse click, button click, etc). 

    But I do not want my application to be event guided/oriented. Is there any way of avoiding the Application::Run() function or should I just put all the code inside the Objects constructors 

    (I do not know what this Application::Run() function does)

    Thank you very much again (and in advance), 

    Bia. 

  • AndyCourtenay

    Either create the controls on the panel at runtime based on where the user is in the app, or create all the panels at design time and control which ones can be seen using the .Visible property.

    Example:

    3 panels (Panel1, Panel2, Panel3) all occupy the same location on the screen.  Obviously, only one can be visible at a time.  The panels are all 200px by 400px in size and located at 0,0.

    To see Panel1:
    Panel1.Visible=True
    Panel2.Visible=False
    Panel3.Visible=False

    To see Panel2:
    Panel1.Visible=False
    Panel2.Visible=True
    Panel3.Visible=False


    Does this help

  • showing different text in a pannel at runtime