Code for Numericupdownextender event handler - c#

i want to change image of an image control based on the value of textbox by clicking on the ajax numericupdownextender control.Please provide solution in c# and also tell me how we can write custom code for this.

I imagine you'll want to use the currentChanged event of the NumericUpDownExtender. This fires an event client side (in javascript - not C#).
"The NumericUpDown extender has only one event, currentChanged. This event is fired whenever the value of the extended textbox is changed through the up/down buttons or when the user types a new value in that textbox. In case of typing, the event fires whenever the focus leaves the textbox."
function pageLoad(sender, args) {
$find('<%= NumericUpDownExtenderID.ClientID %>').add_currentChanged(methodtocall);
}
function methodtocall(sender, e) {
alert(String.format('change event fired. Value: {0}', e));
}
If this doesn't solve your problem then you need to be clearer in your question.

Related

Preventing a TextBox leave event when loosing focus

How to stop textbox leave event on clicking on linklable/button while Textbox was in focus?
I have a textbox TextBox1. On leave event of it, I have to validate it's text. And as per text of it, I have to populate suggestion of next textbox TextBox2.
so in this process, when I'm clicking on a link-label [which is there for different reason] while TextBox1 is in focus, It is firing Leave event [which is obvious] and calling validating functionality [which is not supposed to be called - because user have not left textbox after completing input- It was triggered by linkable click].
I have tried to unsubscribe Leave event on link-label's click event but that doesn't help as leave event is being fired in first place.
what should be done in this case ?
EDIT :
posting related code,
I'm Having same Event Handler for both textbox
private void txtBox_Leave(object sender, EventArgs e)
{
TextBox textBox = sender as TextBox;
textBox.Visible = false;
#region TextBox 1
if (textBox.Equals(txtBox1))
{
//Text Box 1 validation
//Populating Suggestions for TextBox2
//Passing focus on Textbox 2
}
#endregion
#region TextBox 2
else if (textBox.Equals(txtBox2))
{
//Text Box 2 validation
}
#endregion
}
As I've mentioned earlier, Link label is there for different and I cannot disable or hide it during operation on Text box.
One observation is,
When I click on Link label, Leave event of text box raise first and after that Click Event of Link Label, so we cannot perform any logic (like unsubscribing leave event of Textbox or something else) on Link-Label's click event - as by the time validation process would be done which we don't want.
When you click on link label, it becomes focused control. What you can do is - find the focused control of the form in textbox1_leave event handler. If it is not that link label then only the functionality for populating suggestions of textbox2 should be done.
This link should be helpful for you to find out focused control of the form - What is the preferred way to find focused control in WinForms app?

Modify element property on key press

I need to hook up an event that shows a WPF Popup control when a TextBox has focus and a keyboard shortcut is clicked. For instance. When typing in the TextBox field, the user can press ALT+H for help, to get a popup dialog showing input help. Pressing ALT+H "outside" the TextBox should not open the popup.
Any ideas?
Looks like a job for an Attached Event.
From MSDN:
The concept of
an attached event enables you to add a handler for a particular event
to an arbitrary element rather than to an element that actually
defines or inherits the event. In this case, neither the object
potentially raising the event nor the destination handling instance
defines or otherwise "owns" the event.
You can find details here, on the MSDN
Use command binding:
ApplicationCommands.Help.InputGestures.Add(new KeyGesture(Key.H, ModifierKeys.Alt));
this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Help, Help_Executed, Help_Enabled));
In function Help_Executed do some operations
In function Help_Enabled check if textbox selected, do e.CanExecute = true;
InputGestures assign ALT-H for help

Programmatically calling PreviewTextInput not invoking TextChanged event

