Results 1 to 11 of 11

Thread: [RESOLVED] Adding Buttons to Form on load

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2011
    Posts
    21

    Resolved [RESOLVED] Adding Buttons to Form on load

    Hello again,

    I'm still working on making my button project and I've came across a new problem.

    I want to create buttons based off the amount of lines in a text file which will be easy if I can get this to work.

    This is my test code before I implement it into the real program:
    Code:
            Try
                Dim btn() As Button
                Dim BtnStartPointX, BtnStartPointy, BtnSizeX, BtnSizeY As Integer
                BtnStartPointX = 0
                BtnStartPointY = 0
                BtnSizeX = 100
                BtnSizeY = 100
    
                For i = 0 To 4
                    btn(i) = New Button()
                    btn(i).Visible = True
                    btn(i).Location = New Point(BtnStartPointX, BtnStartPointY)
                    btn(i).Size = New Size(BtnSizeX, BtnSizeY)
                    btn(i).Top = 50
                    btn(i).Width = 100
                    btn(i).Height = 100
                    btn(i).Left = 50
                    Me.Controls.Add(btn(i))
                    BtnStartPointX += BtnSizeX
                    BtnStartPointY += BtnSizeY
    
                Next
    
            Catch ex As Exception
    
            End Try
    This code will not work for some reason, I've went over and over it and I can't find anything wrong with it.

    There are no errors and 1 warning which states:

    "Warning 1 Variable 'btn' is used before it has been assigned a value. A null reference exception could result at runtime"

    (This Warning points to the btn I made Bold in the code above)

    and this I wouldn't think should effect the making of the buttons.

    This code is located under the Form1_Load Sub, and later the 4 will be changed to my variable that has number of lines in text file.

    Thanks in advance

    -PsycoZL

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: Adding Buttons to Form on load

    The reason it won't work is that you're trying to populate an array that doesn't exist. Here:
    Code:
    Dim btn() As Button
    you are declaring an array variable but you don't actually create an array object. Here:
    Code:
    btn(i) = New Button()
    you are trying to assign a Button to an element in the array that you never created.

    The obvious solution is to create the array, but that's not the best solution. Your code doesn't indicate any need for an array. You populate this array with Buttons and then you never use it again, so what's the point? Just use a Button variable to assign the new Button object to.

    Also, you shouldn't need to position the Buttons. Add a TableLayoutPanel to your form and configure it appropriately. You can then simply add each Button to the TLP and it will handle the layout.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2011
    Posts
    21

    Re: Adding Buttons to Form on load

    Ok, I see what you mean by using arrays in the wrong way I revised my code a little using an object array:
    Code:
            Try
                Dim btn() As Button
                Dim BtnStartPointX, BtnStartPointy, BtnSizeX, BtnSizeY As Integer
                BtnStartPointX = 0
                BtnStartPointY = 0
                BtnSizeX = 100
                BtnSizeY = 100
    
                For i = 0 To 4
                    btn(i) = New Button
                    btn(i).Visible = True
                    btn(i).Location = New Point(BtnStartPointX, BtnStartPointY)
                    btn(i).Size = New Size(BtnSizeX, BtnSizeY)
                    btn(i).Top = 50
                    btn(i).Width = 100
                    btn(i).Height = 100
                    btn(i).Left = 50
                    Me.Controls.Add(btn(i))
                    BtnStartPointX += BtnSizeX
                    BtnStartPointY += BtnSizeY
    
                Next
    
            Catch ex As Exception
    
            End Try
    this revised code won't work same error as the first time.

    I was trying to but together a btn variable set as object but how would I name the button with the i varable if its no longer an array?

    This is what I tried with out an array and using a for loop:

    Code:
            Try
                Dim btn As Button
                Dim BtnStartPointX, BtnStartPointy, BtnSizeX, BtnSizeY As Integer
                BtnStartPointX = 0
                BtnStartPointy = 0
                BtnSizeX = 100
                BtnSizeY = 100
    
                For i = 0 To 4
                    btn(i) = New Button
                    btn(i).Visible = True
                    btn(i).Location = New Point(BtnStartPointX, BtnStartPointy)
                    btn(i).Size = New Size(BtnSizeX, BtnSizeY)
                    btn(i).Top = 50
                    btn(i).Width = 100
                    btn(i).Height = 100
                    btn(i).Left = 50
                    Me.Controls.Add(btn(i))
                    BtnStartPointX += BtnSizeX
                    BtnStartPointy += BtnSizeY
    
                Next
    
            Catch ex As Exception
    
            End Try
    Oh and I'll use your TableLayoutPanel after I get the code working great idea never thought to use it, thanks.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: Adding Buttons to Form on load

    Quote Originally Posted by PsycoZL View Post
    Ok, I see what you mean by using arrays in the wrong way
    Apparently you don't because you're still using the array in exactly the same way. You're still declaring an array variable and then trying to set an element without ever creating an array. I think that you need to do a bit of reading on arrays.

    http://www.startvbdotnet.com/language/arrays.aspx
    http://www.homeandlearn.co.uk/net/vbnet.html#Arrays

    As for the second code snippet, you have replaced the array variable with a Button variable but you are still trying to set an array element inside the loop. 'btn' is just a Button variable so you just assign a Button object to that variable. There's no array anymore so you don't use an index.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2011
    Posts
    21

    Re: Adding Buttons to Form on load

    I'm sorry,

    I know I sound like I have no idea what I'm talking about mostly because I just started doing vb after my teacher asked me to teach his class in my first fourm on here.

    But how would I declare the button name without using an array?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: Adding Buttons to Form on load

    You already know how to declare a variable and assign a value to it because you're already doing it. Look at your other variables. You declare them. You assign a value to them.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2011
    Posts
    21

    Re: Adding Buttons to Form on load

    Ok from what I'm understanding you want this:

    Code:
            Try
                Dim btn As Button
                Dim BtnStartPointX, BtnStartPointy, BtnSizeX, BtnSizeY As Integer
                BtnStartPointX = 0
                BtnStartPointy = 0
                BtnSizeX = 100
                BtnSizeY = 100
    
                For i = 0 To 4
                    btn = New Button
                    btn.Visible = True
                    btn.Location = New Point(BtnStartPointX, BtnStartPointy)
                    btn.Size = New Size(BtnSizeX, BtnSizeY)
                    btn.Top = 50
                    btn.Width = 100
                    btn.Height = 100
                    btn.Left = 50
                    Me.Controls.Add(btn)
                    BtnStartPointX += BtnSizeX
                    BtnStartPointy += BtnSizeY
    
                Next
    
            Catch ex As Exception
    
            End Try
    but my question I was asking last time when I made the mistake of saying "declaring" is how do I tell the loop to make more than 1 button? (Still not sure if I'm asking this right)

    This will make a button and then edit the same button 4 times, how do I specify that I want it to make more than one button?

    Sorry for the confusion, hopfully this helps

    Thanks

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: Adding Buttons to Form on load

    Quote Originally Posted by PsycoZL View Post
    This will make a button and then edit the same button 4 times
    No it won't. This code:
    Code:
    btn = New Button
    creates a new Button object. That code is inside the loop so a new Button object is created each iteration of the loop. If you had that line outside of the loop then it would only be executed once, so you'd only have one Button object, so you'd have a problem.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2011
    Posts
    21

    Re: Adding Buttons to Form on load

    Hmm... thats weird because only one button is showing up I'll do a few quick tests to see if there in the same location.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: Adding Buttons to Form on load

    Quote Originally Posted by PsycoZL View Post
    Hmm... thats weird because only one button is showing up I'll do a few quick tests to see if there in the same location.
    They would be. You're setting the Location correctly first but then you override that by setting the Top and Left to the same value every time. Like I said, you should use a TLP and let that take care of layout.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Mar 2011
    Posts
    21

    Re: Adding Buttons to Form on load

    Oh I see I was trying to anchor the buttons using The top and left values, and with your TableLayoutPanel idea its not necessary because it does it for you.

    Ok thank you so much for putting up with me throughout the last few fourms, you've taught me alot about the language, not only in my fourms but some of the others I've looked up and seen you in.

    Anyways thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width