|
-
Jul 17th, 2007, 08:05 AM
#1
Thread Starter
Fanatic Member
Directcast in C#
When I used VB I would use directcast to change my Main menu items visibility like this:
Code:
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#?
-
Jul 17th, 2007, 08:10 AM
#2
Re: Directcast in C#
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:
VB.NET Code:
Dim mainForm As frmMain = DirectCast(Me.MdiParent, frmMain)
mainForm.MenuItem13.Visible = True
mainForm.MenuItem4.Visible = True
mainForm.MenuItem14.Visible = True
C# Code:
frmMain 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.
-
Jul 17th, 2007, 08:13 AM
#3
Hyperactive Member
Re: Directcast in C#
DirectCast doesn't exist in C#?
Just try
C# Code:
this.mdiParent.menuItem4.Visible = true;
this.mdiParent.menuItem13.Visible = true;
this.mdiParent.menuItem14.Visible = true;
» Twitter: @rudi_visser : Website: www.rudiv.se «
If Apple fixes security flaws, they are heralded as proactive. If Microsoft fixes a security flaw, they finally got around to fixing their buggy OS.
-
Jul 17th, 2007, 08:20 AM
#4
Re: Directcast in C#
 Originally Posted by RudiVisser
DirectCast doesn't exist in C#?
Just try
C# Code:
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.
-
Jul 17th, 2007, 09:00 AM
#5
Thread Starter
Fanatic Member
Re: Directcast in C#
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.
-
Jul 17th, 2007, 09:06 AM
#6
Thread Starter
Fanatic Member
Re: Directcast in C#
I tried this from your example:
Code:
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?
Last edited by Beast777; Jul 17th, 2007 at 09:29 AM.
-
Jul 17th, 2007, 09:17 AM
#7
Re: Directcast in C#
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.
-
Jul 17th, 2007, 09:24 AM
#8
Thread Starter
Fanatic Member
Re: Directcast in C#
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.
Last edited by Beast777; Jul 17th, 2007 at 09:30 AM.
-
Jul 17th, 2007, 09:37 AM
#9
Re: Directcast in C#
I agree. Great explanation.
If this is the MainMenuStrip of your form you can do something like this:
Code:
MDIParent1 frm = (MDIParent1)this.MdiParent;
frm.MainMenuStrip.Items["editMenu"].Visible = false;
-
Jul 17th, 2007, 09:55 AM
#10
Thread Starter
Fanatic Member
Re: Directcast in C#
Okay, slowly getting there. Now I have this code:
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.
-
Jul 17th, 2007, 11:56 AM
#11
Re: Directcast in C#
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.
I could be off base, but you could try something like this:
Code:
MDIParent1 frm = (MDIParent1)this.MdiParent;
MenuStrip mnu = (MenuStrip)frm.Controls["mnuLibrary"];
mnu.Items["myMenuItem"].Visible = false;
-
Jul 17th, 2007, 12:15 PM
#12
Thread Starter
Fanatic Member
Re: Directcast in C#
I have the same error with this code -
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.
-
Jul 17th, 2007, 12:24 PM
#13
Re: Directcast in C#
That last line is exactly the same as the last line you posted in post #10. That doesn't work.
Look at this again:
Code:
mnu.Items["myMenuItem"].Visible = false;
-
Jul 17th, 2007, 01:06 PM
#14
Thread Starter
Fanatic Member
Re: Directcast in C#
I just tried this:
Code:
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:
Code:
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.
-
Jul 17th, 2007, 01:10 PM
#15
Thread Starter
Fanatic Member
Re: Directcast in C#
Well this finally worked using
Code:
((ToolStripMenuItem)this.MdiParent.MainMenuStrip.Items[1]).DropDown.Items[0].Visible = true;
I would of thought there would of been an easier way.
-
Jul 17th, 2007, 02:39 PM
#16
Re: Directcast in C#
So this was your MainMenuStrip?
Anyway, glad you figured it out.
Let everybody know and mark this thread resolved.
-
Jul 18th, 2007, 06:47 AM
#17
Thread Starter
Fanatic Member
Re: Directcast in C#
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?
-
Jul 18th, 2007, 08:23 AM
#18
Re: Directcast in C#
The MenuStrip is the control that "replaced" MainMenu. MainMenu is still there, but mostly for backward compatibility: http://msdn2.microsoft.com/en-us/lib...ip(vs.80).aspx
The MainMenuStrip is a form property of which menu strip is, well, the main MenuStrip.
http://msdn2.microsoft.com/en-us/lib...ip(vs.80).aspx
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|