[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-09-26
5
Medium Priority
?
315 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 1000 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month20 days, 4 hours left to enroll

872 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