How can I raise an event while clicking a textbox? I'm having trouble finding references for events for WPF in C#.
The idea is to have textboxes fire an event when clicked. For example, let's say as soon as I click a textbox, notepad is executed.
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
hello = Process.Start("notepad");
}
private void Click(object sender, MouseButtonEventArgs e)
{
/* if (e.LeftButton == MouseButtonState.Pressed)
{
hello = Process.Start(#"notepad");
}*/
}
For text events use TextInput event and read entered character from e.Text
private void yourTextBox_TextInput(object sender, TextCompositionEventArgs e)
{
if (e.Text == "K")
{
}
}
for mouse events use MouseDown/MouseUp
Sometimes MouseDown/MouseUp won't work on TextBox, then use this one:
http://msdn.microsoft.com/en-us/library/system.windows.uielement.previewmouseup.aspx
MouseLeftButtonDown event can be raised when clicking on textbox. This event will not fire by default on textbox. We need to use UIElement.AddHandler().
For e.g:
XAML:
<Textbox X:Name="Name_Textbox" MouseLeftButtonDown="Name_Textbox_MouseLeftButtonDown"/>
In the class Constructor:
TextBox_Name.AddHandler(FrameworkElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler("Name_Textbox_MouseLeftButtonDown"), true);
Add Event in class file:
private void Name_Textbox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// Your logic on textbox click
}
Related
I am currently using winforms and I want to send the text from a textbox to a rich text box by pressing enter. Here is the code I currently have
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
richTextBox1.Text = textBox1.Text;
e.Handled = true;
}
}
What am I missing? Thanks in advance.
Did you subscribe to the event KeyDown? You can add a break point in textBox1_KeyDown to check it.
If the break point is not triggered, you can delete textBox1_KeyDown and double click the Button to re-generate it. Then will subscribe to the event "KeyDown" automatically.
Another way is that you can subscribe to events through code.
public Form1()
{
InitializeComponent();
textBox1.KeyDown += textBox1_KeyDown;
}
I know from this question how to handle a drag&Drop
https://stackoverflow.com/a/17872857/982161
but I can not detect when the Drag event begins so I can prepare some resources...
if I print those events the Drop is coming first and after that the Drag..
how can this be cleanly handled
My Code is pretty simple
private void Label_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
var lbl = (Label)sender;
DragDrop.DoDragDrop(lbl, lbl.Content, DragDropEffects.Move);
Console.WriteLine("Drag...");
}
private void Label_Drop(object sender, DragEventArgs e)
{
Console.WriteLine("Drop...");
}
private void Label_DragEnter(object sender, DragEventArgs e)
{
Console.WriteLine("Label_DragEnter...");
}
private void Label_DragLeave(object sender, DragEventArgs e)
{
Console.WriteLine("Label_DragLeave...");
}
Long story short: If you want to prepare resources before you drop the label, write that code before calling the DragDrop method or in the OnPreviewMouseDown event.
Long Story:
Using Snoop I was able to look into the events that are triggering when dragging the label.
It appears that the only events triggering are the PreviewMouseDown and MouseDown.
So we should only implement those events.
private void Lbl_OnMouseDown(object sender, MouseButtonEventArgs e)
{
var lbl = (Label)sender;
DragDrop.DoDragDrop(lbl, lbl.Content, DragDropEffects.Move);
Console.WriteLine("Drag...");
}
private void UIElement_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
Console.WriteLine("Label_PreviewMouseDown...");
}
This will result in first printing "Label_PreviewMouseDown..." when starting to drag the label and "Drag..." when the label is done being dragged.
However, this isn't the complete truth.
Let's modify our code a little. Let's add DateTime.Now.Second to test when the messages are actually triggering. I will then drag the label for a few seconds, then drop it to see the order of printing to console.
private void Lbl_OnMouseDown(object sender, MouseButtonEventArgs e)
{
var lbl = (Label)sender;
Console.WriteLine("Label_OnMouseDown_BeforeDragging..." + DateTime.Now.Second);
DragDrop.DoDragDrop(lbl, lbl.Content, DragDropEffects.Move);
Console.WriteLine("Label_OnMouseDown_AfterDragging..." + DateTime.Now.Second);
}
private void UIElement_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
Console.WriteLine("Label_PreviewMouseDown..." + DateTime.Now.Second);
}
Now let's try dragging again.
Turns out that OnMouseDown happens before you are done dragging. The DoDragDrop method pauses the code there until you drop the label, then you are able to continue and print to the console.
So therefore: If you want to prepare resources before you drop the label, write that code before calling the DragDrop method or in the OnPreviewMouseDown event.
Hope this helps.
If you need a drag event that is not provided by the control then you may consider adding one to your own code. For example
// Event fired immediately before DragDrop
public DragEventHandler DragBegin { get; set; }
private void Label_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
var lbl = (Label)sender;
// Create args object and fire event if not null
var args = new DragEventArgs(new DataObject(lbl.Content), DragDropKeyStates.None, DragDropEffects.None, lbl, e.GetPoint(lbl));
DragBegin?.Invoke(sender, args);
DragDrop.DoDragDrop(lbl, lbl.Content, DragDropEffects.Move);
Console.WriteLine("Drag...");
}
You could then bind to that event from anywhere you have a reference to that code behind, such as
MyUserControl.DragBegin += (sender, args) => /* some behavior */;
I want to make a click event for a bunch of buttons. The problem is that I want to use the button's Text, and pass it to a function. Now the click event is passed a object sender. When I tried changing that to Button sender, it gave errors. But I don't know how else I can work with the senders Text.
Here is the normal code, which gave a single error:
private void guess_Click(object sender, EventArgs e)
{
guess(sender.Text);
}
I changed it to this, which gave errors:
private void guess_Click(Button sender, EventArgs e)
{
guess(sender.Text);
}
Question:
How can I work with the Button's Text property within this click event, which is a single click_event for multiple buttons?
Step 1: You need to subscribe to the Button Click event of all your buttons to the same EventHandler. so that button click on all your Buttons will fire the same `Event Handler.
Step 2: You need to cast the object sender into Button and then access its Text property to get the Button Text.
Try This:
button1.Click += new System.EventHandler(MyButtonClick);
button2.Click += new System.EventHandler(MyButtonClick);
button3.Click += new System.EventHandler(MyButtonClick);
private void MyButtonClick(object sender, EventArgs e)
{
Button btnClick = (Button)sender ;
guess(btnClick.Text);
}
Cast sender to type button.
Example:
private void guess_Click(object sender, EventArgs e)
{
guess(((Button)sender).Text);
}
You need to cast the sender object to the Button type and use that:
private void guess_Click(object sender, EventArgs e)
{
Button senderBtn = senderBtn as Button;
if(senderBtn != null)
{
guess(senderBtn.Text);
}
}
I have a code to detect keyboard input on a plugin
private void Plugin_Load(object sender, EventArgs e)
{
mouse = new MouseInput();
mouse.MouseMoved += mouse_MouseMoved;
}
void mouse_MouseMoved(object sender, EventArgs e)
{
if (testBool== true)
{
return; // Trying to cancel mouse event
}
}
I need to cancel event e(EventArgs), but return does not seems to work.
MouseEventArgs is a Winform event so doesn't works on my code.
Kindly suggest on how to cancel the event.
Thanks
I have a textbox and in some cases in Enter event I need to set the focus to a different textbox.
I tried that code:
private void TextBox1_Enter(object sender, EventArgs e)
{
if(_skipTextBox1) TextBox2.Focus();
}
But this code doesn't work. After that I found on MSDN:
Do not attempt to set focus from within the Enter, GotFocus, Leave, LostFocus, Validating, or Validated event handlers.
So how can I do it other way?
Postpone executing the Focus() method until after the event is finished executing. Elegantly done by using the Control.BeginInvoke() method. Like this:
private void textBox2_Enter(object sender, EventArgs e) {
this.BeginInvoke((MethodInvoker)delegate { textBox3.Focus(); });
}
You could handle the KeyPress event instead:
private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
e.Handled = true;
TextBox2.Focus();
}
}
textBox.Select();
or
textBox.Focus();
or
set TabIndex = 0 from properties of that textBox.
both methods are use to set focus on textBox in C#, .NET