Dynamic Food Menu in C#
Just curious, and this is a simple question I'm sure anyone here can answer, but I'm curious as to what responses I'll get. A few years back, in an intro. C# class, an assignment I had was to create a dynamic food menu in c#. I constructed the menu and the code does not have any errors when compiled, but it's not finished. Based on what you see, what would you do to complete and finish this?

VulpesRank #55 · 29.2K reputationPosted Apr 21, 2011, 1:26 AM EDT
Your addendum looks OK to me though you can make it slightly more efficient by changing all the 'if's after the first to 'else if's because there's no point examining further conditions once you've found the type of control:
public void ClearControlValues(System.Windows.Forms.Control container)
{
try
{
foreach (Control ctrl in container.Controls)
{
if (ctrl.GetType() == typeof(TextBox))
((TextBox)ctrl).Text = "";
else if (ctrl.GetType() == typeof(ComboBox))
((ComboBox)ctrl).SelectedIndex = -1;
else if (ctrl.GetType() == typeof(CheckBox))
((CheckBox)ctrl).Checked = false;
else if (ctrl.GetType() == typeof(Label))
((Label)ctrl).Text = "";
else if (ctrl.GetType() == typeof(DateTimePicker))
((DateTimePicker)ctrl).Text = "";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Claude Washington5 reputationPosted Apr 20, 2011, 3:03 PM EDT
public void ClearControlValues(System.Windows.Forms.Control container)
{
try
{
foreach (Control ctrl in container.Controls)
{
if (ctrl.GetType() == typeof(TextBox))
((TextBox)ctrl).Text = "";
if (ctrl.GetType() == typeof(ComboBox))
((ComboBox)ctrl).SelectedIndex = -1;
if (ctrl.GetType() == typeof(CheckBox))
((CheckBox)ctrl).Checked = false;
if (ctrl.GetType() == typeof(Label))
((Label)ctrl).Text = "";
if (ctrl.GetType() == typeof(DateTimePicker))
((DateTimePicker)ctrl).Text = "";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Sam HobbsRank #107 · 20.5K reputationPosted Apr 20, 2011, 7:43 PM EDT
Claude Washington5 reputationPosted Apr 20, 2011, 10:09 PM EDT
Sam HobbsRank #107 · 20.5K reputationPosted Apr 21, 2011, 10:53 AM EDT
First note that the form does not have any TextBoxes, CheckBoxes or DateTimePickers. Next note that you don't want to clear the Labels. So for this application you only want to reset the ComboBoxes. Let's assume however that you did want to reset all the controls as in your sample. Then the object-oriented solution would be to:
So for the first item the interface could be:
And the classes could be:
class TextBoxResetable : TextBox, IReset { public void Reset() { Text = ""; } } class ComboBoxResetable : ComboBox, IReset { public void Reset() { SelectedIndex = -1; } } class CheckBoxResetable : CheckBox, IReset { public void Reset() { Checked = false; } } class LabelResetable : Label, IReset { public void Reset() { Text = ""; } } class DateTimePickerResetable : DateTimePicker, IReset { public void Reset() { Text = ""; } }Don't forget to do step 3 above. Then the reset code could be:
This forum's software still mangles source code and I am not sure I know how to post code that is not mangled so the attached zip file shows your code modified in the manner above.Note that this might look like too much work but the same technique could be used for many other things.
Raja KrishnamurthyRank #3,615 · 403 reputationPosted Apr 21, 2011, 12:06 PM EDT
Sam HobbsRank #107 · 20.5K reputationPosted Apr 21, 2011, 1:40 PM EDT
The thing I did not explain in my post here is that if a class is derived from a control, then the derived class will be shown in the toolbox and then controls can be created using the derived class in the designer, eliminating the requirement to modify the designer.cs file. If I were to write an article, I would explain that.
Raja KrishnamurthyRank #3,615 · 403 reputationPosted Apr 22, 2011, 6:45 AM EDT