Solved

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

Posted on 2011-09-26
5
265 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now