Skip to main content
Added comment about paging
Source Link
Lars-Erik
  • 1.4k
  • 9
  • 12

All good answers. I'd like to add my two cents to @deepee1's answer. If you keep the FoodItem class he provides (with a constructor), you can create a list of food items and then create everything else with completely DRY code. You can even easily move the definitions out of the code. Not to mention creating n different UI's with the same "logic".

Here's what I'd do: (Forgive any mistakes, long time since WinForms :) )

private List<FoodItem> foods = new[] { new FoodItem("Taco", 1.0), new FoodItem("Burrito", 2.0), new FoodItem("Fajita", 1.5) }; private const int buttonMargin = 5; protected void Form_Load(...) { CreateButtons(); } private void CreateButtons() { for(var i = 0; i<foods.Length; i++) AddButton(foodfoods[i], i); } private void AddButton(FoodItem food, int index) { var btn = new Button(); btn.Id = "Button_" + i; btn.Tag = food; btn.Click += FoodClicked; btn.Top = btn.Height*index + buttonMargin*index; // Whatever initialization code you have Controls.Add(btn); } private void FoodClicked(object sender, EventArgs e) { var button = (Button)sender; var food = (food)button.Tag; // Do whatever with the food.. } 

I just re-read some comments and realize you need paging too. Add a private int page, and a private int pagesize, then add the multiplication of those to foods[i] in CreateButtons, and swap foods.Length with pagesize. You can have as big an array as you want then.

All good answers. I'd like to add my two cents to @deepee1's answer. If you keep the FoodItem class he provides (with a constructor), you can create a list of food items and then create everything else with completely DRY code. You can even easily move the definitions out of the code. Not to mention creating n different UI's with the same "logic".

Here's what I'd do: (Forgive any mistakes, long time since WinForms :) )

private List<FoodItem> foods = new[] { new FoodItem("Taco", 1.0), new FoodItem("Burrito", 2.0), new FoodItem("Fajita", 1.5) }; private const int buttonMargin = 5; protected void Form_Load(...) { CreateButtons(); } private void CreateButtons() { for(var i = 0; i<foods.Length; i++) AddButton(food, i); } private void AddButton(FoodItem food, int index) { var btn = new Button(); btn.Id = "Button_" + i; btn.Tag = food; btn.Click += FoodClicked; btn.Top = btn.Height*index + buttonMargin*index; // Whatever initialization code you have Controls.Add(btn); } private void FoodClicked(object sender, EventArgs e) { var button = (Button)sender; var food = (food)button.Tag; // Do whatever with the food.. } 

All good answers. I'd like to add my two cents to @deepee1's answer. If you keep the FoodItem class he provides (with a constructor), you can create a list of food items and then create everything else with completely DRY code. You can even easily move the definitions out of the code. Not to mention creating n different UI's with the same "logic".

Here's what I'd do: (Forgive any mistakes, long time since WinForms :) )

private List<FoodItem> foods = new[] { new FoodItem("Taco", 1.0), new FoodItem("Burrito", 2.0), new FoodItem("Fajita", 1.5) }; private const int buttonMargin = 5; protected void Form_Load(...) { CreateButtons(); } private void CreateButtons() { for(var i = 0; i<foods.Length; i++) AddButton(foods[i], i); } private void AddButton(FoodItem food, int index) { var btn = new Button(); btn.Id = "Button_" + i; btn.Tag = food; btn.Click += FoodClicked; btn.Top = btn.Height*index + buttonMargin*index; // Whatever initialization code you have Controls.Add(btn); } private void FoodClicked(object sender, EventArgs e) { var button = (Button)sender; var food = (food)button.Tag; // Do whatever with the food.. } 

I just re-read some comments and realize you need paging too. Add a private int page, and a private int pagesize, then add the multiplication of those to foods[i] in CreateButtons, and swap foods.Length with pagesize. You can have as big an array as you want then.

Source Link
Lars-Erik
  • 1.4k
  • 9
  • 12

All good answers. I'd like to add my two cents to @deepee1's answer. If you keep the FoodItem class he provides (with a constructor), you can create a list of food items and then create everything else with completely DRY code. You can even easily move the definitions out of the code. Not to mention creating n different UI's with the same "logic".

Here's what I'd do: (Forgive any mistakes, long time since WinForms :) )

private List<FoodItem> foods = new[] { new FoodItem("Taco", 1.0), new FoodItem("Burrito", 2.0), new FoodItem("Fajita", 1.5) }; private const int buttonMargin = 5; protected void Form_Load(...) { CreateButtons(); } private void CreateButtons() { for(var i = 0; i<foods.Length; i++) AddButton(food, i); } private void AddButton(FoodItem food, int index) { var btn = new Button(); btn.Id = "Button_" + i; btn.Tag = food; btn.Click += FoodClicked; btn.Top = btn.Height*index + buttonMargin*index; // Whatever initialization code you have Controls.Add(btn); } private void FoodClicked(object sender, EventArgs e) { var button = (Button)sender; var food = (food)button.Tag; // Do whatever with the food.. }