1

I got the following method in my MDIparent1

public void Disablebutton()
{
    toolStripButton1.Enabled = false;
}

In my LoginForm OKButton click

MDIParent1 f1 = new MDIParent1();
f1.Disablebutton();
this.Close();

The LoginForm is Modal of MDIparent1.

Problem is the Disablebutton() don't works in LoginForm

Mo Patel
  • 2,321
  • 4
  • 22
  • 37
Karlx Swanovski
  • 2,869
  • 9
  • 34
  • 67
  • What about it doesn't work? Does toolStripButton1 exist in LoginForm? You probably need to direct Disablebutton to work on the toolStripButton1 which exists inside LoginForm - there is a way to do this, but want to confirm that's your issue first. – NWard Jul 25 '13 at 18:14
  • @NWard tootpStripButton1 is in MDIparent – Karlx Swanovski Jul 25 '13 at 18:15

3 Answers3

2

You're creating a new instance of MDIParent1 and not using the actual instance that opened LoginForm. You could pass the parent into the LoginForm constructor.

private MDIParent1 _parentForm = null;
public LoginForm(MDIParent1 parentForm)
{
   _parentForm = parentForm;
}

//then in whichever event you're using
_parentForm.Disablebutton();
this.Close();

When you want to show the LoginForm form, pass in the MDIParent1 form.

//assuming this is in MDIParent1.cs (otherwise pass the form instance variable)
using(LoginForm lf = new LoginForm(this))
{
   lf.Show();
}
keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • is that same thing with LoginForm lf = new LoginForm(); lf.ShowDialog();? – Karlx Swanovski Jul 25 '13 at 18:20
  • `ShowDialog` will make the parent form inaccessible until the child form is closed (it's a modal window). `Show` will allow both forms to be accessed so you can use `ShowDialog` instead if that's what you want. As an alternative to the above approach, you can set `lf.MdiParent = this` and then access the `MdiParent` property as mentioned in the other answers. – keyboardP Jul 25 '13 at 18:24
  • does not contain a constructor that takes 0 arguments, cannot convert from App.MainForm to App.MDIParent1 – Karlx Swanovski Jul 25 '13 at 18:26
  • Did you use `LoginForm lf = new LoginForm(this)`? – keyboardP Jul 25 '13 at 18:27
  • I tried to use LoginForm lf = new LoginForm(this) and got same error – Karlx Swanovski Jul 25 '13 at 18:30
  • Instead of `this`, pass in your `MDIParent1` object, it seems you're calling the form from another place other than `MDIParent1.cs`. – keyboardP Jul 25 '13 at 18:30
  • I use exactly that code. NUllReferenceException was unhandle Object reference not set to an instance of an object – Square Ponge Jul 25 '13 at 18:34
  • @SquarePonge - I didn't realise you were using two different accounts, I thought you weren't OP. What's null? – keyboardP Jul 26 '13 at 00:00
1

From what I understand of your code, you are creating a new MDIParent1 window and calling DisableButton on the newly created MDIParent1 window. You have to call DisableButton on the form owning LoginForm (via the MdiParent property).

Edit: someone beat me to it.

ARsEnAl51
  • 11
  • 1
0

You should do this in your MDIchild form

  if(this.MdiParent is  LoginForm)
  {
     (this.MdiParent as LoginForm).Disablebutton();
  }
Ehsan
  • 31,833
  • 6
  • 56
  • 65