Create a Windows Form (Net Framework or Net Core) and add a Toolstrip with ToolStripComboBox control. And then add these methods:
public Form1()
{
InitializeComponent();
KeyDown += Form1_KeyDown;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
//any code here;
}
If Form KeyPreview = false then Form1_KeyDown event is not triggered with MouseWheel event on ToolstripComboBox. Setting KeyPreview = true (is necessary) causes MouseWheel event is captured by Form1_KeyDown handler as Up or Down.
Is there a way to prevent Form_KeyDown handler Not To Handle MouseWheel event?
both using comments here as source and referencing vs code knowledge, one solution to this:
private void Form1_KeyDown(dynamic sender, KeyEventArgs e)
{
if (sender.GetType().Name != "Form1")
{
//code for action...
}
}
Related
I have a winforms app, that catches MouseDown, MouseUp and Click events.
Clicking the form slowly (once a second or so) and the event counters stay in sync.
Clicking quickly and the down/up event tallys keep track, but click event tally falls behind:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private int clicks = 0;
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
clicks++;
textBox1.Text = clicks.ToString();
}
private int mdown=0;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
mdown++;
textBox2.Text = mdown.ToString();
}
private int mup = 0;
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
mup++;
textBox3.Text = mup.ToString();
}
}
Reading docs: https://msdn.microsoft.com/en-us/library/ms171542.aspx this doesn't seem possible - am i missing something obvious
(This happens when using trackpad buttons or external bluetooth mouse, so hopefully this is a programming error and not an issue with the machine.)
EDIT
Damien is of course correct, tracking double clicks as well and everything stays in sync:
private void Form1_MouseDoubleClick(object sender, MouseEventArgs e)
{
clicks++;
textBox1.Text = clicks.ToString();
}
Because sometimes you're clicking fast enough that you're triggering double-clicks. Consider the sequence from the documentation page you linked to:
Following is the order of events raised for a double mouse-button click:
MouseDown event.
Click event.
MouseClick event.
MouseUp event.
MouseDown event.
DoubleClick event. (This can vary, depending on whether the control in question has the StandardDoubleClick style bit set to true. For more information about how to set a ControlStyles bit, see the SetStyle method.)
MouseDoubleClick event.
MouseUp event.
And notice that there are twice as many MouseDown/MouseUp events in that sequence as there are MouseClick events.
I have on form1 designer a windows forms chart control.
In form1 constructor i added:
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
chart1.MouseWheel += chart1_MouseWheel;
Then i have the MouseWheel event:
void chart1_MouseWheel(object sender, MouseEventArgs e)
{
MessageBox.Show("hi");
}
And in the designer i also added the event AxisViewChanged:
private void chart1_AxisViewChanged(object sender, ViewEventArgs e)
{
paintToCalaculate = true;
chart1.Invalidate();
}
But using a breakpoint in the MouseWheel event or on the AxisViewChanged event it's never get and stop there.
I'm creating a windows form at run-time. Now i want the Key-press event to be triggered for the dynamically created form.
How do i create/bind the event to newly/dynamically created windows form in C#.
Thanks,
If we take a text box its like this.
private void Form1_Load(object sender, EventArgs e)
{
TextBox myTextBox = new TextBox();
myTextBox.KeyPress += new KeyPressEventHandler(myTextBox_KeyPress);
this.Controls.Add(myTextBox);
}
void myTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
//Do Key press event work here
}
UPDATE
Make sure that the focus should be on Form2.
Try This.
Form dynamicForm = new Form();
dynamicForm.KeyPress += new KeyEventHandler(onkeyPress);
void onkeyPress(object sender, KeyEventArgs e)
{
Console.WriteLine("test");
}
Make sure the forms KeyPreview Property is set to true, that way it will see the keystrokes.
From above link:
When this property is set to true, the form will receive all KeyPress, KeyDown, and KeyUp events. After the form's event handlers have completed processing the keystroke, the keystroke is then assigned to the control with focus. For example, if the KeyPreview property is set to true and the currently selected control is a TextBox, after the keystroke is handled by the event handlers of the form the TextBox control will receive the key that was pressed. To handle keyboard events only at the form level and not allow controls to receive keyboard events, set the KeyPressEventArgs.Handled property in your form's KeyPress event handler to true.
So you will want to do something like this:
public partial class Form1 : Form
{
Form2 f2;
public Form1()
{
InitializeComponent();
KeyPreview = true;
KeyDown += Form1_KeyDown;
}
void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.Control)
{
switch(e.KeyCode)
{
case Keys.C:
MessageBox.Show("Cntrl C");
break;
case Keys.V:
MessageBox.Show("Cntrl V");
break;
default:
break;
}
}
}
}
I have created event for KeyDown which also triggers the mouse event. I dont want the mouse event. What can i do. Here is my sample code,
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode.ToString().Equals("Up"))
{
//...
}
if (e.KeyCode.ToString().Equals("Down"))
{
//...
}
}
This works fine, but whenever i scrool the scrollbar of the mouse it triggers the respective events. What can i do now.
Thanks in advance...
Code in User Control (Windows User Control).
I have a button and when click on the button some logic will fire.
below is button event.
private void btnCancel_Click(object sender, EventArgs e)
{
// Some Logic
}
I have Main form (Windows form)
I am adding the above user control in a Panel of my Main Winform.
From here I want to invoke the user control btnCancel_Click event using some function Keys Like (F3,F4).
This is a very new thing for me. Please help me in this regard.
Thanks .
Krishna
Set your parent form KeyPreview attribute to true and then in the KeyDown event of your parent form, look for if(e.KeyCode == Keys.F3), then write a public method for your usercontrol which fires the btnCancel_Click. You can access it from your parent form.
Parent Form:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F3)
{
MyUserControl.FireButtonEvent();
}
}
Usercontrol:
public void FireButtonEvent()
{
this.btnCancel_Click(null, EventArgs.Empty);
}
Create a KeyEvent method. for example:
private void KeyDown(Object sender, KeyEventArgs e)
{
string sKey = e.KeyValue.ToString();
if (sKey==112) {//SomeCode;} // F1
}
In the designer code link the method to a key press event on the main form:
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.KeyDown);
Look on the net for the key values of the button you want to activate it,
F1 to F24 are values 112 to 135.