newbie question about VS

  • Thread starter Thread starter John Salerno
  • Start date Start date
J

John Salerno

What I want to do, just for an experiment, is make a menu item called
"About" and when the user clicks on it, a separate About window pops up.
I see how to add the About form in the designer, but I don't see how to
reference it in the menu item Click event handler. I tried
AboutBox1.ShowDialog(), but I got a compile error saying that
ShowDialog() needed an instance. I know I could write something like

Form AboutBox1 = new Form();

to avoid this error, but the box that ends up being shown when I click
the menu item isn't the same as the one I added.

So basically, how do you reference other forms that you've added to your
project?

Thanks.
 
You'll need to use the class name. Assuming that the about form class is
named "AboutBox" you could do something like this...

AboutBox aboutBox1 = new AboutBox();
aboutBox1.ShowDialog();
// Note: Don't forget to Dispose if you don't
// need this object anymore at this point.
// aboutBox1.Dispose();
 
John said:
What I want to do, just for an experiment, is make a menu item called
"About" and when the user clicks on it, a separate About window pops up.
I see how to add the About form in the designer, but I don't see how to
reference it in the menu item Click event handler. I tried
AboutBox1.ShowDialog(), but I got a compile error saying that
ShowDialog() needed an instance. I know I could write something like

Form AboutBox1 = new Form();

to avoid this error, but the box that ends up being shown when I click
the menu item isn't the same as the one I added.

So basically, how do you reference other forms that you've added to your
project?

Thanks.
You need <FormName> AboutBox = new <FormName>();
AboutBox.ShowDialog();

You have added a class definition (your form), then you need to
instantiate an instance of this class.

Such as

frmAbout AboutBox = new frmAbout();
AboutBox.ShowDialog();

HTH
JB
 
Tim said:
You'll need to use the class name. Assuming that the about form class is
named "AboutBox" you could do something like this...

AboutBox aboutBox1 = new AboutBox();
aboutBox1.ShowDialog();
// Note: Don't forget to Dispose if you don't
// need this object anymore at this point.
// aboutBox1.Dispose();

Ah! I was wrong about doing Form AboutBox1! Form isn't the write class
name to use! Duh!

Also, do I need Dispose(), or does simply closing the About window do this?
 
Tim said:
You'll need to use the class name. Assuming that the about form class is
named "AboutBox" you could do something like this...

AboutBox aboutBox1 = new AboutBox();
aboutBox1.ShowDialog();
// Note: Don't forget to Dispose if you don't
// need this object anymore at this point.
// aboutBox1.Dispose();

Ok, new question. I figured out how to reference the About box, but the
About box that is called when I run the program doesn't reflect the
changes I make to it in the designer. I changed the Text property of the
application name, for example, but this didn't show up. I also changed
it in the Assembly Information form, and it still didn't show up. How do
I show these changes?
 
If you display a Form window using its "Show" method it will be closed when,
for example, the user clicks the "X" at the top right. However, when you
display a Form using its "ShowDialog" method, the Form is not closed when
the user clicks the "X", its merely hidden. See the remarks section at the
link below to learn more about this.
http://msdn.microsoft.com/library/d...stemwindowsformsformclassshowdialogtopic1.asp

So the typical display pattern for a modal dialog is like this...

MyForm myForm1 = new MyForm();
if (myForm1.ShowDialog() == DialogResult.OK)
{
// The user OK'd something.
}
myForm1.Dispose();

--
Tim Wilson
..Net Compact Framework MVP

John Salerno said:
Ah! I was wrong about doing Form AboutBox1! Form isn't the write class
name to use! Duh!

Also, do I need Dispose(), or does simply closing the About window do
this?
 
So you changed the Text property of the About Form? This should reflect the
text in the title bar of the About Form. You're not seeing that?
 
Tim said:
So you changed the Text property of the About Form? This should reflect the
text in the title bar of the About Form. You're not seeing that?

No, I changed the Text property of the labels inside the form.
 
Tim said:
If you display a Form window using its "Show" method it will be closed when,
for example, the user clicks the "X" at the top right. However, when you
display a Form using its "ShowDialog" method, the Form is not closed when
the user clicks the "X", its merely hidden. See the remarks section at the
link below to learn more about this.
http://msdn.microsoft.com/library/d...stemwindowsformsformclassshowdialogtopic1.asp

So the typical display pattern for a modal dialog is like this...

MyForm myForm1 = new MyForm();
if (myForm1.ShowDialog() == DialogResult.OK)
{
// The user OK'd something.
}
myForm1.Dispose();

