-
May 3rd, 2011, 07:27 PM
#1
Thread Starter
Junior Member
[RESOLVED] Adding Buttons to Form on load
-
May 3rd, 2011, 07:41 PM
#2
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.
-
May 3rd, 2011, 08:08 PM
#3
Thread Starter
Junior Member
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.
-
May 3rd, 2011, 08:46 PM
#4
Re: Adding Buttons to Form on load
Originally Posted by PsycoZL
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.
-
May 3rd, 2011, 09:03 PM
#5
Thread Starter
Junior Member
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?
-
May 3rd, 2011, 09:06 PM
#6
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.
-
May 3rd, 2011, 10:02 PM
#7
Thread Starter
Junior Member
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
-
May 3rd, 2011, 10:11 PM
#8
Re: Adding Buttons to Form on load
Originally Posted by PsycoZL
This will make a button and then edit the same button 4 times
No it won't. This code: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.
-
May 3rd, 2011, 10:16 PM
#9
Thread Starter
Junior Member
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.
-
May 3rd, 2011, 10:21 PM
#10
Re: Adding Buttons to Form on load
Originally Posted by PsycoZL
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.
-
May 3rd, 2011, 10:28 PM
#11
Thread Starter
Junior Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|