Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

I�m fairly new to VB .NET, and I�m stuck on the following problem:

 

I added a load of controls at runtime from code. Depending on the values in a table, I have a loop which creates a tab page and a button (and other controls on the tab page) at runtime.

I named each button/object with the number of the tab page created e.g. btnDel6 (for the xth Tab Page created e.g. btnDel(x).Name = "btnDelete" & x.ToString ).

 

I have bound the button to an event handler which works fine. This button will delete the Tab page and the related record in the table. However, I have hit a snag.

I need to reference another control I created on this tab page which stores my database information i.e. the primary key to be used when calling my stored procedure.

 

I thought it would simply be Me(�Control� & x.ToString).text

 

However, I get an error saying Me �cannot be indexed because it has no default property�. Its got me stumped, I�m sure there is an easy answer, I used to do similar things in VBA.

 

Thanks for any help.

Edited by jch001
Posted
Is it just my imagination or do clumps of the same question keep turning up at the same time? Assuming I'm understanding your question, you simply wish to access a control that was created at runtime, by using a string which represents its name. If this is the case then a search of the forums will find multiple answers to this question. To get you off the mark, the simplest solution is to loop through the controls (using recursion if you have container controls) and comparing the name of the control to your string untill you find it.
Anybody looking for a graduate programmer (Midlands, England)?
Posted

Thanks. Believe me I have been searching the web etc. Its one of those brain freeze moments which I just can't seem to get over. I must be typing in the wrong search criteria.

 

Ok some more info. I loop through as you describe

 

' Enumerate through form's controls looking for one by name:

 

For Each ctrl In Me.Controls

If TypeOf ctrl Is ComboBox Then

CB = CType(ctrl, ComboBox)

MsgBox(CB.Name)

If CB.Name = "cbSect" & x.ToString Then CB.Text = "Found Combo Box in code"

End If

Next

 

It doesn�t find my control: in my example there are 12 combo box�s that have been created at Runtime on the 12 Tab pages, also created.

I do have another Combo Box created outside the Tab Control which does get displayed in the message box above.

 

Do I have to reference the Tab Control to get this working, if so how?

 

Thanks again.

Posted

A TabPage is a Container Control so when you are searching through Me.Controls it will only see the top level controls, i.e. your Tab control and anything else placed directly on the form. You need to recursively call that section of code for each Container object to search through thier Control collections. Check this post for a good example by marble_eater.

 

http://www.xtremedotnettalk.com/showthread.php?t=96128

Anybody looking for a graduate programmer (Midlands, England)?
  • Leaders
Posted

Just a note, if you know which control will contain the control you are looking for (for instance, if it is always in the same TabPage) you can simply search the known container, and skip the recursion. Also, in .Net 2.0, you can actually retrieve controls from a control collection by their name...

Me.Controls["btnThingamajigger"].Text = "Thing-a-ma-jigger";

 

The problem, again, with that is that it only works on "top-level" controls unless you whip up a recursive function.

[sIGPIC]e[/sIGPIC]
  • 2 weeks later...
Posted

Adding a control to a control on a tab page at Runtime. VB

 

(not sure if I should post this as a new thread, its kind of related)

Further to my previous post, I am now trying to add a Combo Box to a data grid created on my Tab Page, which are all created at runtime.

 

Everything is working great. However, I am getting a little problem in that only the FIRST Combo Box is being created (and appearing on all the Grids). I have tracked it down to when I add the Combo Box:

 

dgTable = CType(dgSect.TableStyles(0).GridColumnStyles(0), DataGridTextBoxColumn)

 

dgTable.TextBox.Controls.Add(cbItem)

 

my DataGridTextBoxColumn (dgTable) has been bound to the first column in data grid (dgSect) , then finally I add the Combo Box (cbItem) to the DataGridTextBoxColumn. Normally this would be fine, this code works when the controls are on not on a tab page.

 

I know the code to add a control to a tab page is.

 

TABCONTROL.TabPages(x).Controls.Add(cbItem)

 

So, it must be some combination of the two. Any ideas? :confused:

 

p.s. I am using dynamic arrays in my actual code; I just didn't want to confuse things here.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...