Yes I'm quite noobish I've been learning C# by myself reading various material I can scrounge online but I've run into a small issue while trying to write a small program.

I'm simply trying to use a menuitem as a toggle between various instructions. The basic idea is simply to make text visible or invisible depending on the menuitem that's being clicked. I've solved the problem but the solution requires a lot of repetitive code and I'm trying to avoid that.

What I don't understand is why this piece of code isn't working

private void onImsToolStripMenuItem_CheckChanged(object sender, EventArgs e)
        {
            
            onImp1ToolStripMenuItem.Checked = false;

            if (onImsToolStripMenuItem.Checked)
            {
                lblOnIms.Visible = true;
            }
            else
            {
                lblOnIms.Visible = false;
            }

Essentialy, the problem is that it stays visible. Once I choose a seperate selection the label (lblOnIms) stays visible. I've worked around this by simply adding code lblOnIms.Visble = false; at the beginning of the checkedstate of the next item but that would require me to write that for every option in every menu and it's going to be a large menu so I see this as being extremely inpracticle and there must be a better way I just don't know it. anyone have any ideas what I'm doing wrong?

Thanks for any responses :) I knwo it's probably a pretty noobish question but it's like I said, I'm self taught so blame the teacher lol

Hi Kracus, welcome on DANIWEB!
Please, if you post code use code tags!
I think we need a bit more code to see what is actually wrong.

Hmmm I thought I had used the Code tags around my code...

The whole code is this

namespace Cheat_Sheet_Project
{
    public partial class xeroxReferencePage1 : Form
    {

        public xeroxReferencePage1()
        {
            InitializeComponent();
        }

        private void toolTip1_Popup(object sender, PopupEventArgs e)
        {
        }

        private void onImsToolStripMenuItem_CheckChanged(object sender, EventArgs e)
        {
            
            onImp1ToolStripMenuItem.Checked = false;

            if (onImsToolStripMenuItem.Checked)
            {
                lblOnIms.Visible = true;
            }
            else
            {
                lblOnIms.Visible = false;
            }
            
        }

        private void onImp1ToolStripMenuItem_CheckChanged(object sender, EventArgs e)
        {
           
            onImsToolStripMenuItem.Checked = false;

            if (onImp1ToolStripMenuItem.Checked)
            {
                lblOnImp1.Visible = true;
            }
            else
            {
                lblOnImp1.Visible = false;
            }
            
        }

      
    }  
}

I left out the namespaces but it's just the default ones that come up when creating a windows forms. As you can see there's only 2 menu items right now but there's going to be much much more. I'm trying to avoid having to type out each label.visisble = false for each menu item I place. That's why I'm trying to go with the format I've listed above in order to make the code a bit cleaner rather than cut & paste and repeat the code for every menu item.

I remember having a similar problem with something like this. As Far as I remember, true and false cannot be properly interpreted. Try something like lblOnIms.Visibility = Visibilty.Visible .
That's how I got mine working :)

Hope that helps
M

Actualy when trying to do this I actualy attempted something like that but failed miserably, I think it's cause I'm not sure on how to define

Visibility

. I tried various things but I'm not quite adept enough to figure that out.

Would be something like

bool Visibility();

near the top of the program?

