Rhek

Hello,

I am having some problems adding controls at runtime, or I guess I should say accessing the controls after adding them.

The application starts off with a tab control named ¡°tabIncidents¡± and one tab for an incident information. The user can click on a button to add a client. When the button is pressed a second tab is added to ¡°tabIncidents¡± named ¡°Clients.¡± On the newly created ¡°Clients¡± tab another tabcontrol is added named ¡°tabClients¡± and we create a tab named ¡°Client¡±. Multiple ¡°Client¡± tabs can be created on this tabcontrol. Each ¡°Client¡± tab gets a usercontrol added named ¡°clientInformation¡± which has name and address textboxes.

So basically it is ¡­

TabIncidents.tabFirstTab (already exists)

TabIncidents.Clients (new tab added at runtime if user clicks a button)

TabIncidents.Clients.tabClients (new tabcontrol on the new tab)

TabIncidents.Clients.tabClients.Client (new tab added to new tabcontrol on the new tab, can have multiple client tabs)

TabIncidents.Clients.tabClients.Client.clientInformation (usercontrol added to the new tab¡­)

TabIncidents.Clients.tabClients.Client.clientInformation.txtFirstname (textbox in the usercontrol)

Hope this all makes sense so far. Lots of controls being created at runtime.

The problem is that I am unable to find a way to add a second tab to ¡°tabClients¡± because I cannot find the tab control but I can see the control on the form. I have searched in the watch window and I see ¡°tabIncidents¡± and can access all it¡¯s properties but ¡°tabClients¡± is not there. TabClients was available right after I added it to ¡°tabIncidents¡± but since I left that subroutine it is not.

I need to know how to access ¡°tabClients¡± so I can add a second Client tab. I also need to know how to access the user input in the textboxes, i.e. txtFirstname so I can load the textbox with information and retrieve information from the textbox. Again, there can be multiple txtFirstname textboxes all on different usercontrols.

Here is my code to create the ¡°tabClients¡± tab.

'Create tabClients i it does not exist

If Not TabFound Then

Dim clientsTab As Infragistics.Win.UltraWinTabControl.UltraTab

clientsTab = Me.tabPersonalInjury.Tabs.Add("Clients", "Clients")

'Create the tabClients->Client Tab Control

Dim tabClients As New Infragistics.Win.UltraWinTabControl.UltraTabControl

tabClients.Name = "tabClients"

tabClients.Dock = DockStyle.Fill

Me.tabPersonalInjury.Tabs("Clients").TabPage.Controls.Add(tabClients)

'Create the Client Tab on tabClients Tab Control

Dim tabClient As Infragistics.Win.UltraWinTabControl.UltraTab

Dim tabKey As String = "Client_1"

tabClient = tabClients.Tabs.Add(tabKey, "Client")

'Add the Client User Control to the Client Tab

Dim clientInfo As New ClientInformaiton

clientInfo.Dock = DockStyle.Fill

tabClient.TabPage.Controls.Add(clientInfo)

tabPersonalInjury.SelectedTab = Me.tabPersonalInjury.Tabs("Clients")

tabClients.SelectedTab = tabClients.Tabs(tabKey)

tabClients.Focus()

Else

Dim tabName As String = "Client" & Me.tabClients.Tabs.Count

¡®---------------------------------------------------------------------

¡®Problem¡­ I cannot access tabClients if it is already created at runtime

¡®---------------------------------------------------------------------

End If

Maybe my approach is just wrong but I have tried in the past to create controls at runtime and they create just great, I just can¡¯t access them or their properties after they are created.

Any help and/or guidance would really be appreciated.

Cheers,

Rhek



Re: Visual Basic General How do you access controls after creating them at runtime?

Spidermans_DarkSide - MSP, VSIP

Hi,

Here is one way.

I am not sure but i bet there is a way to do this without knowing the control name you are going to use in advance.

Regards,

S_DS

________________________________________________

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'Code to add a button as Button2 at runtime.

Me.Button2 = New System.Windows.Forms.Button

Me.SuspendLayout()

'

'Button2

'

Me.Button2.Location = New System.Drawing.Point(183, 159)

Me.Button2.Name = "Button2"

Me.Button2.Size = New System.Drawing.Size(75, 23)

Me.Button2.TabIndex = 0

Me.Button2.Text = "Button2"

Me.Button2.UseVisualStyleBackColor = True

Me.ResumeLayout(False)

Me.Controls.Add(Me.Button2)

End Sub

'This is added to make the Button2_Click code available and all

'of the code to add Button2 in the Button1_Click event.

Friend WithEvents Button2 As System.Windows.Forms.Button

Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

MessageBox.Show("Hello Rhek!!")

End Sub

End Class






Re: Visual Basic General How do you access controls after creating them at runtime?

Rhek

Hello,

That takes care of another question I was going to ask. Thanks for the reply!

