Solved

How do I pass the control that has focus to a function?

Posted on 2011-09-26
5
288 Views
Last Modified: 2012-05-12
I have a number of datagridview controls on a form and I want to use context menu strip to be able to add or remove rows in the forms. I am able to pass the control to the function and get the row added but I want to be able to pass the DGV that the context menu was accessed from to the control...can that be done?
0
Comment
Question by:SheldonC
  • 3
  • 2
5 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 250 total points
ID: 36600294
Do not detect the control that has the focus, it would not work. The user can call the ContextMenuStrip on another control than the one that has the focus.

Instead, grab the control on which the ContextMenuStrip was activated with the following code in the Click event of the menu item:

DirectCast(DirectCast(sender, ToolStripMenuItem).GetCurrentParent, ContextMenuStrip).SourceControl

Since your context menu opens on a DataGrid, you will have to add one level of redirection (SourceControl can be any control):

DirectCast(DirectCast(DirectCast(sender, ToolStripMenuItem).GetCurrentParent, ContextMenuStrip).SourceControl),DataGridView)

There is you datagrid.
0
 

Author Comment

by:SheldonC
ID: 36600372
I'm just an intermediate...would you mind explaining that code a bit?
0
 

Assisted Solution

by:SheldonC
SheldonC earned 0 total points
ID: 36600401
I was able to figure it out. I just passed cmsDGVMenu.SourceControl to my function.  cmsDGVMenu being the name of my contextmenustrip

0
 

Author Closing Comment

by:SheldonC
ID: 36895862
The suggestion by the other user guided me to the correct solution to my issue. I posted my exact solution for reference by any other users who may have a similar issue.
0
 
LVL 40
ID: 36601202
I did not have you exact code, so I did not know that you could get cmsDGVMenu in the place where you were handling the job.

My code was from an event that could be called from many different menus (context and standard), so I had to go a little further and identify wich of the menus had made the call. That part DirectCast(DirectCast(sender, ToolStripMenuItem).GetCurrentParent, ContextMenuStrip) enabled me to get the menu that called the event.

Since in your situation you know specifically which menu it is, you do not need such a complex command and can go more directly to the SourceControl.

Always post some code when you ask a question. If you had showed me precisely where you were handling the the call, I would probably have come up with something closer to what you exactly needed.

But you were able to clear it out, this is the important thing, isn't it.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question