MDI one Child instance.

S

scole954387

Hi,

I'm creating a MDI application and running into the common problem of
more than one instance of the child form. I've read several articles
and postings on the Singleton approach, but I can't get it to work.

What I have:

One form with linklabels and another form with a webbrowser. When the
linklabel is clicked the webbrowser navigates to the url. The problem
is each time a linklabel is clicked a new webbrowser form opens.

I'm trying to have the one webbrowser form update to the new url with
each click of a linklabel rather than having multiple webbrowser forms
opening.

Do anyone have any idea on how to do this. Keep in mind that I am very
new to visual studio and c# programming. Example code is best.

Thanks a bunch,
S. Cole
 
S

Steven Nagy

Hi

Sure. Singleton is definately the right approach.
Create a form level declaration of your Form in the parent container
form.
When the link label is clicked, have it check first if the form
instance is null. If it is, then initialise it. Then simply call the
usual mdi parent stuff and show the form.

Eg.
private MyForm as SomeForm
void MyLabel_OnClick(blah blah blah) {
if (MyForm == null) {
MyForm = new SomeForm();
// whatever else needs declaring, like setting form parent.
}
MyForm.Show();
}
 
S

scole954387

Thanks for the information, but I can't get it to work.

main form - parent mdi
form 1 - link labels
form 2 - webbrowser

My code...
private void lnkGoogle_LinkClicked(object sender,
LinkLabelLinkClickedEventArgs e)
{
if (frmContentDisplay == null)
{
frmContentDisplay objChildContentDisplay = new
frmContentDisplay();
objChildContentDisplay.MdiParent = this;
}
objChildContentDisplay.Show();
objChildContentDisplay.LoadURL("http://www.google.com");

}


and I get the following errors...

Error 1 'CDV_My_Agent.frmContentDisplay' is a 'type' but is used like a
'variable'
Error 2 The name 'objChildContentDisplay' does not exist in the current
context
Error 3 The name 'objChildContentDisplay' does not exist in the current
context


The form is initially loaded by the main mdi window with...

// Show ContentDisplay window on Startup
frmContentDisplay objChildContentDisplay = new
frmContentDisplay();
objChildContentDisplay.MdiParent = this;
objChildContentDisplay.Show();

//objChildContentDisplay.LoadURL("http://www.anything.com");
 
S

Steven Nagy

Try this:


frmContentDisplay objChildContentDisplay;

LinkLabelLinkClickedEventArgs e)
{
if (objChildContentDisplay==null)
{
frmContentDisplay objChildContentDisplay = new
frmContentDisplay();
objChildContentDisplay.MdiParent = this;
}
objChildContentDisplay.Show();
objChildContentDisplay.LoadURL("http://www.google.com");


}
 
S

S. Cole

Hey,

Thanks for the help. Here's the errors received...

Error 1 The name 'objChildContentDisplay' does not exist in the current
context
Error 2 The name 'objChildContentDisplay' does not exist in the current
context
Error 3 The name 'objChildContentDisplay' does not exist in the current
context

I'm assuming that this is happening because there's no such thing as
objChildContentDisplay until the frmContentDisplay
objChildContentDisplay = new frmContentDisplay(); is parsed. And I'm
assuming that this is not getting parsed because of the
objChildContentDisplay==null statement. Any ideas?

I have the parent form opening the ContentDisplay form (webbrowser
form) under the _Load function. Should I have the linklabel child form
open it instead?

Thanks,
S. Cole
 
S

Steven Nagy

Sorry, I made a mistake in the code I wrote!
I redeclared a variable with the same name when I ment to use the same
form level variable.
Here's the new code (spot the difference!) :
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top