okay so what I want to do, it when the mouse is HELD DOWN I want it to continously press a key. it should continously press this key until I let off.
Imagine if you will, a left and right button on a windows form,
then by clicking and holding the right button, the letter "R" displays on a textbox continously until you release the button. What I am doing has very little to do with that scenario, but you get what I'm trying to do here.
What exactly do I put in the mouse down to keep the sendkeys going forever without locking up the application?
I hope my question makes sense. lol.
Thanks
RT
private void pictureBoxKeyboard_MouseDown(object sender, MouseEventArgs e)
{
//something goes here
}
This is worth a read...
http://msdn.microsoft.com/en-us/library/ms171548.aspx
SendKeys.Send("r")
This might just fire once, you may want to attach a timer that starts on the MouseDown event and stops on the MouseUp event. Then you could put the SendKeys in the Timer.Tick event.
private void Form1_Load(object sender, EventArgs e)
{
this.timer1.Interval = 500;
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
timer1.Stop();
this.Text = "moose-Up";
}
private void button1_MouseDown(object sender, EventArgs e)
{
timer1.Start();
this.Text = "moose-Down";
this.richTextBox1.Select();
}
private void timer1_Tick(object sender, EventArgs e)
{
SendKeys.Send("r");
Debug.Print("tickling");
}
Select the control that you wish to receive the SendKeys value...
Related
If you ever remove focus from any professional application like Chrome/FireFox/Visual Studio, and then reclick a button/menu item, it will actually click it as if you never lost focus.
How can I apply the same concept in C# WinForm? I tried many things like
private void form1_MouseClick(object sender, MouseEventArgs e)
{
BringToFront();
Activate();
}
Activate/focus/select/etc... nothing worked to react the same way, it always takes 3-4 clicks to actually click on a menu!
I thought about making a click event for every single control, but that seemed rather redundant.
Check this for example (Yellow Clicks)
You are right about Menues taking an extra click to get focus.
Which is extra annoying since the menue get highlighted anyway but doesn't react to the 1st click..
You can avoid that by coding the MouseEnter event:
private void menuStrip1_MouseEnter(object sender, EventArgs e)
{
// either
menuStrip1.Focus();
// or
this.Focus();
}
The downside of this is, that it is stealing focus from other applications, which is not something a well-behaved application should do..
So I think it is better to wait for a definitive user action; code the MouseDown event in a similar way..:
private void menuStrip1_MouseDown(object sender, MouseEventArgs e)
{
menuStrip1.Focus();
}
Or use the event that was made for the occasion:
private void menuStrip1_MenuActivate(object sender, EventArgs e)
{
menuStrip1.Focus();
}
I can't confirm a similar problem with Buttons or any other controls, though.
I have find trick to solve your problem. it work for me 100%
See this code:
dynamic elem1;
private void menuStrip1_MouseEnter(object sender, EventArgs e)
{
elem1 = sender;
}
private void menuStrip1_MouseLeave(object sender, EventArgs e)
{
elem1 = null;
}
private void Form1_Activated(object sender, EventArgs e)
{
if(elem1 != null){
elem1.PerformClick();
if (elem1.GetType().ToString() == "System.Windows.Forms.ToolStripMenuItem") elem1.ShowDropDown();
}
elem1 = null;
}
Here what happend.
When mouse enter button/menu item elem1 = this button/menu, and when mouse leave it set back to null.
so when form Activated we can call elem1.PerformClick() to click the button/menu item.
i need to hide a ListBox when I focus out a textbox. if i click on a different control or use Tab key then the textbox's "Leave" event occurs. But if I click inside the form, on any free space, then focusout doesn't happen. i saw something called mouse capture but i cant implement it.
i tried this:
private void txtProduct_Enter(object sender, EventArgs e)
{
listProduct.Show();
UIElement el = (UIElement)sender;
el.CaptureMouse();
}
private void MouseClickedElseWhere(object sender, MouseEventArgs e)
{
if (e.Clicks >= 1)
{
txtProduct_Leave(sender, new EventArgs());
}
}
private void txtProduct_Leave(object sender, EventArgs e)
{
listProduct.Hide();
}
but obviously it shows error. how do i achieve this? any help?
I had to make click event for my groupboxes even if groupbox doesnt have a click event by default.
//my_page.designer.cs
this.groupBox2.Click += new System.EventHandler(this.groupBox2_clicked);
//my_page.cs
private void groupBox2_clicked(object sender, EventArgs e)
{
listProduct.Hide();
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn0_Click(object sender, EventArgs e)
{
MessageBox.Show("called btn 0 click..");
KeyPressEventArgs e0 = new KeyPressEventArgs('0');
textBox1_KeyPress(sender, e0);
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show("called txtbox_keypress event...");
}
}
Am sorry if this is a silly question,I have just started to learn windows forms, I still find material on the internet confusing.I want to implement calculator. So when number button is pressed it should be filled in textbox. So I thought calling textBox1_keypress() event from button click event would work??? but its not working,
I can manually write the logic in button click event to fill text in text box but if i do so, i have to do the same thing in button1_KeyPress event too. so it would be duplication of code right??...so i thought solution was to call textBox1_KeyPress() event from both button click event and button key press event...but its not working .So what should i do???..is there any other approach which should i follow.
so it would be duplication of code right??
Yes, it would be. So you can do
private void btn0_Click(object sender, EventArgs e)
{
CommonMethod(e);
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
CommonMethod(e);
}
private void CommonMethod(EventArgs e)
{
//Your logic here.
}
The TextBox KeyPress event handler (textBox1_KeyPress) is called after the user presses a key. The KeyPressEventArgs parameter includes information such as what key was pressed. So calling it from your btn0_Click method isn't going to set the text for the TextBox.
Rather, you want to (probably) append whatever number the user pressed to the text already present in the TextBox. Something like
private void btn0_Click(object sender, EventArgs e)
{
textBox1.Text += "0";
}
might be closer to what you're trying to accomplish.
You could put the logic in an extra function like so:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn0_Click(object sender, EventArgs e)
{
NumberLogic(0),
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
// I don't know right now if e contains the key pressed. If not replace by the correct argument
NumberLogic(Convert.ToInt32(e));
}
void NumberLogic(int numberPressed){
MessageBox.Show("Button " + numberPressed.ToString() + " pressed.");
}
}
You don't want to tie the events together like that.
A key-press is one thing, handled in one way.
A button click is something totally different and should be handled as such.
The basic reason is this,
The button doesn't know what number it is, you need to tell it that.
A key-press on the other hand, knows what number was pressed.
If you REALLY want to, for some reason, you could use SendKeys to trigger your key-press event in a round-about way, from the button.
SendKeys.SendWait("0");
I can suggest to you to use an Tag Property of the Buttons. Put in it the value of each button in Design mode or in Constructor, create one button event handler for all buttons and use Tag value:
Constructor:
button1.Tag = 1;
button2.Tag = 2;
button1.Click += buttons_Click;
button2.Click += buttons_Click;
Event hadler:
private void buttons_Click(object sender, EventArgs e)
{
textBox1.Text = ((Button)sender).Tag.ToString();
}
I am currently trying to create a hover effect on a custom control, where a panel (panel1) shows up after the mouse enters the control.
I have a timer that starts when the mouseleave event is raised, the interval is 250ms, and there is an onTick event that changes the visibility of the panel1 to false.
This all works. However, the buttons on panel1 do not always respond when clicked.
Here is the pertinent code - I will supply anything else that is required if I'm missing some information.
private void MagicCardViewer_MouseLeave(object sender, EventArgs e)
{
timer1.Start();
timer1.Interval = 250;
timer1.Tick += new EventHandler(timer1_TickOff);
timer1.Tick -= timer1_TickOn;
}
private void MagicCardViewer_MouseEnter(object sender, EventArgs e)
{
showPanel1();
}
public void showPanel1()
{
//show necessary controls
buttonDiscard.Show();
//show panel1
panel1.Visible = true;
ActiveControl = panel1;
}
public void hidePanel1()
{
panel1.Visible = false;
//hide controls
}
# region button events
private void buttonChoose_Click(object sender, EventArgs e)
{
Chosen = !Chosen;
if (Chosen)
{
callCardChosen();
}
}
private void buttonTap_Click(object sender, EventArgs e)
{
cards[0].ChangeTap();
DrawCardTap();
onCardChanged();
}
private void buttonActivate_Click(object sender, EventArgs e)
{
cards[0].TryActivate(0);
}
private void buttonDiscard_Click(object sender, EventArgs e)
{
cards[0].onDiscard();
}
# endregion
I think that is everything, but there is a lot of code to select from.
breakpoints don't trigger, the button flashes but nothing happens. If the timer interval is set very long, it works fine, but the point is for the button to vanish quickly once the mouse leaves the control. If the mouse is on the buttons, then the form reports it as having left the control.
To sum up - the buttons are not always processing when I click on them.
If you have the timer ticking every 250ms that could prevent the event on the mouse click.
I would check that you stop the timer after the tick if you no longer need it, and restart as I think you are doing when the user leaves the control or enter it again.
This could be why it works when you set a longer time interval.
I'm trying to define MouseEventHandlers such that the application will exit whenever the mouse is clicked or moved or whenever a key is pressed. This is my first time using C#, but based on what I found online, I've written the code as follows:
MouseDown += new MouseEventHandler(mouseClickedResponse);
MouseMove += new MouseEventHandler(mouseMovedResponse);
KeyDown += new KeyEventHandler(keyResponse);
which connects to:
private void keyResponse(object sender, EventArgs e)
{
Application.Exit();
}
private void mouseClickedResponse(object sender, EventArgs e)
{
Application.Exit();
}
private void mouseMovedResponse(object sender, EventArgs e)
{
if (firstCall) //Keeps the application from exiting immediately
firstCall = false;
else Application.Exit();
}
The problem that I'm finding is that while the KeyEventHandler works perfectly, I can move and click the mouse as much as I want to no avail.
This is the sum total of the code that I've written to allow for user control; am I missing something?
On the surface, everything looks good with your code.
One possibility - The MouseEventHandler is defined in both the System.Windows.Input (MSDN) namespace as well as the System.Windows.Forms namespace (MSDN).
I believe the one you want is the one in the Forms namespace. Is it possible that you're using the one from the Input namespace instead?
I fixed my problem--my Form was filled with Panels, and by moving the code for mouse input over to the panels, everything worked instantly.
Change:
private void mouseClickedResponse(object sender, EventArgs e)
to:
private void mouseClickedResponse(object sender, MouseEventArgs e)
It should now work fine.