Solved

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

Posted on 2011-09-26
5
274 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Best way to handle data being sent over a serial COM 5 31
Multiple file Upload asp.net 2 38
System32Int Error 8 56
Disable and re-enable a dynamic System.Timers.Timer 6 39
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

914 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

17 Experts available now in Live!

Get 1:1 Help Now