I have Form1.cs which has two buttons say “ADD” and “EDIT”.
Clicking “ADD” shows dialog Form2.cs.
Form2 has a TextBox and a ComboBox. Say we enter value “A” in textbox and select “A” from ComboBox.
Then close Form2.
Then when EDIT button is clicked on Form1, Form2 should show up with “A” in textbox and “A” selected in ComboBox.
This is a simple explanation. The real form I am using has around 10-12 different controls including combobox, checkbox, textbox etc.
My main doubt is where and how do we save these control values.
Is there a specific approach to this type of DialogBoxes that I am missing?
Create class, that would store values that you want to pass (let’s call it
Foo).Form2should then have a property. In the setter of the property, set controls:On Edit button, set property like this:
Then put a Save button on
Form2, that would save values back from controls to this object.For every control I would have a field in
Fooclass, eg.stringfor textboxes,boolfor checkboxes andenumorintfor comboboxes (where integer value would equal selected index).Alternatively, you could use
Dictionaryclass instead and have key and value pair for every control.You can also have some
enum, if your form looks or behaves differently in New and Edit mode.