This code goes in the event handler? What exactly is it saying? And if I
want my About box to just have an "X" to close it (no OK button), would
this still work?
 
The code would go wherever you want to show the dialog. I was just giving a
"typical" usage scenario with that code. You don't have to follow that
pattern. But you should Dispose of the dialog when you're done with it. The
code would still work even if there wasn't an ok button. Basically the code
snippet that I posted creates an instance of the "MyForm" dialog, shows the
dialog in a modal state (ShowDialog), and waits for the dialog to return (in
this case, hide). When the dialog is hidden the DialogResult will be
returned. If this DialogResult is not necessary to process then you can
simply use the following code.

MyForm myForm1 = new MyForm();
myForm1.ShowDialog();
myForm1.Dispose();
 
And when you changed the Text values for the Labels on your About Form
you're saying that the text was not correct when you rebuilt and displayed
the About Form at runtime? Are you sure that the changes were saved?
 
Tim said:
And when you changed the Text values for the Labels on your About Form
you're saying that the text was not correct when you rebuilt and displayed
the About Form at runtime? Are you sure that the changes were saved?

Yes, that's right. I didn't save the project to my HD, but I did use the
"Save" button and that still didn't work. Does it need to be actually
saved to disk to show these changes?
 
It should save the actual file automatically when you recompile but I was
more curious about whether the changes have been persisted to the
InitializeComponent method. When you make a change to the Text property of a
control (as well as other properties) the change is persisted (stored) in
the InitializeComponent method within the code for the form class being
edited. So you are working through the designer on your About form, and you
select a Label on this About form in the designer, then go to the properties
window and modify the Text property for the selected Label, hit F5 to run
the application, and the Label text is not what you just updated? Try
looking in the InitializeComponent method to see if the modified text has
been saved there or not.
 
Tim said:
It should save the actual file automatically when you recompile but I was
more curious about whether the changes have been persisted to the
InitializeComponent method. When you make a change to the Text property of a
control (as well as other properties) the change is persisted (stored) in
the InitializeComponent method within the code for the form class being
edited. So you are working through the designer on your About form, and you
select a Label on this About form in the designer, then go to the properties
window and modify the Text property for the selected Label, hit F5 to run
the application, and the Label text is not what you just updated? Try
looking in the InitializeComponent method to see if the modified text has
been saved there or not.

Here's what I'm doing, and what's happening:

I first create a new project and leave the default name
"WindowsApplication1". Then I add an About form to the project. Next I
add a button to the first form, and in the event handler I create an
instance of the About form and then Show() it.

So far, this works. The About box shows up, and I only have one instance
so I know it isn't calling the wrong form.

Next I change labelProductName.Text of the About form to something
different (in this case, "My Program"). This change is shown in the
design editor, but when I run the program again it still shows the
default "WindowsApplication1" text.

Now, I did what you said and checked the InitializeComponent() method.
It said this:

this.labelProductName.Text = AssemblyProduct;

so next I took a look at the AssemblyInfo.cs file (I just guessed to do
this, but it seems like the logical next step). In there it still said:

[assembly: AssemblyProduct("WindowsApplication1")]

Not sure if that last step matters.
 
So you're using VS 2005. Now I get it. I was wondering how you just added an
About form to your project as if it was already built in. I was assuming
that this was through VS.Net 2003. Ok. So the text of the "labelProductName"
control is actually set after the InitializeComponent method call in the
constructor. What that means is that any text that you set into the Text
property of the "labelProductName" control through the designer will be
overwritten to reflect the product name later in the constructor, after the
InitializeComponent method is called. This explains why you are unable to
see the correct text at runtime. Inside the constructor there is a
comment...

// Initialize the AboutBox to display the product information from the
assembly information.
// Change assembly information settings for your application through
either:
// - Project->Properties->Application->Assembly Information
// - AssemblyInfo.cs

So you can follow those instructions to modify the text that is displayed.
And essentially that's what you were going to do by modifying the following
line in the AssemblyInfo.cs file...
[assembly: AssemblyProduct("WindowsApplication1")]

Or you could delete the following line...
this.labelProductName.Text = AssemblyProduct;
.... from the constructor and this will allow you to set the Text property of
the "labelProductName" control in the designer.

I would recommend doing what is stated in the comment in the constructor.
Just change the AssemblyProduct attribute value.
 
Oh, I see. So this particular Label controls are different than the
normal Label that I can drag and drop onto a form? Their Text property
behaves differently? I suppose it's tied into the fact that the About
form is pre-designed, like you mention.

