Results 1 to 9 of 9

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

Threaded View

  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.

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