Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB Buttons 1

Status
Not open for further replies.

dcomit

Technical User
Jun 20, 2001
115
GB
I’m prototyping a touch screen application which does database lookups. I need to create a number of buttons, dependent upon the number of items returned from the database, to allow the user to select accordingly. I’d also like a method of arranging the buttons appropriately spaced on the screen.

Is this possible or is there a better way to achieve this?
 
Yes, it is possible. A better way to achieve what? All you said was you wanted one button per item. Kind of need a better idea what your final goal is to say if there might be a better way of doing it.

-I hate Microsoft!
-Forever and always forward.
 
Your form or other container control has a .Controls collection of which you can add and remove controls to. So to dynamically add controls to that collection, all you would need to do would be to iterate through a DataTable for example, create a new button, and then add it to the collection. For example

Code:
For Each dr As DataRow In MyDataTable.Rows
  Dim btn As New Button
  MyPanel.Controls.Add(btn)
  btn.Text = dr.Item("Description")
  btn.Name = dr.Item("Name")
  btn.Height = 50
  btn.Width = 50
  etc...
Next

Make sense? To space them evenly will involve a little more work. The method you choose will depend on how you want your layout and will use some math. Lets say you want rows of three buttons. One way to space them after you have created them with the above method would be as follows:

Code:
Dim Counter As Integer = 0
For i As Integer = 0 To MyPanel.Controls.Count - 1
  If i = 0 Then
    MyPanel.Controls(i).Left = 6
    MyPanel.Controls(i).Top = 6
  ElseIf Counter Mod 3 <> 0
    MyPanel.Controls(i).Left = MyPanel.Controls(i-1).Left + MyPanel.Controls(i-1).Width + 6
    MyPanel.Controls(i).Top = MyPanel.Controls(i-1).Top
  ElseIf Counter Mod 3 = 0
    MyPanel.Controls(i).Left = 6
    MyPanel.Controls(i).Top = MyPanel.Controls(i-1).Top + MyPanel.Controls(i-1).Height + 6
  End If
  Counter += 1
Next
 
That looks to be the business. I'll try it over the weekend.

Many thanks,
Dave
 
I’ve got as far as creating the buttons, now I need to set a variable when I click on them. This is normally handled by a Button_Click subroutine. How do I accomplish this?
 
I’ve checked out AddHandler but I’m confused.

When I put in the AddHandler statement in my code, it only sees Button1 and not the button I’ve just created. I’ve tried referencing Btn.Name but I get errors.
Code:
    Public Sub Test()
        Dim Ctr As Integer
        Ctr = 64
        Dim i
        For i = 1 To 2
            Dim Btn As New Button()
            Me.Controls.Add(Btn)
            Btn.Text = "Text " & i
            Btn.Name = "ButtonNew" & i
            Btn.Height = 50
            Btn.Width = 50
            Btn.Location = New System.Drawing.Point(Ctr, 16)
            Ctr = Ctr + 64
            AddHandler Btn.Click, Btn.name_click

        Next i

    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Test()
    End Sub
 
Here's the situation. You are going to have a bunch of buttons that perform related tasks and are created dynamically. Therefore, you will use a common .Click event handler for all of the buttons. Once inside the Click event handler, the way to tell which button was clicked is to investigate the sender Object. sender will be equal to whichever button was clicked. So here is what you need to do:

1. Create a generic Button .Click event handler (don't put a Handles statement on it). Something like Private Sub DynamicButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

2. When you create your buttons, reference that generic event: AddHandler btn.Click, AddressOf YourGenericClickEventHandler

 
Eureka! It works - you are a star indeed sir, many thanks for your help. I only started with VB.net a couple of weeks ago as I couldn't do what I wanted using HTML, PHP and JavaScript.

Code:
    Public Sub Test()
        Dim Ctr As Integer
        Ctr = 64
        Dim i
        For i = 1 To 3
            Dim Btn As New Button()
            Me.Controls.Add(Btn)
            Btn.Text = "Text " & i
            Btn.Name = "ButtonNew" & i
            Btn.Height = 50
            Btn.Width = 50
            Btn.Location = New System.Drawing.Point(Ctr, 16)
            Ctr = Ctr + 64
            AddHandler Btn.Click, AddressOf DynamicButton_Click
        Next i
    End Sub
    Private Sub DynamicButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MsgBox(sender.text)
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Test()
    End Sub

Best regards,
Dave
 

Consider these changes:
Code:
    Public Sub Test()
        Dim Ctr As Integer[blue] = 64[/blue]
        [green]'Ctr = 64[/green]
        [green]'Dim i[/green]
        For i [blue]As Integer[/blue] = 1 To 3
    ....
Take advantage of VB.NET easy syntax :)


Have fun.

---- Andy
 
Andrzejek, why only do half the job? [wink]

Code:
	Public Sub Test()

		For i As Integer = 1 To 3
			With New Button
				.Text = "Text " + i.ToString
				.Name = "ButtonNew" + i.ToString
				.Size = New Size(50, 50)
				.Location = New Point(i * 64, 16)
				AddHandler .Click, AddressOf DynamicButton_Click
				.Parent = Me
			End With
		Next

	End Sub

[vampire][bat]
 

Me like it, me like it :)

I've always liked eliminating any line(s) of code that is not necessary.

Have fun.

---- Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top