I still am curious how I gather the values in all the textboxes, in the usercontrol, that is in the tabcontrol, that is contained in the tab that was all created at runtime. Pulling my hair out on that one and I really do like my hair!

Cheers,

Rhek





Re: Visual Basic General How do you access controls after creating them at runtime?

Spidermans_DarkSide - MSP, VSIP

Rhek wrote:

Hello,

That takes care of another question I was going to ask. Thanks for the reply!

I still am curious how I gather the values in all the textboxes, in the usercontrol, that is in the tabcontrol, that is contained in the tab that was all created at runtime. Pulling my hair out on that one and I really do like my hair!

Cheers,

Rhek

Hi,

I guess your user control is like a panel

Anyway each of your textboxes will have an assigned "name"

Like txt1 or whatever

How do you want to gather the values

As Strings

As Numbers

As a total of all the textboxes as one total number

As a long string

Please give me more details

If you are using numbers which TYPE of number are you using

Regards,

S_DS






Re: Visual Basic General How do you access controls after creating them at runtime?

Rhek

Hello,

Thanks for the reply. I think the best way to do give you more information would be to create a small project so I can show you what I am trying to accomplish. I will post a project tomorrow.

Thanks,

Rhek





Re: Visual Basic General How do you access controls after creating them at runtime?

Rhek

Hello,

I created a small sample project in VB 2005 using the tabcontrol and textboxes. Here is a link to the project zip file...

http://www.thorsoft.com/sample/rhekstabsample.zip

I thought showing an example would make it more clear as to what I was trying to accomplish.

Thanks,

Rhek





Re: Visual Basic General How do you access controls after creating them at runtime?

Rhek

Hello again,

Just thought I would check back in with you and see if my little project made any sense. If you have any other questions please let me know. I really appreciate you looking at this.

Thanks,

Rhek





Re: Visual Basic General How do you access controls after creating them at runtime?

Spidermans_DarkSide - MSP, VSIP

Hi,

My broadband internet connection has been down for 5 and a half days due to a fault on the original cable modem.

I will now look at it soon.

Regards,

S_DS






Re: Visual Basic General How do you access controls after creating them at runtime?

FalconDW

When I create controls at runtime, I just add an event handler when they are created, and point them to the appropriate function. For example,

<code to create your new control...>

AddHandler NewControl.Click, AddressOf myClickHandlerFunction

Would something like this not work in your situaton





Re: Visual Basic General How do you access controls after creating them at runtime?

Rhek

Hi,

That is probably something that I need to do but I am not sure how adding an event handler helps me get the values from the textboxes when I press the save button (the save button is not created at runtime). Another person mentioned adding an event handler so I might just be missing something in all this.

Can you explain how this would help me I would really appreciate it.

Thanks,

Rhek





Re: Visual Basic General How do you access controls after creating them at runtime?

FalconDW

I use the event handlers to capture the events I need for the controls, e.g. so I know when one was clicked on, etc. Regarding saving the value of them, this may not be the most ideal method, but when I create a new control, I add a new record to a datatable in my dataset (which I use to save the values) and assign the .name property of the control the value of my primary key for that record. This way I have a reference I can tap into if I need to know which dataset record is associated with each control. However, you still need to actually do something that saves the data of the control out to your dataset. To do this, you could create handlers on control leave events to save the data out to the the correct record... or you could even do what I have done in the past, which is to create a generic loop that goes through every control on the form (since I don't know how many were created), and then you can do whatever you want in the loop - such as write the values out to the dataset, etc.

I hope this makes sense...





Re: Visual Basic General How do you access controls after creating them at runtime?

nmadd

Hi there,
If I understand the question correctly, perhaps this will be of help to you as well:

Code Snippet

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Create the controls.
Dim tc As New TabControl()
tc.Name = "TabControl1"

Dim tp As New TabPage()
tp.Name = "TabPage1"

Dim txt As New TextBox()
txt.Name = "TextBox1"

' Add them to the form.
Me.Controls.Add(tc)
tc.Controls.Add(tp)
tp.Controls.Add(txt)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' Look for the text box named TextBox1.
Dim ctls() As Control = Me.Controls.Find("TextBox1", True)

If ctls.Length > 0 Then
' Cast the found control to a text box.
Dim myTextBox As TextBox = DirectCast(ctls(0), TextBox)
' Now the text box can be accessed.
myTextBox.Text = "Here I am."
End If
End Sub


Good luck.




Re: Visual Basic General How do you access controls after creating them at runtime?

daaf

i had a similar problem with a c# project. and i'm sure you can access those controls, but what i did is keep a local arraylist of the user controls. whenever i add it to a new tab, i also add it in the list, and when i need to access them i do a foreach on the arraylist.

that seemed to work...


how are you dinog with yoru problem




Re: Visual Basic General How do you access controls after creating them at runtime?

Rhek

Hi,

Still working on it but not going forward too much. Going to work on it some more this weekend and have a friend that was going to take a look. If I come up with something that works like I want I will post it.

Rhek