I have a TextBox that only allows certain characters to be typed into it; I'm handling this logic in the PreviewTextInput event. If it's a character that's allowed, then the TextChanged event is fired, otherwise it cancels the TextChanged event.
I have another application that is opened on a second screen while this is running, however even if there is keyboard input while the other application is active, it should still update the currently focused TextBox on the main application. To do this, I added a listener to the OnKeyPress() event on the second application, which calls the PreviewTextInput event on the focused TextBox in the main application.
Here is the code:
private void ImageEventController_OnKeyPress(char c)
{
object focusedElement = this.CurrentKeyboardFocus;
if (focusedElement != null)
{
if (focusedElement is TextBox)
{
TextBox target = (TextBox)focusedElement;
if (target.IsEnabled)
{
string text = c.ToString();
var routedEvent = TextCompositionManager.PreviewTextInputEvent;
target.RaiseEvent(new TextCompositionEventArgs(
InputManager.Current.PrimaryKeyboardDevice,
new TextComposition(InputManager.Current, target, text)) { RoutedEvent = routedEvent });
}
}
}
}
When this is called, it goes through the PreviewTextInput event, however the TextChanged event never gets fired even if it's a valid character. Is there any reason why TextChanged is not getting fired when PreviewTextInput is invoked programmatically?
UPDATE:
I added in this code to the bottom of my PreviewTextInput event listener:
if (!e.Handled)
{
textbox.Text = e.Text;
}
This forces the TextChanged event to fire and fixes the functionality when the second application has focus, however if the main application has focus it causes two TextBoxes to get updated when only pressing one button.
I was unable to figure out how to invoke the TextChanged event from PreviewTextInput, but I did manage to accomplish what I needed to do:
Instead of performing the logic to validate the key pressed is valid inside of the PreviewTextInput event, I pulled all of the logic out and put it into a public function. Then in my ImageEventController_OnKeyPress event I am using the LogicalTreeHelper.GetParent() method to find the necessary control. From there I call the public function to validate a valid key is pressed and if it is, I call the TextInput directly.

How to disable the selection on a TextBox

I want to disable selecting text and clicking in the middle of text in a TextBox, but the user must be able to enter this TextBox and write at the end of earlier text, so I cannot make it ReadOnly or Enable = false.
I try to handle MouseDown and do the following:
input.Select(input.Text.Length, 0);
It helps with placing a cursor in the middle of text, but the user still can make a selection from the end.
I also make a MessageBox() on MouseDown event, but in this case the user cannot click on textBox and write anything.
The last try was to set a focus() in another Control and focus back, after a period of time, but it didn't work at all. User still can make a selection.
How can I do it?
How about this for Click event
Edit: Also do the same for DoubleClick and MouseLeave to cover all cases. You can have a common event handler.
private void textBox1_Click(object sender, EventArgs e)
{
((TextBox) sender).SelectionLength = 0;
}
If it fits the UI/user model, another approach is to use two text boxes: a read-only one with the previous text that the user can see and act on (if that is something he needs to do) and an editable one for the new text along with a button to commit the new text to the read-only text box (and persistence layer).
That approach is not only arguably more user-friendly—the editable box is completely editable rather than just "appendable", which gets confusing when the user hits Backspace—but also requires less fighting with the framework to make the boxes do what you need.
You're not far off with your MouseDown event handler, but probably better to catch MouseUp, as this is the event that will fire when they have finished selecting.
Alternatively, you could catch the SelectionChanged event.
Just put your:
input.Select(input.Text.Length, 0);
code in any of those event handlers.

Toolstripbutton not validating textbox

I have a textbox, a standard button and a toolstrip containing a couple of buttons.
In the validating event of the textbox I coded to check whether it is blank.
If yes then it shows a message 'Enter Value'. When the standard button is clicked while
the textbox is empty, it's validating properly and showing the message but when the
toolstripbutton is clicked it's not validating the textbox and no message is shown. It seems that I've got to write the validation code explicitly in the
toolstripbutton_click event which is too troublesome when there are multiple textboxes and toolstripbuttons on a single form.
What I want to know is whether the textbox_validating can be fired when the toolstripbutton is clicked? Handling toolstrips is really a headache.
The ToolStripItem classes are special, they don't derive from Control. One side-effect of that is that they don't take the focus away from the active control. And that prevents the Validating event from firing.
Several things you can do. You could call the textbox' parent's ValidateChildren() method. Or you could move the focus yourself:
private void toolStripButton1_Click(object sender, EventArgs e) {
btnSave.Focus();
if (btnSave.Focused) btnSave.PerformClick();
}
Write the following in toolstripbutton click event:
Me.Validate()
You can call the textbox_validating procedure from the procedure that handles the toolstripbutton click event, but you may have to add some logic to see if it passed validation before proceeding with the rest of the toolstripbutton_click event. Since you said you have a lot of textboxes to validate, you might want to consider making a Validate() function that returns true or false and checks all of the textboxes. Then all you have to do is check if Validate() = true and call the same function from all of your toolstrip buttons instead of copying the same code over and over again.

Categories