user2960452
user2960452

Reputation: 41

Adding OnClick Events to Buttons

I am trying to create a POS system (very basic one) in C# and I have products in a MYSQL database that I want to be pulled and then displayed in the system.

I can generate the buttons with the names no problem, the problem I have is that I want to implement a way for the user to click a button and it adds it into a list box - this is easy enough to do if I know the amount of products in the database but I won't so I need the onclick handler to be programmatically generated with the buttons

here is my button generation code:

int i = 1;

while (sqlReader.Read())
{
    //Create label
    var button = new Button {Text = String.Format(sqlReader.GetString("productName"), i)};
    //Position label on screen
    button.Left = 110;
    button.Top = (i + 1)*30;
    //Add controls to form
    Controls.Add(button);
    i++;
}

I realise not all of it is there but that's the while loop I am using to generate the buttons so I am wondering if the handler would go in there?

Upvotes: 0

Views: 3528

Answers (4)

Bennett
Bennett

Reputation: 95

To see good examples of adding event handlers to your control take a look at the .Designer.cs file for your form. Just about every line in there that has a += operator is assigning a new event handler to a control.

I spend a lot of time in .Designer.cs deleting unneeded event handler assignments when I delete the (often blank) event handler functions and rebuild the solution. The errors are always on the line of the handler assignments.

Upvotes: 0

John Saunders
John Saunders

Reputation: 161831

If you need access to product information in the Click handler, you can do as follows:

int i = 1;

while (sqlReader.Read())
{
    //Create label
    var button = new Button {Text = String.Format(sqlReader.GetString("productName"), i)};
    //Position label on screen
    button.Left = 110;
    button.Top = (i + 1)*30;

    // Get product data
    var prodData1 = sqlReader["prodData1"];
    var prodData2 = sqlReader["prodData2"];
    // etc.

    button.Click += (sender,e)=>{
        // In here, you can access prodData1 and prodData2
    };
    //Add controls to form
    Controls.Add(button);
    i++;
}

Upvotes: 2

B L
B L

Reputation: 1602

Your approach is a little unorthodox, but you're almost there. You can assign an event handler the same way you assign the Text property when making a new button. If the button clicks are all going to do the same thing, you can get away with saying:

var button = new Button {Text = String.Format(sqlReader.GetString("productName"), i)};
button.Click += button_Clicked

button_Clicked would be a function defined somewhere in your code:

 private void button_Click(object sender, EventArgs e)
 {
    // add to listbox here
 }

Cast the sender argument as a Button to determine which one invoked the click, if needed, and do whatever you need to do.

Upvotes: 0

Jon Barker
Jon Barker

Reputation: 1829

Yes, whenever you create a button, you have to add an eventhandler for it:

button.Click += Button_ClickedEvent;


private void Button_ClickedEvent(object sender, EventArgs e)
{
  //Use the sender object to work out which button was clicked.
}

Upvotes: 1

Related Questions