Hi there ! I hope you guy who like Badminton also can help me in Visual Basic. I am a beginner in VB .NET and have a problem. My problem is : I want to create 4 buttons (or other control), but all the 4 must be in an array, so the names must be like these : cntl(0), cntl(1), cntl(2) and cntl(3). In VB6, I have no problem, because when I copy and paste the button (in design form), I will be askes, create in array or not. But in VB .NET, no question like that. When I change the name of the control in its property to cntl(0) or button(1) enz, I got an error message, something like 'invalid name'. So..... how to make the 4 buttons in an array ? Please help me guys !!!!! FYI : I use VB .NET Professional version 2003.
i personally hate desining object arrays on those forms. Why not just generate the buttons at runtime?
Because I am new in VB .NET so I don't know how. BigRedLemon, will you let me know how to generate the buttons at runtime ? Thx.
Is the complier on your local computer or on a server's computer? was is the file extension of your saved program?
Wauhhh...... Let's make it simple Brl. I am new in VB .NET and still learning. I create a new project in VB .NET. I have a TextBox for enter a sentence that consist of max. 12 words and a button to get it starts. The program should create (a) button(s) as many as the words in the sentence. If the sentence consist of 5 words, so I got 5 buttons. If the sentence consist of 10 words, so I got 10 buttons. Just that Brl.
different code is used depending on where the code is being compiled. The gist of it would be something like: Code: dim btnMyButtons(2) as array For n = 0 to 2 dim btnMyButtons(n) as new System.Windows.Forms.Button() btnMyButtons(n).property1 = some value btnMyButtons(n).property2 = some value Next n The above code will not work, but you can reformat it with the right calls to make it work. Btw, i thought all buttons had to be declared as btnSomeName as standard coding procedure... its what microsoft recommends to make the code readable
Brl, I think we must write like this : dim btnMyButtons(2) as array For n = 0 to 1 Not --> for n = 0 to 2 (Thanks BadRad) dim btnMyButtons(n) as new System.Windows.Forms.Button() btnMyButtons(n).property1 = some value btnMyButtons(n).property2 = some value Next n But I have red an article that say : VB .NET doesn't support controls array. Does MyButtons(n) work ? Hier you can read the article.
iirc array(2) makes an array or (0), (1), and (2).... I only have a java complier on this computer so i have no way of finding out for sure.... You can always declare a vector and not have to worry about array sizes.
I stick to VB .NET I don't like C++, if I have to choose between C++ and C#. I will choose C#. Anyway....... my problem is in VB .NET.
vb6 does allow the upper boundary (ie array(2) - {0,1,2)), but depending on which version of vb.net it might not (certain beta releases which I played with did not allow it (array(2) - {0,1) 2 was not allowed and returned "IndexOutOfRangeException". I think the current version fixed it to be consistent with vb6.
Give you a short code for this. I suppose you have one TextBox(named txtSentence) and one Button(named Button1) in your main form. Eric ___________________________________________-- Private btns() As Button Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim i As Integer Dim counter As Integer = 0 Dim word() As String = txtSentence.Text.Split(" "c) For i = 0 To word.Length - 1 If word(i).Trim <> "" Then ReDim Preserve btns(counter) btns(counter) = New Button() btns(counter).Text = word(i) btns(counter).Top = 200 btns(counter).Width = 50 btns(counter).Height = 20 btns(counter).Left = 50 + (counter * 70) Me.Controls.Add(btns(counter)) AddHandler btns(counter).Click, AddressOf ResponseButtonClick counter += 1 End If Next End Sub Private Sub ResponseButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs) MessageBox.Show(sender.text) End Sub
The code from Gamebird does work. The control array that I said before is actually a little confuse. If you write the code like Gamebird's, it works. But if you try to change the control's name from myButton to myButton(1), than you get error. I don't know why ? Gamebird, Your statement : For i = 0 To word.Length - 1 Can you tell me what the different between yours and this : For i = 0 To ubound(word) Thanks guys.
VB does has control array. Whenever you make the identical name for 2 controls of same type, VB's IDE will pop up a message for making these 2 controls as array. The benifit for the control array is you can only implement one sub for handlinig the event of these controls, for example, Private Sub Command1_Click(Index as Integer) Select Case Index Case 0 Msgbox "Button 0 was clicked" Case 1 Msgbox "Button 1 was clicked" End Select End Sub .NET don't support the Control array at design time. You need to use other way to get this approach. For example, you have 2 button controls on your form, named cmdButton_1 and cmdButton_2. The VB.NET code below can do the same thing like VB does. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load AddHandler cmdButton_1.Click, AddressOf ProcessButtonArrayClick AddHandler cmdButton_2.Click, AddressOf ProcessButtonArrayClick End Sub Private Sub ProcessButtonArrayClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim tmpStr() As String = sender.name.split("_"c) Select Case CInt(tmpStr(1)) Case 1 MsgBox("Button 1 was clicked") Case 2 MsgBox("Button 2 was clicked") End Select End Sub About your question, UBound(Array) is the dialect only for VB. Array.Length is the common syntax in all .NET language. UBound still supported in VB.NET, but it is better if you can write more common code for running in .NET CLR. UBound(Array) is equal to Array.Length - 1.
but the problem with the latter method is that you'd actually have to create all those buttons. The difference between the suggested one that supposedly works and the one i said that definetly doesn't work is this: me: creating an array of buttons other: creating an array that contains buttons I guess the only functional difference is that in my method, rediming a larger array would create more buttons, whereas the latter method won't. The latter method also can hold non-buttons as well... i think anyway. Haven't touched VB in years...