master_rigel2

How do I retrieve the name of a new control that I add to my form programmatically.

I use this line of code
dim new textbox as textbox.

The textbox appears on my form, at run-time, but I can't access it's properties, or retrieve it's name.

I could just use a loop that says something to the effect of

"Each time a textbox is added, the textbox name becomes textbox(number + 1) and track it that way, but I can't see that being the most effective way. Doesn't every control have to have programmatically accessible properties and functions

Rigel



Re: Visual Basic General retrieve the name of a "NEW" control

Mick Doherty

Name is not a necessary property, it is purely there for your convenience.

Every Control has a unique Handle and this is what is used to Identify it.

If you need it to have a name, then it is up to you to assign one:

Code Snippet

Dim myTextBox As New TextBox

myTextBox.Name = GetUniqueName()

...but why do you need a Name

You can access the controls properties by the temporary reference you created when declaring it.

This temporary reference is only valid within the method that created it, and only for the lifetime of that method.

Code Snippet

Dim someTextBox As New TextBox

someTextBox.Location = GetNextLocation()

Me.Controls.Add(someTextBox)





Re: Visual Basic General retrieve the name of a "NEW" control

master_rigel2

I'm sorry. This doesn't help me. I know the control's location, but how does that help me.

I'm creating a new textbox using the dim function, but letting Visual Studio to define it's attributes.

What I need to do is track down the control, regardless of where it is on the form. I need to know how to call this new control that's been put on my form.

If I can't do that, then how do I increment the name of new controls I create

ex

Code Snippet
dim textboxname(using variable).textbox as new textbox

I don't know how to do this.






Re: Visual Basic General retrieve the name of a "NEW" control

ReneeC

There's a nice way to do this. You don't need the name at all.

Declare the textbox as a member variable

Protected friend withevents NewTextbox as TextBox

When you want it:

Newtextbox = new textbox

Control.Controls.add(NewTextbox)

Newtextbox.location = new position(200,200)






Re: Visual Basic General retrieve the name of a "NEW" control

master_rigel2

Sorry... I made a mistake in my code sample. What I actually typed was.

Code Snippet
Controls.Add(
New TextBox)

So there's a new textbox on my form. How do I get its properties.

From now on I'll test my code before I post it to the forms.

If you can help me with this, that would be great.






Re: Visual Basic General retrieve the name of a "NEW" control

ReneeC

The way I did it, it would be

Dim TextLength as Integer = Newtextbox.Text.Length






Re: Visual Basic General retrieve the name of a "NEW" control

Rea Software Engineering

Hi rigel2,

