Various newbie questions on C#

C

COHENMARVIN

I am used to using Visual Basic, and I am now trying out Visual C#
2008 express. One thing I don't know is how to make a dialog box
disappear.
In VB, you could say
"Unload Me"
or
"Me.Hide"
But I can't find anything like this with C# forms.

Another thing I tried to do was put a textbox on a form and have the
cursor start in the textbox when the form appears.
So in the Form1_Load event, I put the line: TextBox1.Focus();.
But this does not work - the cursor does not appear in the textbox.

Speaking of the Form_Load event, in VB you can find the events
associated with a form easily - you just click on Form in the left
dropdown and you get its events in the right dropdown, and you can
click on the event you want and it is pasted into the code. But in C#
2008, I could not find this feature. Visual Studio does provide you
with two dropdowns at the top of your edit window, and one of them has
the Form in it, but the other does not show Form events.
So if I wanted to find events such as "load", "activate", etc. for a
Form, instead of relying on my memory, how do I do that?
Thanks,
Marvin
 
T

Tom Shelton

I am used to using Visual Basic, and I am now trying out Visual C#
2008 express. One thing I don't know is how to make a dialog box
disappear.
In VB, you could say
"Unload Me"
or
"Me.Hide"
But I can't find anything like this with C# forms.

Well, you can call the forms Close method - but often with a dialog you
often want to return a value to the caller (like, did it close because the
user hit ok? or was it a cancel). So, often the best way is to set the
forms DialogResult property. You can also set up buttons to do this
automatically, in the designer. Then you can do stuff like:

using (MyDialog myDialog = new MyDialog())
{
if (myDialog.ShowDialog(this) == DialogResult.OK)
{
someValue = myDialog.SomeValue;
}
}

Setting the DialogResult will cause a modal dialog to close.
Another thing I tried to do was put a textbox on a form and have the
cursor start in the textbox when the form appears.
So in the Form1_Load event, I put the line: TextBox1.Focus();.
But this does not work - the cursor does not appear in the textbox.

Speaking of the Form_Load event, in VB you can find the events
associated with a form easily - you just click on Form in the left
dropdown and you get its events in the right dropdown, and you can
click on the event you want and it is pasted into the code. But in C#
2008, I could not find this feature. Visual Studio does provide you
with two dropdowns at the top of your edit window, and one of them has
the Form in it, but the other does not show Form events.
So if I wanted to find events such as "load", "activate", etc. for a
Form, instead of relying on my memory, how do I do that?

In the properties window for your form or control, hit the little button
with lightning icon.
 
Z

zacks

I am used to using Visual Basic, and I am now trying out Visual C#
2008 express.  One thing I don't know is how to make a dialog box
disappear.
In VB, you could say
"Unload Me"
or
"Me.Hide"
But I can't find anything like this with C# forms.

Another thing I tried to do was put a textbox on a form and have the
cursor start in the textbox when the form appears.
So in the Form1_Load event, I put the line: TextBox1.Focus();.
But this does not work - the cursor does not appear in the textbox.

Speaking of the Form_Load event, in VB you can find the events
associated with a form easily - you just click on Form in the left
dropdown and you get its events in the right dropdown, and you can
click on the event you want and it is pasted into the code.  But in C#
2008, I could not find this feature.  Visual Studio does provide you
with two dropdowns at the top of your edit window, and one of them has
the Form in it, but the other does not show Form events.
So if I wanted to find events such as "load", "activate", etc. for a
Form, instead of relying on my memory, how do I do that?
Thanks,
Marvin

Form.Close()
 
J

JamesB

I am used to using Visual Basic, and I am now trying out Visual C#
2008 express. One thing I don't know is how to make a dialog box
disappear.
In VB, you could say
"Unload Me"
or
"Me.Hide"
But I can't find anything like this with C# forms.

