dushyantp
dushyantp

Reputation: 4718

Creating Edit dialog box in Windows Forms C#

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?

Upvotes: 0

Views: 5610

Answers (5)

Tschareck
Tschareck

Reputation: 4239

Create class, that would store values that you want to pass (let's call it Foo).

Form2 should then have a property. In the setter of the property, set controls:

public partial class Form2 : Form
{
    private Foo _bar;
    public Foo Bar
    {
        set
        {
            _bar = value;
            //set your controls here
        }
    }

On Edit button, set property like this:

Form2 form2 = new Form2();
form2.Bar = bar; //bar contains values to edit

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 Foo class, eg. string for textboxes, bool for checkboxes and enum or int for comboboxes (where integer value would equal selected index).
Alternatively, you could use Dictionary class 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.

Upvotes: 1

Bruno Charters
Bruno Charters

Reputation: 494

Do you want to just load the last value user entered there?

For instance he writes "text" on the textbox and chooses "A" combobox it should be pre-selected next time you open it?

Edit: Then instead of closing it using Form.Close make it so that it hides. Form1.Hide. Next time it opens values are still saved. Unless application has been closed. In the other hand, users might click on the close button in the windows form. You can either make it "unclickable" throught proprieties or just configure it using events i think.

Upvotes: 1

jrb
jrb

Reputation: 1728

public Form2(string form1Textbox)
{
    InitializeComponent();
    form2Textbox.Text = form1Textbox;
}

Upvotes: 0

Mr.Pe
Mr.Pe

Reputation: 739

Your Dialog Form should have a field containing the properties/fields you want, a copy a business object for example. Then you pass it or initialize it in your dialog constructor or Load, depending the behavior you want. From there you can create / initialize your controls.

If you want a built in system you may wanna take a look to the PropertyGrid (which you could embedded in a dialog (to fit your question))

Upvotes: 1

Mitja Bonca
Mitja Bonca

Reputation: 4546

Create a method on Form2, where you will set values into textBox and select an item in comboBox. Call this method just after instantiating form2 and before showing it. Example:

    public Form2()
    {
        InitializeComponent();
        comboBox1.Items.AddRange(new string[] { "a", "b", "c" });//fill comboBox your way on a loading time
    }

    public void UpdatingControls(string a, string b)
    {
        textBox1.Text = a;
        comboBox1.SelectedText = b;
    }
    //on form2;
    Form1 f2;
    private void OpenForm2Button_Click(object sender, EventArgs e)
    {
        f2 = new Form2();
        f2.UpdatingControls("a", "b"); //a will go into textBox, b will be choosen in comboBox
    }

Upvotes: 0

Related Questions