469,323 Members | 1,575 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

Enable / Disable forms?

Hi!

I have two forms, one opening the other. The opener disables itself
when it opens the other window, and the other window is suppose to
enable the first window again when closed. I tried this code, but I
get the error that "'object' does not contain a definition for
'DoEnable'". I'm quite a rookie in C#, so hopefully there is a quick
way to fix this...

FORM1:
private void buttonNew_Click(object sender, System.EventArgs e)
{
this.Enabled = false;
NewCDForm myNewCDForm = new NewCDForm(this);
myNewCDForm.Show();
}

public void DoEnable()
{
this.Enabled = true;
}
FORM 2:
private void NewCDForm_Closed(object sender, System.EventArgs e)
{
myCaller.DoEnable();
}

best regards,
Andreas
Nov 16 '05 #1
9 30343
Are you storing the Form reference in the overloaded constructor for Form2
as the proper type? Make sure that you're not storing the Form1 reference as
a basic Form.

--
Tim Wilson
..Net Compact Framework MVP

"Andreas" <d9****@efd.lth.se> wrote in message
news:f7**************************@posting.google.c om...
Hi!

I have two forms, one opening the other. The opener disables itself
when it opens the other window, and the other window is suppose to
enable the first window again when closed. I tried this code, but I
get the error that "'object' does not contain a definition for
'DoEnable'". I'm quite a rookie in C#, so hopefully there is a quick
way to fix this...

FORM1:
private void buttonNew_Click(object sender, System.EventArgs e)
{
this.Enabled = false;
NewCDForm myNewCDForm = new NewCDForm(this);
myNewCDForm.Show();
}

public void DoEnable()
{
this.Enabled = true;
}
FORM 2:
private void NewCDForm_Closed(object sender, System.EventArgs e)
{
myCaller.DoEnable();
}

best regards,
Andreas

Nov 16 '05 #2
Oh, sorry. I have one more method of importance, the load-method of
Form 2 that sets myCaller = FORM1! (or at least that is what I try to
do.)
So, my problem is still, why can't I call myCaller.DoEnable()???

I'm not sure what you ment with "basic Form" Tim, I store it as
"object".

FORM 1:
private void buttonNew_Click(object sender, System.EventArgs e)
{
this.Enabled = false;
NewCDForm myNewCDForm = new NewCDForm(this);
myNewCDForm.Show();
}

public void DoEnable()
{
this.Enabled = true;
}

FORM 2:
internal class NewCDForm : System.Windows.Forms.Form
{
object myCaller;
public NewCDForm(object caller)
{
myCaller = caller;
}
private void NewCDForm_Closed(object sender, System.EventArgs e)
{
myCaller.DoEnable();
}
}

Thanks again,
Andreas Lundgren

"Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote in message news:<eZ**************@TK2MSFTNGP15.phx.gbl>...
Are you storing the Form reference in the overloaded constructor for Form2
as the proper type? Make sure that you're not storing the Form1 reference as
a basic Form.

--
Tim Wilson
.Net Compact Framework MVP

"Andreas" <d9****@efd.lth.se> wrote in message
news:f7**************************@posting.google.c om...
Hi!

I have two forms, one opening the other. The opener disables itself
when it opens the other window, and the other window is suppose to
enable the first window again when closed. I tried this code, but I
get the error that "'object' does not contain a definition for
'DoEnable'". I'm quite a rookie in C#, so hopefully there is a quick
way to fix this...

FORM1:
private void buttonNew_Click(object sender, System.EventArgs e)
{
this.Enabled = false;
NewCDForm myNewCDForm = new NewCDForm(this);
myNewCDForm.Show();
}

public void DoEnable()
{
this.Enabled = true;
}
FORM 2:
private void NewCDForm_Closed(object sender, System.EventArgs e)
{
myCaller.DoEnable();
}

best regards,
Andreas

Nov 16 '05 #3
Instead of storing the Form as an Object type, store it as the actual Form
type. The compiler needs to verify that the method that you are attempting
to call actually exists on the object. So, let's say, that the first Form
class reference, the one that is being passed into the "NewCDForm" Form, is
called "Form1", then you would need to store it as the proper Form type
instead of object.

FORM 2:
internal class NewCDForm : System.Windows.Forms.Form
{
Form1 myCaller;
public NewCDForm(Form1 caller)
{
myCaller = caller;
}
private void NewCDForm_Closed(object sender, System.EventArgs e)
{
myCaller.DoEnable();
}
}

--
Tim Wilson
..Net Compact Framework MVP

"Andreas" <d9****@efd.lth.se> wrote in message
news:f7**************************@posting.google.c om...
Oh, sorry. I have one more method of importance, the load-method of
Form 2 that sets myCaller = FORM1! (or at least that is what I try to
do.)
So, my problem is still, why can't I call myCaller.DoEnable()???

I'm not sure what you ment with "basic Form" Tim, I store it as
"object".

FORM 1:
private void buttonNew_Click(object sender, System.EventArgs e)
{
this.Enabled = false;
NewCDForm myNewCDForm = new NewCDForm(this);
myNewCDForm.Show();
}

public void DoEnable()
{
this.Enabled = true;
}

FORM 2:
internal class NewCDForm : System.Windows.Forms.Form
{
object myCaller;
public NewCDForm(object caller)
{
myCaller = caller;
}
private void NewCDForm_Closed(object sender, System.EventArgs e)
{
myCaller.DoEnable();
}
}

Thanks again,
Andreas Lundgren

