Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

RobHat (TechnicalUser) (OP)
13 Jun 11 9:08
Hi guys,
 Hope someone can help me with this. I can only assume its a relativly simple one to solve. I have a visual c# system which I have taken over (with very little c# experience). I am trying to disable a (right click) menu item when there is a date present in the SQL database record.

I have found the click event for the menu item, is this where i will need to disable it? I presume that I need to create an SQL query to find whether the date is there and if so with an 'if' statement disable it?

Sorry if there is a lack of info, if you need any additional info please let me know.
Helpful Member!  robertfah (Programmer)
13 Jun 11 9:32
Is the menu item something that you/the program created (a custom one) or is it a built-in one that the control uses?  You can disable the right-click functionality all together (after you've done some previous checks for data in the click event) but I'm not sure if you want to just disable 1 menu item (and leave the rest enabled) or prevent the menu from showing at all.
RobHat (TechnicalUser) (OP)
13 Jun 11 9:38
Hi, Thanks for the speedy reply. IT is a custom right click menu that has a few options. some of which are relavent still but the one I want to gray out/disable for instance is Register. I want to disable this option if there is a registration date present in the table. Does that make sense??? sorry if not. I would post some code but I dont really have any as yet other than the standard click event.

Rob
RobHat (TechnicalUser) (OP)
13 Jun 11 9:43
Just to add to that, there is already a query to pull up all the client details for this form based on the Persons ID which is passed from a previous search. Whether this same query can be used or whether it would be easier to creat a smaller more specific one I dont know.
robertfah (Programmer)
13 Jun 11 9:56
Ok, then it sounds pretty simple.  In the click event, you'll need to find out what button the user clicked.  That information is held in the MouseEventArgs parameter of rhe MouseDown event for that control.  Here's what your code may look like (note that the control name will be different than yours):

CODE

        private void simpleButton1_MouseDown(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Right)

        }

Once you check for this, all you have to do is disable the menu item in your contextMenu control, like so:

CODE

        private void simpleButton1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
                toolStripMenuItem2.Enabled = false;
        }

The reason you use the MouseDown event instead of the MouseClick event is because the MouseDown event will fire BEFORE the click event.  Once the Click event is fired, the context menu will show no matter what so you want to capture and process your code before the event happens.

Obviously you still need to put your code around this that will check if the current row has data or not but this will resolve your right click menu options.

Hope this helps!
robertfah (Programmer)
13 Jun 11 10:05

Quote:


 In the click event, you'll need to...

I meant to change this to the MouseDown event, not click event.
RobHat (TechnicalUser) (OP)
13 Jun 11 10:17
Thanks for that, I see what you mean however would that not disable the entire right click functionality? I currently have this:

private void RegisterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Opacity = .1;
            frmActivity newContact = new frmActivity();
            newContact.Activity_Type = 36;
            newContact.ShowDialog();
            this.Opacity = 1;
        }

This as far as I can see is the left click event for selecting "Register" from the right click context menu strip. I only want to disable this event as other items in the right click menu are relevant.

Also how would I go about writing the If statement and SQL to decide whether the date is already there.

Thanks for you help with this I know I am being a pain and I promise I have spent many hours today trying to figure this out with books etc but just cant get it.
robertfah (Programmer)
13 Jun 11 10:35
No worries...you're not being a pain.

A right click menu is made up of 2 controls really; a ContextMenuStrip control and a ToolStripMenuItem.  The ContextMenuStrip is the placeholder for all the right-click menu options to appear.  So if you put a ContextMenuStrip on your form and assign it to a control, it won't work because there are no menu items in it to display.  This is where the ToolStripMenuItems come in.  They are the menu items that you see when you right click.  So a right click menu must consist of at least 1 ToolStripMenuItem.  Not too sure what yours looks like (as far as what menu items are there) but I'll take a basic Windows right-click menu as an example.

If you open NotePad and just right click anywhere in the program, you get menu that looks like this:

Undo
------
Cut
Copy
Paste
Delete
------
Select All

And probably some more that I don't need to list.  Each menu item you see there is a ToolStripMenuItem.  You can access each of these control properties directly in your code and choose to enable or disable them.  If you Enable/Disable the ContextMenuStrip then the whole right click menu will be disable (which is what I think you did).  What you want to do is disable the specific ToolStripMenuItem on your ContextMenuStrip.  To find out what that control's name is, in design mode, open the form and find the ContextMenuStrip and click on it once.  It should open the menu with all the mene options on it.  You can click on the "Register" menu item and look at its name in the properties window.  Once you have it, that is the name to use in code to disable or enable that specific menu item.
RobHat (TechnicalUser) (OP)
13 Jun 11 11:23
Thanks, I have had a look and sure enough the item i need to disable is called "RegisterToolStripMenuItem" as above. I have just as a test changed the code from:

private void RegisterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Opacity = .1;
            frmActivity newContact = new frmActivity();
            newContact.Activity_Type = 36;
            newContact.ShowDialog();
            this.Opacity = 1;
        }

to:

private void RegisterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RegisterToolStripMenuItem.Enabled = false;
        }

This caused the Register menu item to change to disabled after being clicked. I can see why this happened as the disable property has been placed inside the click event. Does this mean that I would need to structure my code like this roughly:

