none
Windows Form not properly displayed when running code

    Question

  • Hi

    I am not experienced writing application using visual basic, so here is the problem encountered:

    I wanted to create a windows form application that would do some processing on the harddisk, then close when it finishes. The status of running would be shown via updating the label2.text.

    So I created a project with Windows form. And on the simple Windows form form1, I added an image and a few text labels.

    My code is shown as below. I wonder why is my windows form content not painted/shown properly when the application is running? I only see the window and the title, but anything within the window is blank or not drawn/painted properly. It looks like I have to pause the application with a MsgBox instruction, then I can see the Windows form contents. Any clues?

    Imports System.IO
    Imports System.Text.RegularExpressions
    Imports System.Xml

    Public Class Form1

        Private Sub Form1_Load(ByVal sender As System.Object,
                ByVal e As System.EventArgs) Handles MyBase.Load
            Console.WriteLine("blah blah")
            Label2.Text = "Started For HC"
            Dim workingFolder1 = "\\hc-fs-01\Audio\Public\"

             ...... some for loop reading the disk and writing to a file ....

    Wednesday, April 26, 2017 1:24 AM

All replies

  • You may have to force a refresh/update of the controls so that they
    get redrawn before your program goes into a lengthy file processing loop.

    Example:

    Label2.Text = "Started For HC"
    Label2.Update()

    - Wayne

    Wednesday, April 26, 2017 3:36 AM

  • Public Class Form1

        Private Sub Form1_Load(ByVal sender As System.Object,
                ByVal e As System.EventArgs) Handles MyBase.Load
            Console.WriteLine("blah blah")
            Label2.Text = "Started For HC"
            Dim workingFolder1 = "\\hc-fs-01\Audio\Public\"

             ...... some for loop reading the disk and writing to a file ....

    You could also try this in your Load event before starting your loop:

    Me.Refresh()

    - Wayne

    Wednesday, April 26, 2017 3:48 AM
  • Hi Lenny,

    Can you please add new Form, and add some controls, such as Label, TextBox, Button in the Form, then please remember to choose startup form(left-click project----click properties), run again.

    Best Regards,

    Cherry Bu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 26, 2017 5:30 AM
    Moderator
  • Hi Lenny Li,

    I suppose it takes long time to ...read the disk and write to a file...
    If this is correct and you want to show any changing on Form1, you should use Async and Await, and show a ProgressBar on Form1.
    Please refer to Asynchronous Programming with Async and Await (C# and Visual Basic).
    ___________
    Ashidacchi
    Wednesday, April 26, 2017 5:44 AM
  • The quick and dirty method is to just add the line of code Application.DoEvents to the loop. That should allow the user interface to be updated. Otherwise, you can use a BackgroundWorker Class, and run your processing code on a different thread, which is a little more involved but probably the pure way to handle the issue.

    Paul ~~~~ Microsoft MVP (Visual Basic)


    Wednesday, April 26, 2017 1:29 PM
  • Hi Lenny,

    Several persons have tried to resolve your issue.
    How is your problem? Have you done something?
    _____________
    Ashidacchi
    Wednesday, April 26, 2017 10:49 PM