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?
Raja KrishnamurthyPosted Apr 22, 2011, 10:45 AM
Sam HobbsPosted Apr 21, 2011, 5:40 PM
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 KrishnamurthyPosted Apr 21, 2011, 4:06 PM
Sam HobbsPosted Apr 21, 2011, 2:53 PM
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:
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.
VulpesPosted Apr 21, 2011, 5:26 AM
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 WashingtonPosted Apr 21, 2011, 2:09 AM
Sam HobbsPosted Apr 20, 2011, 11:43 PM
Claude WashingtonPosted Apr 20, 2011, 7:03 PM
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());
}
}