PDA

Click to See Complete Forum and Search --> : Directcast in C#


Beast777
Jul 17th, 2007, 08:05 AM
When I used VB I would use directcast to change my Main menu items visibility like this:

DirectCast(Me.MdiParent, frmMain).MenuItem13.Visible = True
DirectCast(Me.MdiParent, frmMain).MenuItem4.Visible = True
DirectCast(Me.MdiParent, frmMain).MenuItem14.Visible = True

How do I accomplish the same thing in C#?

jmcilhinney
Jul 17th, 2007, 08:10 AM
First of all, you wouldn't cast the same variable three times in either language. You would cast only once and then use the new variable three times:Dim mainForm As frmMain = DirectCast(Me.MdiParent, frmMain)

mainForm.MenuItem13.Visible = True
mainForm.MenuItem4.Visible = True
mainForm.MenuItem14.Visible = TruefrmMain mainForm = (frmMain)Me.MdiParent;

mainForm.MenuItem13.Visible = true;
mainForm.MenuItem4.Visible = true;
mainForm.MenuItem14.Visible = true;I'm also a little concerned about any app with 14 menu items without descriptive names. I wouldn't do that even in a test app.

RudiVisser
Jul 17th, 2007, 08:13 AM
DirectCast doesn't exist in C#?

Just try

this.mdiParent.menuItem4.Visible = true;
this.mdiParent.menuItem13.Visible = true;
this.mdiParent.menuItem14.Visible = true;

jmcilhinney
Jul 17th, 2007, 08:20 AM
DirectCast doesn't exist in C#?

Just try

this.mdiParent.menuItem4.Visible = true;
this.mdiParent.menuItem13.Visible = true;
this.mdiParent.menuItem14.Visible = true;That will not work because it would require late-binding. DirectCast was added to VB so it could behave like other languages. Casting has existed in C and languages derived from C for ages, and presumably before that too.

Beast777
Jul 17th, 2007, 09:00 AM
Yes I was lazy about the descriptive names. They were all done in VB and I am now doing them over in C# with proper name formating.

I guess my biggest problem is that I don't understand what casting really is.

Beast777
Jul 17th, 2007, 09:06 AM
I tried this from your example:

frmMain mainForm = (frmMain)Me.MdiParent;
mainForm.itmLogin.Visible = true;

Me doesn't exist and I changed it to this.

CODE]frmMain mainForm = (frmMain)this.MdiParent;[/CODE]

When I now type mainform and hit the ".", I do not get any of the menu items in my intellisense. I do get Menu and then MenuItems but no where to specify the item. Any ideas?

jmcilhinney
Jul 17th, 2007, 09:17 AM
Have you ever heard the expression "to cast something in a different light"? Literally that means to cast light on an object from a different direction to reveal previously unseen aspects of it. Figuratively it means to look at the same thing in a different way. Casting in programming is exactly the same. Your MdiParent property refers to a frmMain object, but because it is type Form you can only access members of the Form class. When you cast it you create a new variable of a different type to refer to the same object. Because the new variable is type frmMain you can access all the members of the frmMain class, not just those inherited from the Form class. You're looking at the same object in a different way.

Here's an analogy I like to use. A vet can treat essentially an animal. You bring a box to the vet and they know it contains an animal. They don't know what kind of animal though, so they don't know how to treat it yet. All they can do are the things that are common to all animals because that's as much as they know about it. You open the box and they see that your animal is a cat. Now they know they can treat it as a cat. The cat didn't magically change. It's the same thing it always was. It's just how it can be treated that's changed because now the vet knows more about it.

The Form type is analogous to the animal type, the frmMain type to the cat type and the compiler is the vet. The compiler knows it has a Form but no more. When you cast you are telling the compiler that the object is actually type frmMain, so the compiler can now treat it as such.

Beast777
Jul 17th, 2007, 09:24 AM
Thanks for the explanation. Great analogy! My menu is called mnuLibrary. I would of thought by the explanation I should of been able to do the

Main mainForm = (Main)this.MdiParent;

and then when I hit the period after typing mainForm that I would be able to see mnuLibrary but I cant. I am getting lost.

I just made my menu public and I can now see the it with intellisense but I still do not have my items listed just MenuItems as a choice.

nmadd
Jul 17th, 2007, 09:37 AM
I agree. Great explanation.

If this is the MainMenuStrip (http://msdn2.microsoft.com/en-us/library/system.windows.forms.form.mainmenustrip(vs.80).aspx) of your form you can do something like this:

MDIParent1 frm = (MDIParent1)this.MdiParent;
frm.MainMenuStrip.Items["editMenu"].Visible = false;

Beast777
Jul 17th, 2007, 09:55 AM
Okay, slowly getting there. Now I have this code:

Main mainForm = (Main)this.MdiParent;
mainForm.mnuLibrary.Items["mnuLogin"].Visible = true;

and I get this error:

Object reference not set to an instance of an object.

nmadd
Jul 17th, 2007, 11:56 AM
So this is not your form's MainMenuStrip? That is a property of the form itself. So now, as jmc said, we have a MenuStrip in the box, but the vet is not sure which one. :bigyello:

I could be off base, but you could try something like this:


MDIParent1 frm = (MDIParent1)this.MdiParent;
MenuStrip mnu = (MenuStrip)frm.Controls["mnuLibrary"];
mnu.Items["myMenuItem"].Visible = false;

Beast777
Jul 17th, 2007, 12:15 PM
I have the same error with this code -

Main mainForm = (Main)this.MdiParent;
MenuStrip mnu = (MenuStrip)mainForm.Controls["mnuLibrary"];
mainForm.mnuLibrary.Items["mnuLogin"].Visible = true;

Object reference not set to an instance of an object.

nmadd
Jul 17th, 2007, 12:24 PM
That last line is exactly the same as the last line you posted in post #10. That doesn't work.
Look at this again:
mnu.Items["myMenuItem"].Visible = false;

Beast777
Jul 17th, 2007, 01:06 PM
I just tried this:

Main mainForm = (Main)this.MdiParent;
MenuStrip mnu = (MenuStrip)mainForm.Controls["mnuLibrary"];
mnu.Items["mnuLogin"].Visible = true;

and got the same error as before, then I did try:

Main mainForm = (Main)this.MdiParent;
MenuStrip mnu = (MenuStrip)mainForm.Controls["mnuLibrary"];
// mainForm.mnuLibrary.Items["mnuLogin"].Visible = true;
((ToolStripMenuItem)this.MdiParent.MainMenuStrip.Items[0]).Visible = false;

and it will make the whole menu dissapear. I wish I could do subitems now.

Beast777
Jul 17th, 2007, 01:10 PM
Well this finally worked using

((ToolStripMenuItem)this.MdiParent.MainMenuStrip.Items[1]).DropDown.Items[0].Visible = true;

I would of thought there would of been an easier way.

nmadd
Jul 17th, 2007, 02:39 PM
So this was your MainMenuStrip?
Anyway, glad you figured it out.

Let everybody know and mark this thread resolved.

Beast777
Jul 18th, 2007, 06:47 AM
So this was your MainMenuStrip?

I used to just drop a MainMenu control on the form in 2003 but now there are menustrips, menus, etc.

What is the norm to use for a MDI app?

nmadd
Jul 18th, 2007, 08:23 AM
The MenuStrip is the control that "replaced" MainMenu. MainMenu is still there, but mostly for backward compatibility: http://msdn2.microsoft.com/en-us/library/system.windows.forms.menustrip(vs.80).aspx

The MainMenuStrip is a form property of which menu strip is, well, the main MenuStrip. :bigyello:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.form.mainmenustrip(vs.80).aspx