0

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?

4
  • I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not". Commented Mar 6, 2014 at 21:30
  • Is this a winforms application? Commented Mar 6, 2014 at 21:31
  • Yes it is a windows form Commented Mar 6, 2014 at 21:33
  • It's important to add the technology tag to the tags list. That way, you won't get WPF developers giving you the wrong answers. Commented Mar 6, 2014 at 21:34

4 Answers 4

2

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++;
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.
}

Comments

0

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.

5 Comments

If I wanted to add information regarding that product how could I pass an Identifier. The system is based off of numerous products but I would never know the exact count so if you pressed button X then the system would need to add product X to the listbox I'm thinking something along the lines of a function that adds product information to the listbox such as private void add(String product){ listbox1.add(product); } But im not sure how to sent the product name via a button click
The idea is that once the button is pressed (button information generated from the database) it inserts that into the listbox
You can use the Button.Tag property to store associated information with the button. You can store any object you want in there, and use that in the button click handler to add the appropriate item to your list box.
@Mark As I've been working on projects making extensive use of this approach, I would not recommend this way, because it gets messy pretty fast...
@MisterHenson Good point, I can see how it would, since Tag is just object - remembering/working out what was dumped in there down the road would be "interesting"!
0

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.

1 Comment

-1: you should never edit the .Designer.cs files. They belong to the designer. Use the designer UI to remove handlers which were added by the designer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.