The short answer is that what you want to do, and the way you are doing it is actually not compatible. The reason is that if you programmatically add a Textbox the "name" descriptor is not assigned. This is a value that you would have to assign at run-time. Since there is no name to reference (at least VB doesn't know what it is) you can't create code at design time to handle, or interact, with the new textbox.

Remember that VB.Net is an object-oriented programming language. This means that if you want to create a new textbox, programmatically, you will first want to create an "object" instance of the new textbox. Once you create the object instance you can then manipulate it in any way you wish (within the rules of programming of course).

Below is a code sample of how to do this. If you wish to try out this code then create a new Windows Application project (leave everything at the default names, if you want to copy and paste the code example). I'm providing a simple example that will show how to add only 1 textbox and to access the members of the textbox.

Code Snippet

Public Class Form1

'Declare object instance for a new TextBox

Public objTextBox1 As New TextBox


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'Add the object instance to the form

Controls.Add(objTextBox1)

'Name the new textbox (only because this was your original question)

objTextBox1.Name = "Custom1"

'Display within the textbox the name of the new textbox

objTextBox1.Text = "My name is: " & objTextBox1.Name

End Sub

End Class

As ReneeC had mentioned, it really isn't necessary to place a value for the "name" property. I would like to add, that this is because you can't access the textbox members using the name property; this is because VB has no idea the textbox exists (hence why you are programmatically creating, instead of at design time). The only way to access the members is through your object reference; hence why you MUST use an object instance if you want to interact with the new textboxes!

One last note, if you want to create multiple textboxes then you will want to create an array (or better yet, use 'Generics'..in particular the 'Generic.Collections' namespace). If you don't create an array (or collection) of textboxes then either VB will only access your last created object instance...or you will have to create individual object instances each time you create a new textbox. I encourage you to mess around with both options and decide which best fits your needs; I would suspect if you are only creating 1 - 3 boxes then individual objects will be fine and easy to maintain; otherwise read-up on arrays (and especially Generic.Collections).

Keep in mind that if you want to do something programmatically then object-oriented programming is the way to go; otherwise you will have a large headache and more times than not come up short with the answer.

I hope the above helps you!

Thanks,

James






Re: Visual Basic General retrieve the name of a "NEW" control

master_rigel2

Thanks, James.

Now i have one more tiny question. Yikes!... The visual basic guided tour mentioned nothing about generics... How do I use them Or...

What I'm trying to accomplish, which I thought I need an object name for was:

I was creating a check list program, where every time the user clicked enter on, or clicked out of the last textbox in the list, another would automatically appear below it, be given an object instance, and have it's properties accessible to me.

One would think it frustrating and, not to mention silly, to have to create all thsoe object instances in advance. The number could be potentially endless... Is there an easy way to do this You mentioned generics... Will generics work for what I'm trying to accomplish






Re: Visual Basic General retrieve the name of a "NEW" control

master_rigel2

I was wondering... Is it possible to add checkboxes and such to correspond with an embedded SQL dataset on a windows form If so, it may be drasticaly simpler for me to create a SQL Database, bind it to my project, and have corresponding check-box values for the status of each task in teh check-list....

Could this work out






Re: Visual Basic General retrieve the name of a "NEW" control

Rea Software Engineering

Hi rigel2,

Generics is more for the intermediate user; however, what you are trying to accomplish is really an intermediate developers task...if not an advanced user's task. You can read further information at: http://msdn2.microsoft.com/en-us/library/system.collections.generic(VS.80).aspx. This information is very technical and broad; it will lead to very detailed information and can prove to be helpful.

As for creating the objects, yes it is frustrating at first. It gets easier as you do it more and more; this is how programmers did things before the nice graphical user interface called "Visual Basic IDE". You can best think of it as a necessity; usually VB IDE will do this for you when you just drag-n-drop controls onto your windows form. Another way to think of it, is could you tell me right this second what page # in a given dictionary the word "application" appears The reason I ask, is this is what you are ultimately telling VB you want to know. You want it to know that you "will" create a textbox and that you want to access it; problem is that it doesn't know where the textbox is and thus you have to tell it to hold the information in an object, so you can later use that object. I hope this all makes sense (I'm trying to find a way to relate the basic principal of what is going on to you, with the knowledge that you are fairly new to programming).

Generics can help you; but, most likely it will be an overkill and more frustrating because you probably don't quite have the full understanding of programming concepts yet. Once you get more solid in the concepts then Generics can become your best friend. Generics are currently under used by many programmers; it's to the point of where just about every other magazine issue in a programming magazine will cover this topic in some form. Don't feel left out about not knowing about them or how to use them. Try to research more and more when you can and find ways it will help you out. MSDN has many articles, webcasts and labcasts covering the subject...problem is you have to search for them. Would be nice if Microsoft would make them more readily available.

Here I am providing a simple example of how to use a generic list; I also created a custom sub-routine for creating a textbox. You will, hopefully, see how they can easily interact with each other. The biggest problem is that you will have to manually maintain a list of indexes for the textboxes if you wish to use a textbox later in your code. There are multitude of methods to do this; so I didn't address this. It should be fairly simple for you to figure that part out. I didn't comment the code very well, since I made this on the fly in the past hour...feel free to post questions about the code. Most of my comments are just thoughts on what I am doing with that line of code or of things I think you should pay particular attention to. Also feel free to re-use the code in your application; I do warn you that I did not thoroughly test it and it doesn't necessarily follow the "best practices" guidelines.

You will want to create a new form, add a listbox to the upper right-hand corner, two buttons to the lower left hand corner. Keep all controls in their original names and sizes. The first button, "Button 1", will add the textboxes. The second button, "Button 2", will perform work using the textboxes. This is meant to show you how to use the textboxes if you don't retain seperate object instances and choose to use a generic list collection.

Here is the code:

Code Snippet

Public Class Form1

'Declare the public variables

Public intCount, intY, intX As Integer

'This declaration is using a "Generic Collection - List"

Public lstBoxes As New List(Of TextBox)

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

'We need to increase our counter by 1

intCount = intCount + 1

'We now call our custom subroutine to add a textbox to the form

AddTextBox("CustomBox" & intCount, "Box #: " & intCount, intX, intY)

'We now increase our location (vertical) by 25 pixels

intY = intY + 25

End Sub

Public Sub AddTextBox(ByVal txtName As String, ByVal txtText As String, ByVal intLocationX As Integer, ByVal intLocationY As Integer)

'The point variable will handle placing our textbox, we

'need to specify a new location for each textbox so we

'don't overlap the previous textbox. Simple mathmatical equation

Dim pntNew As New Point(intLocationX, intLocationY)

'We now create a new instance of the textbox object

Dim txtNewBox As TextBox = New TextBox

'We now can set various properties of the textbox object

txtNewBox.Name = txtName

txtNewBox.Text = txtName

txtNewBox.Location = (pntNew)

'We now add the textbox to the form

Controls.Add(txtNewBox)

'We now add the textbox reference to our Generic List Collection

'Note: ideally you can later go through the list and create another

'object instance of an existing textbox and work with it as you

'please later in the coding. This would take further practice of

'using Generic Lists; I encourage you to try it out and read, read, read!

lstBoxes.Add(txtNewBox)

'For visual sake only, we add the textbox reference name to a list

'box, this just visually proves it does exist and was named.

ListBox1.Items.Add(txtNewBox.Name)

End Sub

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

'We now declare object instances of the textbox

'Notice that we are NOT using the NEW keyword, this

'is because the textboxes already exist and we aren't

'going to be adding it to the form again! We only want

'to work with it.

Dim tbTest, tbTest2 As TextBox

'We check to see if the Generic list contains anything,

'if not then notify the user to add a textbox to the form first.

If lstBoxes.Count <= 0 Then

MessageBox.Show("No boxes added to the form. Please add a textbox first")

Else

'if items in Generic List then access the very first item only

'notice we directly assign the object using the index. Generic

'list's indexes are 0-based; meaning first item is indexed as 0

tbTest = lstBoxes.Item(0)

'Notice here that since we referenced a textbox object we can

'use the properties that are found within the textbox class.

MessageBox.Show("Your first textbox is named: " & tbTest.Name)

'Now we want to check to see if three items exist in the list,

'if so then we can work with the third item. Remember, the list

'index is 0-based, so we want the 2nd index (item # 3)...since

'the first item was indexed as 0.

If lstBoxes.Count >= 3 Then

tbTest2 = lstBoxes.Item(2)

'Notice here that since we referenced a textbox object we can

'use the properties that are found within the textbox class.

MessageBox.Show("Your 3rd textbox text contained is: " & tbTest2.Text)

End If

End If

End Sub

End Class

I hope you find the above helpful in some sort!

Thanks,

James






Re: Visual Basic General retrieve the name of a "NEW" control

Rea Software Engineering

Hi rigel2,

SQL would be a much more preferred method of performing any Database tasks. There are positives and negatives to doing it though. Thorough review them and check to see if they are acceptable.

The checkbox idea would definitely work with SQL; again there are postives and negatives. Most likely would be worth it. I am surprised you couldn't do that with Access database file; I don't use Access with my applications (so I am not familiar with what can and can't be done). I would look a little deeper and ensure that you can't use your checkbox idea with Access. If not, then SQL would probably be a good idea to look into. You will find that VB is much better equipped out of the box to handle SQL (how much better depends on the version of VB you are using...i.e. VB.Net Express, VB.Net Standarrd, etc).

If you further delve into this idea and have further questions, you should start a new thread. Even though it is nearly the same topic, it would get confusing to people answering it and to anyone using it later to find an answer for a similar question. Keep this topic focused on creating controls programmatically and accessing the controls.

I hope my opinion helps you out!

Thanks,

James






Re: Visual Basic General retrieve the name of a "NEW" control

Don008

Hi James,

Pardon me for jumping in. I'm sort in the same boat that Rigel is... but can't exactly follow the same way out. And, I probably have the same extent of VS/VB experience, maybe less.

Your code snippet using generics was extremely helpful to get to a point. However, I'm using checkboxes instead of textboxes.

What I've managed to do, using your code snippet for some direction, is create a button that will create X number of checkboxes, based upon the records in a table. Each checkbox is named for a unique ID for each record in the table. I've created another button that will, for each checked checkbox (in the generic list of checkboxes), add a datarow (consisting of the checkbox name) to a datatable. As the user might change their mind, each time the button is pushed, it clears the datatable and refreshes all the information.

Problem is, this is desired to operate on a more real-time basis, without the need for pressing a refresh button. I need the action of clicking a checkbox to trigger one or more other events (or event handlers). Going through the help files, I may have found a way, using AddHandler, to add an event handler to the checkbox when it's created. My problem with the example is, that the event handler relies on finding the control that launched it - with the control name - and then acting accordingly. Like Rigel, I won't have this name. I will not know the name of any given checkbox that is checked at any given time. Is there a way for the event handler to determine for itself, the source of the click that launched it

In writing my question, a thought has come to mind, that perhaps the event handler could be set to do what the second button currently does - clear everything from the datatable and add everything back based on all the checked boxes... and then perform some other procedures. I'll go with that if I can't find the solution of the click-finding event handler.

Any suggestions

Thanks,

Don





Re: Visual Basic General retrieve the name of a "NEW" control

master_rigel2

I probably could do this with an Access Database, but I don't have access and I'm running the express edition.

Bearing these two things in mind, I'm tied to SQL Server and the Database Designer. Therefore I am grateful that I can use SQL database to fullfill my task. I am learning some more everyday.

Rigel

P.S. May house looks so awesome now! I'm going to put up lots of pics when I get myself a digicam Big Smile!!! Let me know if you want to see.






Re: Visual Basic General retrieve the name of a "NEW" control

Rea Software Engineering

Hi Don,

Your question is actually just a tad off topicˇ­it seems you need to know the controls name to accomplish what you want, but in reality you only need to know the sender of the event.

Since this is a question that many others may encounter, and hopefully my answer will be useful, I have started a new topic thread called, ˇ°How to handle programmatically created checkbox (or any control type) eventsˇ°. Please see that thread for your answer.

Thanks,
James