"Tim Wilson" <TIM(UNDERSCORE)WILSON(AT)ROGERS(PERIOD)COM> wrote in message

news:<eZ**************@TK2MSFTNGP15.phx.gbl>...
Are you storing the Form reference in the overloaded constructor for Form2 as the proper type? Make sure that you're not storing the Form1 reference as a basic Form.

--
Tim Wilson
.Net Compact Framework MVP

"Andreas" <d9****@efd.lth.se> wrote in message
news:f7**************************@posting.google.c om...
Hi!

I have two forms, one opening the other. The opener disables itself
when it opens the other window, and the other window is suppose to
enable the first window again when closed. I tried this code, but I
get the error that "'object' does not contain a definition for
'DoEnable'". I'm quite a rookie in C#, so hopefully there is a quick
way to fix this...

FORM1:
private void buttonNew_Click(object sender, System.EventArgs e)
{
this.Enabled = false;
NewCDForm myNewCDForm = new NewCDForm(this);
myNewCDForm.Show();
}

public void DoEnable()
{
this.Enabled = true;
}
FORM 2:
private void NewCDForm_Closed(object sender, System.EventArgs e)
{
myCaller.DoEnable();
}

best regards,
Andreas

Nov 17 '05 #4
Thanks Tim!
(Google problem made me answer the first post.)

That solvs my problem, but it removes a bit of the beauty! What if
this was a more general form that could have different callers? Lets
say a kind of a more advanced Yes/No form. In VB6, I can typically do
such an design where a general form can call a specific function that
several different callers have implemented.

best regards
/Andreas
Nov 17 '05 #5
One way you could do this is to create a base Form class that contains the
method. Then have all the other Forms inheriting from this Form. Then use
the base Form type as the expected parameter. Since all the derived Forms
will contain the method through inheritance, you would gain some more
flexibility this way.

Alternatively, you might consider creating a custom event on the second Form
that other Forms can hook into and receive notification when something
interesting happens. In this case, the event is already created for you. If
you listen for the second Form to close, through the Closed event, the first
Form can re-enable itself.

....

Form2 f = new Form2();
f.Closed += new EventHandler(f_Closed);
this.Enabled = false;
f.Show();

....

private void f_Closed(object sender, EventArgs e)
{
this.Enabled = true;
}

Are you sure that you don't want to use the ShowDialog() method ike Otis
proposed?

--
Tim Wilson
..Net Compact Framework MVP

"Andreas" <d9****@efd.lth.se> wrote in message
news:f7**************************@posting.google.c om...
Thanks Tim!
(Google problem made me answer the first post.)

That solvs my problem, but it removes a bit of the beauty! What if
this was a more general form that could have different callers? Lets
say a kind of a more advanced Yes/No form. In VB6, I can typically do
such an design where a general form can call a specific function that
several different callers have implemented.

best regards
/Andreas

Nov 17 '05 #6
Thanks a lot!

That was two handsome ways to solv my problem! I tried the
ShowDialog() functionallity, and it was a simple way to solve simple
problems, byt if you want for example a general print dialog that
could be opened from several different forms, and the print dialog
must notify it's caller the status when it is done, the ShowDialog()
is not enougth. (It did however solv my acctual question, but as a
newbe to the language, I was also intressted in finding a solution to
the general problem.)

/Andreas
Nov 17 '05 #7
So are you back up and running? No problems now?

--
Tim Wilson
..Net Compact Framework MVP

"Andreas" <d9****@efd.lth.se> wrote in message
news:f7**************************@posting.google.c om...
Thanks a lot!

That was two handsome ways to solv my problem! I tried the
ShowDialog() functionallity, and it was a simple way to solve simple
problems, byt if you want for example a general print dialog that
could be opened from several different forms, and the print dialog
must notify it's caller the status when it is done, the ShowDialog()
is not enougth. (It did however solv my acctual question, but as a
newbe to the language, I was also intressted in finding a solution to
the general problem.)

/Andreas

Nov 17 '05 #8
Andreas wrote:
That solvs my problem, but it removes a bit of the beauty! What if
this was a more general form that could have different callers? Lets
say a kind of a more advanced Yes/No form. In VB6, I can typically do
such an design where a general form can call a specific function that
several different callers have implemented.


You might consider defining an appropriate interface that all callers must
implement. This will be less specific. Otherwise, inheriting from a common
base class would solve your problem, too.

Nov 17 '05 #9
Andreas wrote:

[...snip...]
for example a general print dialog that
could be opened from several different forms, and the print dialog
must notify it's caller the status when it is done, the ShowDialog()
is not enougth. [...snip...]

Your form might contain public properties for the information you'd want to
pass to the caller(s). The caller may retrieve the properties value after
the form has closed. Setting the Forms DialogResult property when dismissing
the dialog might be an option, too:

Form1 myDialog = new MyDialog();
if (myDialog.ShowDialog() == DialogResult.OK)
{
int retrievedCount = myDialog.itemCount;
string retrievedText = myDialog.userText;
};

Where itemCount and userText are examples for properties of the MyDialog
class.
(It did however solv my acctual question, but as a
newbe to the language, I was also intressted in finding a solution to
the general problem.)

/Andreas


HTH
Michael
Nov 17 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Bob Bedford | last post: by
1 post views Thread by vncntj | last post: by
6 posts views Thread by Ben | last post: by
3 posts views Thread by Stilgar[bbs.isca.uiowa.edu] | last post: by
1 post views Thread by inadsad | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.