No Visibility shouldn't need to be defined. As far as I know it's one of the classes predefined in all your UIs. Without that Visibility.Visible should work fine. At least it did for me. If you do not declare Visibily at the top (please please don't) then when you type the word Visibility it should be in light blue, indicating it is a regognised class. If this doesn't happen, just let me know, I'll check it out

M

No it definitely is not recognized as a class. What I had tried earlier was to define one. I think I had done something like

private void onImsToolStripMenuItem _CheckedChanged (object sender, EventArgs e)
{
  onImsToolStripMenuItem item = (onImsToolStripMenuItem)sender;
  lblOnIms.Visible = item.Checked;
}

but I ran into problems because what I posted above is a code snipet I dug up from the research I'd been doing to solve my problem. it says the menu item is a field but used like a type?

Thanks a lot for the help btw, I've been trying to solve this myself for the last 2 days with limited success. the only solution I've come up with is to write the code to mark every label false for every menu item, as you can imagine, for every 10 items you need to write the same code 100 times, I'm sure there has to be a better way :)

Okay I admit that certain types of applications seem unable to get a hold of UIElement.Visibility. I've seen my WCF services can't use it. Not really surprising, they have no UI elements. But I'm still surprised. What kind of project did you start?

Okay that error means that trying to cast something improperly. The idea is sound though. Maybe try (MenuItem)sender as opposed to (ToolStripMenuItem)sender . I suspect that ToolStripMenuItem is not being recognised as a proper class, and you can only convert to classes and primitives.

Let me know if you make any progress
M

Okay I admit that certain types of applications seem unable to get a hold of UIElement.Visibility. I've seen my WCF services can't use it. Not really surprising, they have no UI elements. But I'm still surprised. What kind of project did you start?

Just a windows forms project.

Okay that error means that trying to cast something improperly. The idea is sound though. Maybe try (MenuItem)sender as opposed to (ToolStripMenuItem)sender . I suspect that ToolStripMenuItem is not being recognised as a proper class, and you can only convert to classes and primitives.

Let me know if you make any progress
M

Sorry I actualy misled you there by accident. See I got the code snipet from a book about C# I found online that was talking about toolstripmenuitems but that's not actualy what I'm doing. I was just trying to replicate the toggle functions of the toolstripmenuitem.

I corrected the code I actualy wrote afterwards after I noticed the error but not before your post.

Okay the problem is an improper cast. You're trying to cast it to onImsToolMenuItem. Unfotunately, this is a function, not a class. That's why you're getting this error. Try (ImsToolMenuItem)sender .
Not sure it'll solve the entire problem, but it should get rid of the error.
I'm not sure why a windows form does not have visibility defined

Perhaps I did it wrong but I'm still getting the same error

"Is a field but used like a type"

Unfortunately(or fortunately depending on which way you look at it) I'm done work for the day and beleive it or not I don't even have a pc at home lol... I'm too poor, I program in my spare time at work whenever I have the time lol... anyway I'll be back tomorrow and check this thread. Thanks for the help!!!

Just wish I could've helped. I can't see what's wrong. Hope some supersmart person can help you out :)
Peace out
M

lol thanks anyway, any help is good as far as I'm concerned, I'm more interested in learning what I'm doing wrong anyway, all examples are helpful :)

From post#1 >>>Essentialy, the problem is that it stays visible

The problem has nothing to do with if else but is an event problem.
The CheckedChanged events only get executed when you change the checked property of the menutoolstrip.
You can do this by implementing the Click events like this(pseudocode):
menuitem.Click(obj sender...)
{
this item checked = !(this item checked )
}
Hope it helps.

Well the idea is to have the label stay visible only when the menu item is checked. That code unchecks the menu item instantly which is, in itself, a cool trick to know but it still doesn't make the label dissapear with the check.... I tried some variations to get this done on what you posted but I'm still getting the same problem that I'll have to type out label1.visible = false for every menu item. It wouldn't be a problem if there were only a few items but there's going to be a lot. I'm starting to lean towards making a loop of somekind that constantly checks what menu item is checked and just making visible the correct label that corresponds to the right menu item that's checked...

>>>it still doesn't make the label dissapear with the check
It should, because after the Click event changes the checked property, the ChangeChecked event gets fired.

Sorry, it's probably me that's doing something wrong, although I didn't use your exact code I did have used it and I've just come up with a solution to my problem.

namespace XeroxReferenceProject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }
        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            label1.Visible = false;
            label2.Visible = false;
        }

        private void onImp1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            onImp1ToolStripMenuItem.Checked = !(label1.Visible = true);
        }

        private void onImsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            onImsToolStripMenuItem.Checked = !(label2.Visible = true);
        }

    }

}

I didn't realize the entire menu can be used as a clickable item so I'll simply indicate on activation of the menue to clear everything. This works great actualy so far and I won't have to type out for EACH item repteatedly just the one time and it's great! thanks for hte help!!! :)

Are the menu items "checked" state mutually exclusive? If so (like radio buttons), then the "CheckChanged" event fires more often than you think.

You could try checking the "sender" parameter to ensure that it is the actual menu item raising the event.

private void onImsToolStripMenuItem_CheckChanged(object sender, EventArgs e)
{
   if(sender.Equals(onImsToolStripMenuItem)
      lblOnIms.Visible = onImsToolStripMenuItem.Checked;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.