By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,052 Members | 1,456 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,052 IT Pros & Developers. It's quick & easy.

MDI one Child instance.

P: n/a
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

Sep 28 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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();
}

Sep 28 '06 #2

P: n/a
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");

Steven Nagy wrote:
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();
}
Sep 28 '06 #3

P: n/a


Try this:
frmContentDisplay objChildContentDisplay;

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

Sep 28 '06 #4

P: n/a
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


Steven Nagy wrote:
Try this:
frmContentDisplay objChildContentDisplay;

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

P: n/a
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!) :
frmContentDisplay objChildContentDisplay;

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

This discussion thread is closed

Replies have been disabled for this discussion.