Hi there,
Am calling a form from a button in another form. And then I am calling the form as

frmSubCDetails subDetails = new frmSubCDetails();
subDetails.Show();
in a button which is in the View Submit form.What I want is how can I make the items in the from frmSubCDetails disable
when the form load, I cannot write the code for enabling and disabling the code in the frmSubCDetails form because it is called in another place also,
so is there any other way that I can do this,

thanxxxxxxxxxxx

Recommended Answers

All 12 Replies

You can disable controls in the other form in this way:

Form2 oForm;

oForm = new Form2();
oForm.Show();
foreach(Control c in oForm.Controls)
{
    c.Enabled=false;
}

HTH

You can disable controls in the other form in this way:

Form2 oForm;

oForm = new Form2();
oForm.Show();
foreach(Control c in oForm.Controls)
{
    c.Enabled=false;
}

HTH

hey
how can i make one button in the from enable.

how can i do this.

thanxxxx

If the effect you want is to have all but one controls disabled while the second form is open then disable them just before you open the second form.

One way is to disable all the controls using the foreach(Control c in oForm.Controls) idea show by Teme64. Then enable the control you want to remain active.

Another is to group controls in to panels, and disable the panels; becasue when you set panel.Enabled = false; all the controls in it are also disabled. This might not be appropriate for your case however. It depends on your control layout.

If the effect you want is to have all but one controls disabled while the second form is open then disable them just before you open the second form.

One way is to disable all the controls using the foreach(Control c in oForm.Controls) idea show by Teme64. Then enable the control you want to remain active.

Another is to group controls in to panels, and disable the panels; becasue when you set panel.Enabled = false; all the controls in it are also disabled. This might not be appropriate for your case however. It depends on your control layout.

hey
i did as mentioned in the post as below

Form2 oForm;

oForm = new Form2();
oForm.Show();
foreach(Control c in oForm.Controls)
{
    c.Enabled=false;
  [B]  panelBtn.Enable = true;//panelBtn is in the Form2 how can i make it enable in another form   [/B]

}

how can i do this "
panelBtn.Enable = true;//panelBtn is in the Form2 how can i make it enable in another form " in the code
thanxxxxx

You can do one more thing
Set all the controls disabled by default
Set a global boolean variable which will help u determine which button is calling the form and while the form loads you can check value of the boolean variable and accordingly enable the controls

In frmSubCDetails form have a method that takes a bool and enables/diables controls as required base on that bool.
Then call the method from the other form(s) to either enable/disable frmSubCDetails controls.

In line with what nick and babbu said, you can modify or overload your constructor. Either add a boolean parameter to the standard constructor or leave your standard constructor parameterless and add a second constructor whioch takes a boolean value:

public partial class frmSubCDetails : Form
{
    public frmSubCDetails()
    {
        InitializeComponent();
    }
    
