Solved

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

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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
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 video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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