1

I am new to c# programming and u might feel its very simple ... I am using menu bar(tool strip) with Add, update,delete, cancel and close buttons... In add button i have....

private void btn_Add_Click(object sender, EventArgs e)
    {
NewSavebtn();
}

public void NewSavebtn()
        {
           if (btn_Add.Text == "&New")
            {
                btn_Add.Text = "&Save";
                btn_Edit.Enabled = false;
                btn_Delete.Enabled = false;
                txtDetailName.Enabled = true;
                TxtHeadName.Enabled = true;


                   UnLock();
                }



else if (btn_Add.Text == "&Save")
            {

            save_data();
            Lock(); 
            btn_Add.Text = "&Add";
            btn_Edit.Enabled = true;
            btn_Delete.Enabled = true;
            ClearAll();
            txtDetailName.Enabled = false;
         }
        else
            MessageBox.Show("cant save data");
    }


    #region Clear Lock Unlock
    public void ClearAll()
    {

        foreach (Control ctl in this.Controls)
        {
            if (ctl is TextBox || ctl is ComboBox)
            {
                ctl.Text = "";
            }
        }

    }

    public void Lock()
    {

        foreach (Control ctl in this.Controls)
        {
            if (ctl is TextBox || ctl is ComboBox)
            {
                ctl.Enabled=false ;
            }
        }

    }
    public void UnLock()
    {

        foreach (Control ctl in this.Controls)
        {
            if (ctl is TextBox || ctl is ComboBox)
            {
                ctl.Enabled=true ;
            }
        }

    }
    #endregion

I want to add same code in almost 200+ forms. can anyone tell me what i suppose to do for it. Is there any user control or master control like asp.net in c# so that common code can be kept aside and called when its necessary... because if i put the NewSavebtn() and other methods in different class say class1 and try to call it from form1 like...

class1 c1 =new class1(this);
c1.NewSavebtn(this);

It does not work.... Thnks in advance....

Sam
  • 31
  • 1
  • 4

2 Answers2

0

If this is for a Windows forms application then you can Create a User Control, and if you are developing WPF application then you may see the following tutorial: How to Create a WPF User Control & Use It in a WPF Application ( C# )

Habib
  • 219,104
  • 29
  • 407
  • 436
0

You can create user control in windows form to use through a hundred forms. However, I think your code is not easy to change in terms of maintenance. You should use delegate to handle the event. For future improvement, you can easily to define your results with the use of delegate Execute a delegate in the ui thread (using message pump).

Community
  • 1
  • 1
cat916
  • 1,363
  • 10
  • 18