Results 1 to 9 of 9

Thread: [RESOLVED] Threading - Add textboxes to form in thread

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2008
    Posts
    32

    Resolved [RESOLVED] Threading - Add textboxes to form in thread

    Hi,

    I would like your help again, please.

    I'm just getting to grips with threading. I'm trying to add 20 textboxes to a form.

    I have 2 forms. The first one (form1) has a button 'Load textboxes'. This will take me to the second form (form2) where in the OnLoad event, it will create 20 textboxes and place it on form2.

    Each time, it adds a textbox to the form, it refreshes form2.

    I used Threading for this. I wish I could use Backgroundworker. I'm using Visual Studio 2005 with Pocket PC 2003. The language is Visual Basic.

    Here's my code.

    This is in my module modPocketPhone
    Code:
    Imports System.Threading
    Imports System.ComponentModel
    
    Module modPocketPhone
    
        Private oThreadStart As ThreadStart
        Private oThread As Thread
    
    
        Public Sub loadMytextBox()
            oThreadStart = New ThreadStart(AddressOf populateScreen)
    
            oThread = New Thread(oThreadStart)
    
            oThread.Start()
    
            Dim myTextBox As New TextBox
            myTextBox.Text = "Test"
            myTextBox.Top = 0
            myTextBox.Left = 30
            myTextBox.BackColor = Color.Red
            myTextBox.ForeColor = Color.White
            Form2.Controls.Add(myTextBox)
            Form2.Refresh()
    
        End Sub
    
    
        Public Sub populateScreen()
            Dim myTextBox As TextBox = Nothing
            Dim iHeight As Integer = 50
            Dim iWidth As Integer = 200
    
            Dim iTop As Integer = 0
            Dim iOffset As Integer = 6
    
            Dim i As Integer = 1
            Dim j As Integer = 20
    
            For i = 1 To j
                myTextBox = New TextBox
                myTextBox.Text = "Box " & i
                myTextBox.Height = iHeight
                myTextBox.Width = iWidth
                myTextBox.BackColor = Color.Azure
                myTextBox.Visible = True
                myTextBox.Left = 0
    
                If i > 0 Then
                    myTextBox.Top = iHeight + iTop + iOffset
                End If
    
                Try
                    Form2.Controls.Add(myTextBox)
                    Form2.Refresh()
                    Debug.WriteLine("Control  " & i)
                Catch ex As Exception
                    Debug.WriteLine(ex.Message)
                End Try
    
            Next
        End Sub
    
    End Module
    I ran the code. I didn't get any errors shown through my debugging.

    The above code is executed when form2 is loaded once the button in form1 is pressed. Form2 loads and fires up the procedure 'loadMyTextbox'. When it does, it creates a thread calling up up the procedure 'populateScreen'. This is where it creates the textboxes onscreen.

    This is my Form2 code.
    Code:
    Public Class Form2
    
        Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            loadMyTextBox()
        End Sub
    End Class
    I am doing this to create a questionnaire application. I noticed if I did not use Threading the process is quite intensive in loading controls to the screen. I can add a textbox to form2 without threading as you see in the lines followin 'oThread.Start'. I can see it on screen.

    When the thread runs, I do not see any of my controls.

    Can anyone please advise what I'm doing wrong?
    Last edited by Jova; Sep 5th, 2008 at 02:22 AM.

  2. #2
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    Re: Threading - Add textboxes to form in thread

    Have you stepped through your code to see what is happening - that would be the first port of call.

    It will take the same time to draw the controls using threading or not - why not just use a waitcursor?
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2008
    Posts
    32

    Re: Threading - Add textboxes to form in thread

    I did the step through the code but it broke down on the line.

    Code:
     Form2.Controls.Add(myTextBox)
    It said 'No Source Code Available for the current Location'.

    I copied two lines of code before and after the For...Next function.

    Code:
    MessageBox.Show(Form2.Controls.Count.ToString & " controls")
    When it hits the first one, it says "0 controls" and after the For...next completes, I get a messagebox "20 controls". The controls are there but not seen.

  4. #4
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    Re: Threading - Add textboxes to form in thread

    Stepping through your code, your textboxes stay at the same place in the loop

    Try

    If i > 0 Then
    myTextBox.Top = iHeight + iTop + iOffset
    iTop += (iHeight + iOffset)
    End If
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2008
    Posts
    32

    Re: Threading - Add textboxes to form in thread

    Hi Pete,

    Thanks for your help.

    This morning, I decided to come back to this wee problem after learning a lot more about VB.Net. I realised now I needed a delegate to add the controls. When you run a thread, you need to access the Main form's thread to add a control (if that makes sense).

    Maybe my solution below is not tidy but it works. I'm going to reuse this function as a template.

    I created a new project as I no longer have the old project.

    Create a new project and copy the code into your form1.vb file.

    Code:
    Public Class Form1
    
        ' FORM LOADING
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            ' CREATE THREAD AND START IT
            Dim trdAddTextBox As New Threading.Thread(AddressOf AddTextBox)
            trdAddTextBox.IsBackground = True
            trdAddTextBox.Start()
    
        End Sub
    
        ' CREATE DELEGATE
        Private Delegate Sub AddTextBoxDelegate()
    
        ' CREATE 5 TEXTBOXES
        Private Sub AddTextBox()
            If Me.InvokeRequired = True Then
                Dim d As New AddTextBoxDelegate(AddressOf AddTextBox)
                Me.Invoke(d)
            End If
    
            Dim i As Integer = 1
            Dim j As Integer = 5
    
            Dim iOffset As Integer = 25
            Dim iTop As Integer = 0
    
            For i = 1 To 5
                Dim myTextBox As New TextBox
    
                myTextBox.Width = 150
                myTextBox.Height = 50
                myTextBox.Top = iTop
                myTextBox.Left = 0
                myTextBox.Multiline = True
                myTextBox.Text = "Box " & i
    
                Try
                    Me.Controls.Add(myTextBox)
                Catch ex As Exception
                    'MessageBox.Show(ex.Message)
                End Try
    
                iTop += 55
            Next
    
        End Sub
    
    End Class
    This is my first foray into Threading.

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: [RESOLVED] Threading - Add textboxes to form in thread

    There's a slightly easier way to call a sub in a different thread, especially in the UI thread. I'd have to search for a link, or you could. The search would be on my user name and SynchronizationContext.

    Basically, each thread has a synchronization context, and you can make a SynchronizationContext variable and set it to the current context in the main thread. Then in other threads, if you call the Post method of that SynchronizationContext variable, you can pass it the address of the method you want to call, and that method will be called in the thread of the SynchronizationContext, which if you saved the context in the UI thread, would be the UI thread. Voila, you are calling a method in the UI thread from a background thread. This takes only a single line to save the SynchronizationContext, and a single line to call the Post method of the context.
    My usual boring signature: Nothing

  7. #7
    Lively Member
    Join Date
    Aug 2007
    Posts
    91

    Re: Threading - Add textboxes to form in thread

    Quote Originally Posted by Jova View Post
    Hi Pete,

    Thanks for your help.

    This morning, I decided to come back to this wee problem after learning a lot more about VB.Net. I realised now I needed a delegate to add the controls. When you run a thread, you need to access the Main form's thread to add a control (if that makes sense).

    Maybe my solution below is not tidy but it works. I'm going to reuse this function as a template.

    I created a new project as I no longer have the old project.

    Create a new project and copy the code into your form1.vb file.

    Code:
    Public Class Form1
    
        ' FORM LOADING
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            ' CREATE THREAD AND START IT
            Dim trdAddTextBox As New Threading.Thread(AddressOf AddTextBox)
            trdAddTextBox.IsBackground = True
            trdAddTextBox.Start()
    
        End Sub
    
        ' CREATE DELEGATE
        Private Delegate Sub AddTextBoxDelegate()
    
        ' CREATE 5 TEXTBOXES
        Private Sub AddTextBox()
            If Me.InvokeRequired = True Then
                Dim d As New AddTextBoxDelegate(AddressOf AddTextBox)
                Me.Invoke(d)
            End If
    
            Dim i As Integer = 1
            Dim j As Integer = 5
    
            Dim iOffset As Integer = 25
            Dim iTop As Integer = 0
    
            For i = 1 To 5
                Dim myTextBox As New TextBox
    
                myTextBox.Width = 150
                myTextBox.Height = 50
                myTextBox.Top = iTop
                myTextBox.Left = 0
                myTextBox.Multiline = True
                myTextBox.Text = "Box " & i
    
                Try
                    Me.Controls.Add(myTextBox)
                Catch ex As Exception
                    'MessageBox.Show(ex.Message)
                End Try
    
                iTop += 55
            Next
    
        End Sub
    
    End Class
    This is my first foray into Threading.
    thanks.
    this help me as well.

  8. #8
    Hyperactive Member Bajrang's Avatar
    Join Date
    Oct 2006
    Posts
    309

    Re: [RESOLVED] Threading - Add textboxes to form in thread

    12334
    Last edited by Bajrang; Sep 15th, 2009 at 06:41 AM.
    Using .NET 3.5 (VS 2008) And XP SP2

  9. #9
    Frenzied Member
    Join Date
    Oct 2005
    Posts
    1,286

    Re: [RESOLVED] Threading - Add textboxes to form in thread

    Hi,
    see this as an explanation of why you are getting that error
    Pete Vickers
    MVP - Device Application Development
    http://www.gui-innovations.com http://mobileworld.appamundi.com/blogs/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width