Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select textbox on right-click 1

Status
Not open for further replies.

itn04

IS-IT--Management
Aug 20, 2004
9
US
How can I make a textbox the active control when the user right-clicks over the control? I have a context menu that pops up when the user right-clicks. One of the menu items changes the font in the selected textbox to have a line through it (strikeout). If the user left clicks or tabs to the appropriate textbox and then right clicks it works great. However if the focus is in a different textbox when they right-click the wrong textbox gets the strikethrough. I have captured the click event and I can display a messagebox when the user clicks the right mouse button, but I can't figure out how to select the correct textbox.

Thank you in advance for your help.
 
Just a shot in the dark, but would something like this work in each text box's mousedown event?

Code:
if(e.Button == MouseButtons.Right)
{
     myTextBox.Focus();
}

Hope this helps,

Alex



[small]----signature below----[/small]
I don't do any programming whatsoever

Ignorance of certain subjects is a great part of wisdom
 
Thanks for the help Alex. That would definitely do it if I hard code each textbox's mousedown event but there are twelve of them. I was hoping I could write one method and assign it to each of the textbox's mousedown events.

this.tb_one.MouseDown += new MouseEventHandler(this.OnMouseDown);

I did that for each textbox but inside of OnMouseDown I can't figure out which control is raising the event.

Is there a way to find a control by location based on the mouse location?

Thanks again.
 
Hm, if you want it to apply to the form it could get interesting. I'm not sure if you'd have to write some hit-testing code to check if the mouse's coordinates were within the boundaries of a text box or if C# provides something like this for you.

Out of curiousit, how do you disable the windows context menu? If I can get rid of that, then I can play with this on my own a little bit.

Good Luck!

Alex

[small]----signature below----[/small]
I don't do any programming whatsoever

Ignorance of certain subjects is a great part of wisdom
 
I didn't actually disable the Windows context menu. I created my own context menu and assigned it to each of the textbox controls which replaces the Windows menu.

You may have solved my problem though.

I just went back and removed the custom context menu from one of the textboxes and the right-click actually sets the focus to the control before it displays the Windows context menu.

I will either try to append my custom menu items to the Windows context menu or maybe there is a property on the context menu control that I am missing.

Thanks again.

Jeremy
 
When I add my own context menu, the only way I can get the right click to work to select the text box is to add this code to the MouseDown event for each text box:

Code:
if(e.Button = MouseButtons.Right)
{
     ((Control)sender).Focus();
}

I think this is generic enough that you could add it to all your text boxes programmatically without too much difficulty.

I also tried a similar approach to what you are trying, but found that the container's MouseDown event does not appear to fire when the mouse is over a control within the container. So I still needed to create a MouseDown event handler for each control.

I think this is a good thing though, because why have the event firing for a right click anywhere on the form if you only want to do anything when you're over a text box?

Hope this helps,

Alex

[small]----signature below----[/small]
I don't do any programming whatsoever

Ignorance of certain subjects is a great part of wisdom
 
Thanks again for the help Alex.

I'm pretty close to figuring it out. There is actually an opening event for a context menu. One of the members of a context menu is SourceControl. So I have captured the control that has initiated the context menu and I can select it before the menu pops up. Which is what I wanted, but the boys in Redmond are making me look bad because when their context menu pops up it actually freezes the cursor in the correct textbox. This gives the user a little security to know which textbox they are going to effect with the menu item.

My cursor only flashes for a millisecond in the correct textbox. I had to call MyForm.Refresh just to get the flash of the cursor to happen.

Any ideas on freezing the cursor while the shortcut menu is displayed?

Thanks,

Jeremy

 
I don't know about that, but why not highlight it while the ContextMenuStrip is open?

Something like this:

Code:
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
     ((ContextMenuStrip)sender).SourceControl.Select();
     ((ContextMenuStrip)sender).SourceControl.BackColor = Color.BurlyWood;
}

private void contextMenuStrip1_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
     ((ContextMenuStrip)sender).SourceControl.BackColor = Color.White;
}

Hope it help,

Alex

[small]----signature below----[/small]
I don't do any programming whatsoever

Ignorance of certain subjects is a great part of wisdom
 
Great idea!!

Now the user can be confident about which textbox they are going to effect with the menu item.

Thanks for the help Alex.

Jeremy
 
12 text boxes can lead to 1 event handler.

Select all 12 textboxes, switch to the event properties page (the lightning bolt) and double click the MouseDown event.

all 12 textboxes will now go to that one event handler on mousedown.

at that point you can do focus on Sender (the textbox making the request)


private void textbox_MouseDown(object sender, MouseEventArgs e)
{
((Control)sender).Focus();
}

 
Wow, I never would have tried that. Star for you, Jurky!

[small]----signature below----[/small]
I don't do any programming whatsoever

Ignorance of certain subjects is a great part of wisdom
 
Man that works really well. Thanks JurkMonkey. I had hard coded the eventhandler on all twelve textboxes:

this.tb_one.MouseDown += new MouseEventHandler(this.OnMouseDown);

Your way is much easier and cleaner.

Both of you guys used ((Control)sender). What is that called? I understand that sender is an object passed to the method as an argument but how did you know to put the (Control) in front of it. It may be a dumb question but I'm converting over from Powerbuilder and I didn't know you could do that.

Thanks.

Jeremy


 
it is known as Casting.

Be careful when you use it as it can be slow and cause exceptions.

The other option is to say

Control c = sender as Control.

the as keyword will attempt to cast the object for you or make the object null rather than throw an exception.

Personally, I prefer my application blow up in my face where the problem is rather than set the value to null and let it continue on for a while.

 
Personally, I prefer my application blow up in my face where the problem is rather than set the value to null and let it continue on for a while.

I would give you another star if I could :)

Jeremy - another thing to note is why you need to cast it to a control. When working with the object, you really only have the bare bones methods and properties available (ones that every object has). So, if you continue working with sender as an object, you will not be able to do things like select or change background color.

Casting it to a control will let you access all of this, but just be careful that you don't add the same code to something that is not in fact a control. Because you will only be calling this from text boxes, you could also cast it to a text box, but I prefer to keep it as generic as possible (while still getting what I need out of it)

Glad you've got it working now (I think the highlighting looks a little nicer than when it just shows the cursor there too :) )

Alex



[small]----signature below----[/small]
I don't do any programming whatsoever

Ignorance of certain subjects is a great part of wisdom
 
Thanks for the info. guys.

I've used Casting in C# but I would have never figured out that you could use it in this context.

Jeremy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top