Thanks for visiting us!

Badminton Central is a free community for fans of badminton! If you find anything useful here please consider registering to see more content and get involved with our great community users, it takes less than 15 seconds! Everybody is welcome here.

Click here for a FREE account!

VIsual Basic

Discussion in 'Chit-Chat' started by jkusmanto, Jan 19, 2004.

  1. jkusmanto

    jkusmanto Regular Member

    Joined:
    Jul 9, 2002
    Messages:
    736
    Likes Received:
    0
    Occupation:
    Computer Analyst
    Location:
    Belgium
    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.
     
  2. bigredlemon

    bigredlemon Regular Member

    Joined:
    Sep 28, 2002
    Messages:
    2,096
    Likes Received:
    0
    Location:
    T.O.
    i personally hate desining object arrays on those forms.

    Why not just generate the buttons at runtime?
     
  3. jkusmanto

    jkusmanto Regular Member

    Joined:
    Jul 9, 2002
    Messages:
    736
    Likes Received:
    0
    Occupation:
    Computer Analyst
    Location:
    Belgium
    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.
     
  4. bigredlemon

    bigredlemon Regular Member

    Joined:
    Sep 28, 2002
    Messages:
    2,096
    Likes Received:
    0
    Location:
    T.O.
    havent used MS compliers in awhile... is it server side or client side?
     
  5. jkusmanto

    jkusmanto Regular Member

    Joined:
    Jul 9, 2002
    Messages:
    736
    Likes Received:
    0
    Occupation:
    Computer Analyst
    Location:
    Belgium
    Actually can be in server and client side.
    Can you explain in general ?
     
  6. bigredlemon

    bigredlemon Regular Member

    Joined:
    Sep 28, 2002
    Messages:
    2,096
    Likes Received:
    0
    Location:
    T.O.
    Is the complier on your local computer or on a server's computer?
    was is the file extension of your saved program?
     
  7. jkusmanto

    jkusmanto Regular Member

    Joined:
    Jul 9, 2002
    Messages:
    736
    Likes Received:
    0
    Occupation:
    Computer Analyst
    Location:
    Belgium
    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.
     
  8. bigredlemon

    bigredlemon Regular Member

    Joined:
    Sep 28, 2002
    Messages:
    2,096
    Likes Received:
    0
    Location:
    T.O.
    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
     
  9. badrad

    badrad Regular Member

    Joined:
    Apr 25, 2002
    Messages:
    1,650
    Likes Received:
    2
    Occupation:
    currently unemployed
    Location:
    Surrey, Canada
    watch your FOR loop as it will exceed the array size.
     
  10. jkusmanto

    jkusmanto Regular Member

    Joined:
    Jul 9, 2002
    Messages:
    736
    Likes Received:
    0
    Occupation:
    Computer Analyst
    Location:
    Belgium
    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.
     
  11. bigredlemon

    bigredlemon Regular Member

    Joined:
    Sep 28, 2002
    Messages:
    2,096
    Likes Received:
    0
    Location:
    T.O.
    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.
     
  12. bigredlemon

    bigredlemon Regular Member

    Joined:
    Sep 28, 2002
    Messages:
    2,096
    Likes Received:
    0
    Location:
    T.O.
    it doesn't even support control arrays? :eek:

    Have you considered C++? :p
     
  13. bluejeff

    bluejeff Regular Member

    Joined:
    Jan 16, 2003
    Messages:
    3,949
    Likes Received:
    4
    Occupation:
    Developer
    Location:
    TW
    Exactly, Microsoft is evil. C++ is definetely the way to go (with use of Linux or UNIX)
     
  14. jkusmanto

    jkusmanto Regular Member

    Joined:
    Jul 9, 2002
    Messages:
    736
    Likes Received:
    0
    Occupation:
    Computer Analyst
    Location:
    Belgium
    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.;)
     
  15. badrad

    badrad Regular Member

    Joined:
    Apr 25, 2002
    Messages:
    1,650
    Likes Received:
    2
    Occupation:
    currently unemployed
    Location:
    Surrey, Canada
    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.
     
  16. Gamebird

    Gamebird Regular Member

    Joined:
    Jul 9, 2003
    Messages:
    8
    Likes Received:
    0
    Location:
    Ontario, Canada
    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
     
  17. bigredlemon

    bigredlemon Regular Member

    Joined:
    Sep 28, 2002
    Messages:
    2,096
    Likes Received:
    0
    Location:
    T.O.
    then you have the same problem as before... a control array, which is not supported by his complier
     
  18. jkusmanto

    jkusmanto Regular Member

    Joined:
    Jul 9, 2002
    Messages:
    736
    Likes Received:
    0
    Occupation:
    Computer Analyst
    Location:
    Belgium
    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.
     
  19. Gamebird

    Gamebird Regular Member

    Joined:
    Jul 9, 2003
    Messages:
    8
    Likes Received:
    0
    Location:
    Ontario, Canada
    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.
     
  20. bigredlemon

    bigredlemon Regular Member

    Joined:
    Sep 28, 2002
    Messages:
    2,096
    Likes Received:
    0
    Location:
    T.O.
    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...
     

Share This Page