For this, use "this.Hide();"
Another thing I tried to do was put a textbox on a form and have the
cursor start in the textbox when the form appears.
So in the Form1_Load event, I put the line: TextBox1.Focus();.
But this does not work - the cursor does not appear in the textbox.
I think that's because the control won't be there until the load has
finished- or something. Try using the "Activated" event rather than the Load
one... (see below)
Speaking of the Form_Load event, in VB you can find the events
associated with a form easily - you just click on Form in the left
dropdown and you get its events in the right dropdown, and you can
click on the event you want and it is pasted into the code. But in C#
2008, I could not find this feature. Visual Studio does provide you
with two dropdowns at the top of your edit window, and one of them has
the Form in it, but the other does not show Form events.
So if I wanted to find events such as "load", "activate", etc. for a
Form, instead of relying on my memory, how do I do that?

When in Designer view, you shuold have the properties somewhere on screen
(where you set the form size, name etc etc). At the top of here is a little
lightning flash. Click that, and you'll see all the events. Double clicking
the blank space next to one will insert the code as you are expecting.

James.
 
F

Family Tree Mike

see inline...

I am used to using Visual Basic, and I am now trying out Visual C#
2008 express. One thing I don't know is how to make a dialog box
disappear.
In VB, you could say
"Unload Me"
or
"Me.Hide"
But I can't find anything like this with C# forms.

Hide() will work, or Close()
Another thing I tried to do was put a textbox on a form and have the
cursor start in the textbox when the form appears.
So in the Form1_Load event, I put the line: TextBox1.Focus();.
But this does not work - the cursor does not appear in the textbox.

Move your code to the Shown event. The tab order takes over after you set
the focus in the Load event. The Shown event is later in the chain.
Speaking of the Form_Load event, in VB you can find the events
associated with a form easily - you just click on Form in the left
dropdown and you get its events in the right dropdown, and you can
click on the event you want and it is pasted into the code. But in C#
2008, I could not find this feature. Visual Studio does provide you
with two dropdowns at the top of your edit window, and one of them has
the Form in it, but the other does not show Form events.
So if I wanted to find events such as "load", "activate", etc. for a
Form, instead of relying on my memory, how do I do that?
Thanks,
Marvin

Type "this." then events and methods should be shown. Events will have a
lightning bolt. to override this.Shown, type this.Shown += then tab twice to
get a default handler. You will find there are many ways to achieve any
goal. This is just my prefered way. Your mileage may vary...


Hope this helps.
 
I

Ignacio Machin ( .NET/ C# MVP )

see inline...



Hide() will work, or Close()

Note that there is a BIG difference between Hide and Close.
Move your code to the Shown event.  The tab order takes over after you set
the focus in the Load event.  The Shown event is later in the chain.

I would set the taborder of the control instead.

The best way for a newbie is using the designer, in the properties
window change to Event, you will get a list of all the events
 
F

Family Tree Mike

Ignacio Machin ( .NET/ C# MVP ) said:
Note that there is a BIG difference between Hide and Close.

I agree. They had unload and hide which very different too.

I would set the taborder of the control instead.

I agree, and should have said he could, or change the tab order...
 
C

COHENMARVIN

using (MyDialog myDialog = new MyDialog())
{
if (myDialog.ShowDialog(this) == DialogResult.OK)
{
someValue = myDialog.SomeValue;
}

}
Thanks for your help (and the help of the other people in this
thread). As usual, advice often leads to more questions. When I
create a Visual C# project, it comes with a Form called "Form1", and a
program class that has a routine such as:
static void Main()
{
Application.Run(new Form1());
}

Now is this "Form1" a modal dialog or a modeless form?

Also, where can I fit in the "using" code you showed me above, if the
form is called within "Application Run"?

Finally, if the dialog has a textbox called "TextBox1", I notice that
the Textbox is declared as being "private". Do I have to declare it
as "internal" to be able to use code such as you show above (someValue
= MyDialog.TextBox1.Text;)?
Thanks
-- Marvin
 

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

Top