    //overloaded constructor
    public frmSubCDetails(bool ControlsEnabled)
    {
        InitialiseComponent();
        //either set controls manually:
        btn1.Enabled = ControlsEnabled;
  
        //or use a loop:
        foreach(Control c in this.Controls(
        {
            c.Enabled = ControlsEnabled;
        }
    }
}

In line with what nick and babbu said, you can modify or overload your constructor. Either add a boolean parameter to the standard constructor or leave your standard constructor parameterless and add a second constructor whioch takes a boolean value:

public partial class frmSubCDetails : Form
{
    public frmSubCDetails()
    {
        InitializeComponent();
    }
    
    //overloaded constructor
    public frmSubCDetails(bool ControlsEnabled)
    {
        InitialiseComponent();
        //either set controls manually:
        btn1.Enabled = ControlsEnabled;
  
        //or use a loop:
        foreach(Control c in this.Controls(
        {
            c.Enabled = ControlsEnabled;
        }
    }
}

hey,
i wrote the below code in ViewPSubmited where i am calling the frmSubDetails form

frmSubCDetails subDetails = new frmSubCDetails();
                subDetails.Show();

                foreach (Control ctrl in subDetails.Controls)
                {
                    ctrl.Enabled = false;
                    bool value = true;
                  [B]  subDetails.frmSubCDetails(value);//here there is an error[/B]
                }

subDetails.frmSubCDetails(value);
//here there is an error saying type 'Admin_Application.PresentationLayer.Proposal_in_Progress.frmSubCDetails' could be found (are you missing a using directive or an assembly reference?) \

why is that
and in the frmSubDetails
i wrote the code

//overloaded constructor
        public frmSubCDetails(bool ControlsEnabled)
        {
            InitializeComponent();
            //either set controls manually:
            pBtnClose.Enabled = ControlsEnabled;             
        }

what is the problem here

public frmSubCDetails isnt a method, it is a constructor. It is the code that is run when you create a new instance of the class.
You would call it like this:

frmSubCDetails newForm = new frmSubCDetails(true);

public frmSubCDetails isnt a method, it is a constructor. It is the code that is run when you create a new instance of the class.
You would call it like this:

frmSubCDetails newForm = new frmSubCDetails(true);

hey so i made it as in the formthat i am calling

frmSubCDetails subDetails = new frmSubCDetails(true);
                

                foreach (Control ctrl in subDetails.Controls)
                {
                    ctrl.Enabled = false;
                    subDetails.ShowDialog();
                }

and in the frmSubDetails i wrote the code as

//overloaded constructor
        public frmSubCDetails(bool ControlsEnabled)
        {
            InitializeComponent();
            //either set controls manually:
            pBtnClose.Enabled = ControlsEnabled;             
        }

but it still dosen't work

how can i solve this

thanxxxxx

What on earth did you do? You've butchered all of the code you were given lol.
Daniweb lesson number one...Take the time to examine the code you are given. Read the msdn pages for the parts you don't understand. If you ever want to be a great programmer you need to understand HOW and WHY things work, not just use code that others have given you.

Lets examine your code:

foreach (Control ctrl in subDetails.Controls)
   {
      ctrl.Enabled = false;
      subDetails.ShowDialog();
   }

A foreach loop will execute the block of code once for each item found in the given collection. In your case, the code will run once for each control on your form. Do you want to show your new form just once, or once for every control you have on it? If you have 10 controls on your form then your code will attempt to show the form 10 times. And on top of that, you are showing it modally (ShowDialog()) so the loop will pause until the form is closed.

Next, you have implemented part of the method overloaded constructor i showed you. When i had:

//either set controls manually:
            pBtnClose.Enabled = ControlsEnabled;

I meant you can manually enable/disable each control. SO list every control you have and set its .Enabled property to match the EnabledProperty parameter. Its far more efficient to use the foreach loop to do this.

Go back through the code you were shown and see if you can correct the problems. If you get stuck then I'll see if i can guide you to a workable solution.

commented: "butchered" LOL +1

What on earth did you do? You've butchered all of the code you were given lol.
Daniweb lesson number one...Take the time to examine the code you are given. Read the msdn pages for the parts you don't understand. If you ever want to be a great programmer you need to understand HOW and WHY things work, not just use code that others have given you.

Lets examine your code:

foreach (Control ctrl in subDetails.Controls)
   {
      ctrl.Enabled = false;
      subDetails.ShowDialog();
   }

A foreach loop will execute the block of code once for each item found in the given collection. In your case, the code will run once for each control on your form. Do you want to show your new form just once, or once for every control you have on it? If you have 10 controls on your form then your code will attempt to show the form 10 times. And on top of that, you are showing it modally (ShowDialog()) so the loop will pause until the form is closed.

Next, you have implemented part of the method overloaded constructor i showed you. When i had:

//either set controls manually:
            pBtnClose.Enabled = ControlsEnabled;

I meant you can manually enable/disable each control. SO list every control you have and set its .Enabled property to match the EnabledProperty parameter. Its far more efficient to use the foreach loop to do this.

Go back through the code you were shown and see if you can correct the problems. If you get stuck then I'll see if i can guide you to a workable solution.

hey thanx for the explanation

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.