|
I am trying to make a epos for a bar as a uni project have spoke to my tutor but he’s of no help and tells me to look it up on the net so here i am
Situation
On the First Sales screen i have 24 buttons (speedbar) the names will come from a db but if there are not 24 items in the database it stops due to it being out of range
This is my current code
<br />
<br />
Dim speedbar As New Collection<br />
Dim sql As String<br />
Dim ds As DataSet = New DataSet<br />
Dim cmd As OleDb.OleDbCommand = OleDbConnection1.CreateCommand<br />
cmd.CommandType = CommandType.Text<br />
sql = "select * from Drinks"<br />
cmd.CommandText = sql<br />
<br />
Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter<br />
da.SelectCommand = cmd<br />
da.Fill(ds, "Drinks")<br />
<br />
Dim dc As DataColumn, dr As DataRow<br />
<br />
<br />
<br />
For Each dr In ds.Tables("Drinks").Rows<br />
speedbar.Add(dr.Item("Name"))<br />
Next<br />
As you can see a items for the speed bar are stored in a collection what i cant do (where i need your help please) is make the buttons dynamical or add the name to some and make the rest invisible
Thanks
aj
|
|
|
|
|
You create button the same way you create an instance of any other class, with New. You then set a bunch of properties on the button for size, location, Text, whatever, ... Then you add the button to the container form's Controls collection.
Dim newButton As New Button
newButton.Location = New Point(x, y)
newButton.Size = New Size(50, 75)
newButton.Text = someText
Me.Controls.Add(newButton)
Next, you have to wire up the button's Click event so you can get notified when the user clicks on it. That you do with the AddHandler statement:
AddHandler newButton.Click, AddressOf myButtonClickHandler
Of course, myButtonClickHandler has to have the signature of the appropriate event handler:
Private Sub myButtonClickHandler(ByVal sender As System.Object, ByVal e As System.EventArgs)
... code to handle the button click goes here
End Sub
Multiple buttons can be wired up to the same event handler.
|
|
|
|
|
how do i get it to make the right amount of buttons and name them all and put them in the right place
modified on Friday, May 9, 2008 2:16 PM
|
|
|
|
|
jacko873 wrote: how do i get it to make the right amount of buttons
Iterate over the returned rows in your datatable. One button for each row.
jacko873 wrote: put them in the right place
Simple math. All you have to do is calculate an offset from a fixed point multiplied by the count of buttons you've put down already.
|
|
|
|
|
Thank you Got the buttons working the names are right and the positions are great but I do have one last think to ask
How do I create an event for each button?
What I want the buttons to do is when they are clicked and an item to a list box and add the price to a variable for later
|
|
|
|
|
I already told you how to wire up the button's Click event.
|
|
|
|
|
I get that I can create event but each button has the same event
I what each event to be different dependant on the text of the button
|
|
|
|
|
Like was said before:
AddHandler newButton.Click, AddressOf myButtonClickHandler
but change myButtonClickHandler to the name of the method you want to be called onClick.
Edit:
pseudo code:
if button.text == string1 then
AddHandler newButton.Click, AddressOf myButtonClickHandler1
else if button.text == string2 then
AddHandler newButton.Click, AddressOf myButtonClickHandler2
etc etc
At university studying Software Engineering - if i say this line to girls i find they won't talk to me
Dan
|
|
|
|
|
Then you have to cast the Sender object back to a button. You can then use it just like it was dropped on the form in the designer:
Dim b As Button = DirectCast(sender, Button)
|
|
|
|