Alexander R.
Alexander R.

Reputation: 1756

Winforms best practice for Adding/Editing forms

I studying developing under .NET C# WINFORMS

I have form of adding teacher to DB.

Now I need to make form for editing an teacher.

It's normal to create different form for this purpose? Because it have the same controls, so maybe just call the same form of adding teacher, but load data to controls, and change button from ADD to SAVE?

In web developement (php) I simetimes use the same form for editing/adding data, because it's not too hard to handle this behavior. But I am not sure that this practice is good form windows programming too.

Here the form of adding. I am afraid that it will be hard to maintenance this code later, if I reuse this form for editing too.

Tnx.

This form

Upvotes: 1

Views: 1321

Answers (5)

Javier Salas
Javier Salas

Reputation: 1188

Another constructor could work, in that way you could know which controls to enable or disabled depending on your needs, that's the easiest way and you will reuse the same form/code.

Upvotes: 0

Hos
Hos

Reputation: 11

 Reproduktor rep;
    public Edit(Reproduktor rep)
    {
        InitializeComponent();
        this.rep = rep;
        comboBox1.SelectedIndex = comboBox1.FindStringExact(rep.typSoustavy);
        if (comboBox1.SelectedIndex == 0)
        {
            txtFl.Text = (rep as Reproduktor).fl.ToString(); //+fr
        }
        else if(comboBox1.SelectedIndex ==1)
        {
            txtSub.text blablabla 
        }
        else if(comboBox1 ==2)
        { }
        textBox1.Text = rep.vyrobce;
        textBox2.Text = rep.nazev;
        //cena.tostring
        comboBox1.SelectedIndex = comboBox1.FindStringExact(rep.bluethoot);
    }

    private void buttonStorno_Click(object sender, EventArgs e)
    {
        DialogResult = DialogResult.Cancel;
        this.Close();
    }

Upvotes: 1

Imp0ssible
Imp0ssible

Reputation: 49

Is this form, the main form? If no, you can call different constructors for (add, edit,...) of this form

Upvotes: 2

Fredrik Mörk
Fredrik Mörk

Reputation: 158369

I would typically approach this by using the same form. For clarity, I would not relabel the buttons, but rather use a different caption and/or other clear headline indicating whether the user is adding or updating.

Upvotes: 1

Rémi
Rémi

Reputation: 3967

imo yeah you should reuse the same form... less code to maintaint in long term

edit : as imp0ssible said this isn't possible if it's the main form

just make 2 constructor so you know if you are adding or editing a teacher

Upvotes: 2

Related Questions