Thanks for the help though. I remember reading that Assembly comment
when I first looked through the AssemblyInfo.cs file (just to see what
it was used for in the first place). Is it not weird that you have to
edit this file to change the property of a control, or is it just (like
I said above) because it's a special form?
 
Oh, I see. So this particular Label controls are different
than the normal Label that I can drag and drop onto a
form?
No. It's just a normal label.
Their Text property behaves differently?
No. They behave as expected.
I suppose it's tied into the fact that the About
form is pre-designed, like you mention.
That's the reason that all the labels are on the form, their already named,
and code already exists. This form is a template.
Is it not weird that you have to edit this file to change
the property of a control, or is it just (like I said above)
because it's a special form?
It's primarily because it's a template form. That means that they (MS) get
you started and its up to you to fill in the blanks. Pulling the information
from the AssemblyInfo file makes sense since it allows you to store the
author information, as well as product information, in one location. They do
allow you to edit this information in a nice dialog box accessible through
the project properties, rather than editing the AssemblyInfo file directly.
But I can see how this may be confusing.

--
Tim Wilson
..Net Compact Framework MVP

John Salerno said:
Oh, I see. So this particular Label controls are different than the
normal Label that I can drag and drop onto a form? Their Text property
behaves differently? I suppose it's tied into the fact that the About
form is pre-designed, like you mention.

Thanks for the help though. I remember reading that Assembly comment
when I first looked through the AssemblyInfo.cs file (just to see what
it was used for in the first place). Is it not weird that you have to
edit this file to change the property of a control, or is it just (like
I said above) because it's a special form?




Tim said:
So you're using VS 2005. Now I get it. I was wondering how you just added an
About form to your project as if it was already built in. I was assuming
that this was through VS.Net 2003. Ok. So the text of the "labelProductName"
control is actually set after the InitializeComponent method call in the
constructor. What that means is that any text that you set into the Text
property of the "labelProductName" control through the designer will be
overwritten to reflect the product name later in the constructor, after the
InitializeComponent method is called. This explains why you are unable to
see the correct text at runtime. Inside the constructor there is a
comment...

// Initialize the AboutBox to display the product information from the
assembly information.
// Change assembly information settings for your application through
either:
// - Project->Properties->Application->Assembly Information
// - AssemblyInfo.cs

So you can follow those instructions to modify the text that is displayed.
And essentially that's what you were going to do by modifying the following
line in the AssemblyInfo.cs file...
[assembly: AssemblyProduct("WindowsApplication1")]

Or you could delete the following line...
this.labelProductName.Text = AssemblyProduct;
... from the constructor and this will allow you to set the Text property of
the "labelProductName" control in the designer.

I would recommend doing what is stated in the comment in the constructor.
Just change the AssemblyProduct attribute value.
 
Tim said:
But I can see how this may be confusing.

Yeah, I'm still confused why the Text property has no affect in this
case, but I understand how to change it using the other method.
 
John said:
Tim Wilson wrote:




Yeah, I'm still confused why the Text property has no affect in this
case, but I understand how to change it using the other method.

P.S. Is this a related issue: I've noticed that when I use the Property
box to set a form to have a "Help" button, the button doesn't get added
(even though I make it True). Is this a bug, or do I need to do it a
different way?
 
What happens under normal circumstances is that when you change the Text
property of a control through the designer the value is serialized (stored)
in the InitializeComponent method. If you look into the definition for an
InitializeComponent method you'll see, amongst other things, a whole lot of
properties being set. So when you modify the Text property from its default
you may see something like this...

private void InitializeComponent()
{
...
this.label1.Text = "Text";
...
}

.... where the "..." in the above example simply denotes code that is
omitted. The InitializeComponent method is called from the constructor of
the form...

public Form1()
{
InitializeComponent();
}

But in the case of the About form, what is happening is that the label Text
property is being explicitly set after the InitializeComponent method
call...

public Form1()
{
InitializeComponent();
this.labelProductName.Text = AssemblyProduct;
}

So, if you change the Text property of the label through the designer, it is
"stored" in the InitializeComponent method as shown above. When the form is
created, the InitializeComponent is called which causes the Text property of
the label to be set to the value that you designated through the designer.
Then the InitializeComponent method call is returning and the labels Text
property is again being set, only this time the value is the product name.
So what is happening is that the Text property is getting set to the value
that you are specifying, it's just being overwritten afterwards before the
form is shown. Does that help?
 
Back
Top