I added the event, I click in the label and press any key, but it doest go to the method. How can I capture?
I don't think labels can receive keyboard input. It will go to the control with focus, and labels can never have focus (by default, I guess you might pull some shenanigans though), most likely your events are going to your main form.
Maybe you could disguise a text box to look like a label.
To add content beyond my other comment. Have you looked at something like input bindings for what you are trying to do?
http://msdn.microsoft.com/en-us/library/system.windows.input.inputbinding.aspx
Related
I want to show some information at textBox exactly at cursor's place.Not like in the intellisense at C# :it appears where "." inserted.Please help.At picture you see it appears according to dot(".") but i want to it appear according to that which i mentioned at picture(sorry my english is bad,and i couldnt find what that means .)
What you can do is, whenever the user presses a key inside the textbox, handle the OnKeyDown event, compute the current cursor position and repaint the popup control. This popup control can be defined by you as a custom control.
Edit: as jberger pointed out, you can also implement the handler for OnSelectionChanged, to handle any cursor change.
You can use a Popup: http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.popup.aspx
This is essentially a window without border displayed above other content.
Assign PlacementMode.Mouse to the property Placement. There are several other properties to modify the popup position.
You can also use AdornerClass for creating your own custom-styled popup.
Check this link:
http://msdn.microsoft.com/en-gb/library/system.windows.documents.adorner.aspx
If you aren't used Adorner class yet, you can start with: http://msdn.microsoft.com/en-us/library/ms753340.aspx
I have a feature request that when a ComboBox is 'clicked into' that it clears the text so that the user can start entering in new data to search. Does anyone know of a way to hook into this? The 'click' event is raised on when the text is clicked as well as when the drop down arrow is also clicked (which opens up the drop down with items). I only want it to happen on the first, not the latter.
Right now I'm capturing the click event and filtering on the DroppedDown property like so:
if(!comboBox.DroppedDown)
{
// clear selection
}
This seems to work most of the time, but bugs out frequently as well... so its not 100%.
If anyone knows of a proper way to do this I would appreciate!
Don't handle the click event. For one thing, it won't fire if the user tabs the focus into the control. Use the Enter event which fires when the control receives focus. And rather than clearing text you should just select it all which will give the best of both worlds:
1) The user can start entering new text which will clear any old text or
2) tab past the control and leave the contained text as it was.
If you always remove the previous text you may anger users.
Try the "Enter" event. It happens when a control gains focus on the form.
I'm wondering how to filter the input of a .NET textbox.
I already know that I could listen for the KeyDown event and intercept the key, but that won't filter pasted strings by a right-click menu or a CTRL+V.
I also don't wan't to completely disable the possibility of pasting of characters in the textbox. The paste action should be cancelled whenever it contains one or more invalid characters.
Finally, I'd like to display a notification balloon whenever invalid characters are either entered or pasted.
μTorrent already has this exact behavior:
How can I achieve this functionality in C# ?
TextChanged event - Seems like a good call.
You can spawn your own baloon or ToolTip on any control you want to show a detailed feedback to the user
It seems like a combination of KeyPress, TextChanged, Validating, and Validated events should work for your purposes.
I'm currently updating it on click, but this results in the user being able to see the repopulation occur. Which other event can I use which will allow me to handle it myself, then show the combobox when i'm ready? ( after population)
I don't know what you're developing, but that combobox is probably on a window or so that will have an event that fires on show. Use that event to populate the combobox in.
[edit] Ah Winforms. Use the Load event.
[edit2] On each click eh.
Alright. I found a dirty solutions that advises you to override the WndProc and capture messages, but I think it's better to inherit the combobox and override OnDropDown to perform you populating before calling the ancestor's OnDropDown method.
You should populate the box when entered too, because a value may be selected using the keyboard (arrows) without even dropping down the box. You'll need both if you want it on each selection, because a click only causes the Enter event when the box didn't have focus before.
Have you tried the ComboBox.DropDown Event?
You could try to call SuspendLayout() before updating and calling ResumeLayout() after the changes.
combobox.add_HandleCreated triggers after the control is created as the form is loading
I have a UserControl that consists of three TextBoxes. On a form I can have one or more or my UserControl. I want to implement my own tab behavior so if the user presses Tab in the second TextBox I should only move to the third TextBox if the the second TextBox has anything entered. If nothing is entered in the second TextBox the next control of the form should get focus as per the normal tab behavior. If the user hasn't entered anything in the first or second TextBox and the presses tab there is this special case where a control on the form should be skipped.
By using the ProcessDialogKey I have managed to get it work kind of ok but I still have one problem. My question is if there is a way to detect how a WinForms control got focus since I would also like to know if the my UserControl got focus from a Tab or Shift-Tab and then do my weird stuff but if the user clicks the control I don't want to do anything special.
As a general rule, I would say overriding the standard behavior of the TAB key would be a bad idea. Maybe you can do something like disabling the 3rd text box until a valid entry is made in the 2nd text box.
Now, having said this, I've also broken this rule at the request of the customer. We made the enter key function like the tab key, where the enter key would save the value in a text field, and advance the cursor to the next field.
I don't think there's a built-in way that you could do it. All of the WinForms focus events (GotFocus,LostFocus,Enter,Leave) are called with empty EventArgs parameters, which will not give you any additional information.
Personally, I would disable the third textbox, as Rob Thomas said. If you're determined to do this, though, it wouldn't be difficult to set up a manual (read: hackish) solution. Once the tab key is pressed (if the focus is on the second textbox), set a variable inside your form. If the next object focused is then the third textbox, then you know exactly how it happened.
The reason for this odd tab behavior is all about speed in the input process. It was really good to get some input, I hadn't thought about disabling a textbox but that could actually work. But using the Enter key to accept the input hadn't even crossed my mind. That will work so much better. The user can enter the numbers and then press enter to accept the input and the next possible textbox will be the active one. It's like having the cake and eating it too, The speed factor is there since when using the enter key no unnecessary tabing must be done to get to the correct field and using the enter key next to the numeric keyboard makes it really smooth.
Thanks for the input!
I agree with DannySmurf. Messing with the tab order might give you hell later on if the requirements for the application change.
Another thing that you could do is to implement some kind of wizard for the user to go through.
Better than disabling controls, try monkeying around with TabStop - if this is false, the control will be simply skipped when tabbing.
I'd also suggest that the Changed event of the TextBox is the place to be updating TabStop on the other controls.
I've done something similar to this with a login control, where users could enter either a username or an email address (in separate fields), plus their password, and tabStop is what I used to get the job done.