if (*date present*)
      RegisterToolStripMenuItem.Enabled = false;
else
      private void RegisterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Opacity = .1;
            frmActivity newContact = new frmActivity();
            newContact.Activity_Type = 36;
            newContact.ShowDialog();
            this.Opacity = 1;
        }

Again sorry if this seems a stupid question but c# is completly new to me however I need to get this disabled.
RobHat (TechnicalUser) (OP)
13 Jun 11 11:57
ok, forget my previous post as I have now sorted it. I know where I need to put the disable in to get the desired result. The menu looks like this:

<right click the persons details>
Registration >
Demographics
Events
etc

If you hover or click the Registration text it takes you to:

Register

I have added the follwoing to the registraion> link temporarily to check the behaviour and it works perfectly. My temp disable code looks like this:

//
// toolStripMenuItem3
//
this.toolStripMenuItem3.Enabled = false;
this.toolStripMenuItem3.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.RegisterToolStripMenuItem});
this.toolStripMenuItem3.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold);
this.toolStripMenuItem3.Name = "toolStripMenuItem3";
this.toolStripMenuItem3.Size = new System.Drawing.Size(183, 22);
this.toolStripMenuItem3.Text = "Registration";
this.toolStripMenuItem3.Click += new System.EventHandler(this.toolStripMenuItem3_Click);

Could I do this:

//
// toolStripMenuItem3
//
if(*DatePresentInTable*)
{
this.toolStripMenuItem3.Enabled = false;
}
this.toolStripMenuItem3.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.RegisterToolStripMenuItem});
this.toolStripMenuItem3.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold);
this.toolStripMenuItem3.Name = "toolStripMenuItem3";
this.toolStripMenuItem3.Size = new System.Drawing.Size(183, 22);
this.toolStripMenuItem3.Text = "Registration";
this.toolStripMenuItem3.Click += new System.EventHandler(this.toolStripMenuItem3_Click);
robertfah (Programmer)
13 Jun 11 12:49
Yes you could but you will also want to add an ELSE to that and enable the control, like so:

CODE

if(*DatePresentInTable*)
{
this.toolStripMenuItem3.Enabled = false;
}
else
{
this.toolStripMenuItem3.Enabled = true;
}

The reason for this is if there is data the first time the user tries to right click, the menu will obviously work correctly.  If there is no data and the user right clicks, it will disable the menu item, which is what you want.  If the user then selects something else and it has data and tries to right click, the menu option will still be diabled (from the previous click action) unless you reset it to enabled.
RobHat (TechnicalUser) (OP)
13 Jun 11 16:02
Thanks for that. Sorry to push my luck but any hints on the If statement and how to set the query up?

Thanks again for all your help today. Really appreciated.
RobHat (TechnicalUser) (OP)
15 Jun 11 5:02
Hi,
SO I now know how to get the menu item disabled but am stuck linking it to a query or similar to determine whether there is a date present in the record.

I so far have this:

private void InitializeComponent()
{
   if ()
       this.ToolStripMenuItem.Enabled = false;
   else
       this.ToolStripMenuItem.Enabled = true;
}

There is loads more code within the InitializeComponent but I have cut it out but from what I can see it just outlines the properties for all the oobjects on the form.

I am familiar with Coldfusion in which I would have created a <cfquery> to house my sql query in the head and then set the if statement to look at the results of that query. How would I do this in C#??? Following on from other code within this project I have tried to create an sql statement above this and then reference it but I am obviously missing a critical element somewhere.

Thanks for any advice
RobHat (TechnicalUser) (OP)
15 Jun 11 9:58
Ok finally sorted. in aid of finishing the thread and for future reference:

I had been writing my code in the wrong place and needed to define a new event for the menu. The completed code is as follows:

private void contextMenuStrip1_Opened(object sender, EventArgs e)
{
   string ssql = "";
   ssql = ssql + " SELECT Date ";
   ssql = ssql + " FROM person ";
   ssql = ssql + " WHERE person_id = " + person_id;
   ssql = ssql + " AND Date NOT LIKE 'NULL'; ";

   OleDbDataAdapter myAdapter = new OleDbDataAdapter(ssql, clsGlobal.connectionString);

   DataSet rs = new DataSet();
   myAdapter.Fill(rs);

   if (rs.Tables[0].Rows.Count > 0)
   {
      this.ToolStripMenuItem.Enabled = false;
   }
   else
   {
      this.ToolStripMenuItem.Enabled = true;
   }
}
RobHat (TechnicalUser) (OP)
15 Jun 11 10:01
Also just want to add a thanks to Robertfah for all the help. Most appreciated.
robertfah (Programmer)
15 Jun 11 10:12
Glad to see you were able to find a solution.  Sorry I couldn't have helped on that but I wasn't working yesterday.

Happy programming!
ShankarJ (Programmer)
15 Jun 11 12:10
Hi!

Not sure what database you are querying but since NULL is not a specific value, you could change

 ssql = ssql + " AND Date NOT LIKE 'NULL'; ";

to

 ssql = ssql + " AND Date IS NOT NULL; ";

Regards
 

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close