Accessing Controls from Another Class with Dynamic Names

Discussion in 'Code Snippets and Tutorials' started by rcadble, Dec 19, 2007.

  1. rcadble

    rcadble Newbie

    Joined:
    Dec 16, 2007
    Messages:
    6
    Likes Received:
    0
    Hello,
    I have a small programming challenge I am encountering. I am working with a multithreaded application, and with each thread, a new set of controls is created, with the thread ID and control id. For instance, if it were the 3rd thread, it could be textbox2, listbox2, checkbox2, button2 etc.

    In the event handler for one of my buttons to be created at runtime, I have the following code:
    Code (Text):
    1. <div class="text" id="{CB}" style="font-family: monospace;"><ol><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;"> &nbsp; &nbsp;Public Sub StartBuying(ByVal sender As Object, ByVal e As System.EventArgs)</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; Dim intThreadID As Integer = Int(Replace(sender.name, "cmdStart", ""))</li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; ReDim Preserve Me.BuyThread(intThreadID)</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; Dim TempClassThread As BuyThreadClass</li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; TempClassThread = New BuyThreadClass()</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; Me.BuyThread(intThreadID) = New System.Threading.Thread(AddressOf TempClassThread.DoStartBuying)</li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; TempClassThread.ThreadID = intThreadID</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; TempClassThread.bolRunning = True</li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; Me.BuyThread(intThreadID).IsBackground = True</li><li style="padding: 0 5px; background-color: #f9f9f9; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; &nbsp; &nbsp; Me.BuyThread(intThreadID).Start()</li><li style="padding: 0 5px; background-color: #fff; line-height: 16px; border-bottom: 1px solid #efefef; border-left: 1px solid #999;">&nbsp; &nbsp; End Sub</li></ol></div>
    It creates a new class so that the "controls id" can be passed to the function the thread calls. However, even while knowing the names of the controls, I cannot access them.

    Any idea? Note that the controls are on a tab of a tabstrip in a tab of another tabstrip.
     
  2. the_skip

    the_skip Level IV

    Joined:
    Dec 25, 2006
    Messages:
    2,354
    Likes Received:
    1
    Location:
    Indiana
    When you start the thread pass it the form, you can do it with a this, next make a code that will find the id and then within the thread access the form you passed formname.nameoftextbox.text. No you can only read varibles from the textbvox otherwise you have to invoke the textbpx, google vb.net invoking, I programin mostly in C# so the syntax is different but it is .net none the less.
     
  3. rcadble

    rcadble Newbie

    Joined:
    Dec 16, 2007
    Messages:
    6
    Likes Received:
    0
    Okay,
    I can access the controls, but not all of them. It's only recognizing the controls created before runtime. For instance, when the user adds an account, a new tabpage is added to the tabstrip called tabMain, which has 1 tab to begin with. When I have added an account, it should have two tabs, but if I were to msgbox tabMain.tabcount, it would say 1.
     
  4. the_skip

    the_skip Level IV

    Joined:
    Dec 25, 2006
    Messages:
    2,354
    Likes Received:
    1
    Location:
    Indiana
    It would be easier to make a settings file for each account then load the setting file. Just a sugestion
     
  5. Mat2dong

    Mat2dong Level I

    Joined:
    Jan 27, 2008
    Messages:
    143
    Likes Received:
    0

    Any other info on this?
     
  6. rcadble

    rcadble Newbie

    Joined:
    Dec 16, 2007
    Messages:
    6
    Likes Received:
    0
    Nevermind, I got this, and finished the program. I would post a link but I wouldn't want to be accused of advertising.