I am fairly new with C# and I want to know if there is a way to implement a double right click in this event handler? Can anyone tell me how? thanks
private void pictureBox_MouseClick(object sender, MouseEventArgs e){
if(e.Button == MouseButtons.Left)
{
MessageBox.Show("A");
}
else if(e.Button = MouseButtons.Right)
{
MessageBox.Show("B");
}
else if(e.Button = MouseDoubleClick.Right) <--how to fix this?
{
MessageBox.Show("C");
}
else
{
MessageBox.Show("D");
}
}
You can use the ClickCount propery of MouseRightButtonDown event.
private void OnMouseDownClickCount(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 1)
{
// one click.
}
if (e.ClickCount == 2)
{
// two clicks.
}
}
Override the WndProc function and listen for WM_RBUTTONDBLCLK, which as can be seen on this pinvoke page is {0x0206}.
Then
public class RButton : Button
{
public delegate void MouseDoubleRightClick(object sender, MouseEventArgs e);
public event MouseDoubleRightClick DoubleRightClick;
protected override void WndProc(ref Message m)
{
const Int32 WM_RBUTTONDBLCLK = 0x0206;
if (m.Msg == WM_RBUTTONDBLCLK)
DoubleRightClick(this, null);
base.WndProc(ref m);
}
}
Related
I want to click a button when i press F1
this is my code
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F1)
{
buttonGfxIn_Click.PerformClick();
}
}
But i get this error
that button name Is a 'method' which is not valid in the given context error
I have test with a example and it is working well. try it.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("button1 was clicked");
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
KeyEventArgs e = new KeyEventArgs(keyData);
if (e.KeyCode == Keys.F1)
{
button1_Click("", e);
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
Here's the code. I have modified it bit?
public partial class MainWindow : Window
{
int coursenumber;
public MainWindow()
{
InitializeComponent();
}
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
bool res = int.TryParse(textBox.Text, out coursenumber);
if (res == true)
{
//success
}
}
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
//Check if the key that was pressed was the Enter key.
if (e.Key == Key.Enter)
{
//logic goes here
}
}
Register to the Key Down event and check
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
// your logic here
}
}
What you're looking for is the KeyDown event. Subscribe to this event on your TextBox like this:
<TextBox KeyDown="textBox_KeyDown" ... />
And your event handler will look something like this:
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
//Check if the key that was pressed was the Enter key.
if (e.Key == Key.Enter)
{
bool res = int.TryParse ...
//The rest of your logic here.
}
}
Thanks everyone, i was able to get it working from your responses. here's the modification
public partial class MainWindow : Window
{
int coursenumber;
public MainWindow()
{
InitializeComponent();
}
private void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
bool res = int.TryParse(textBox.Text, out coursenumber);
if (res == true)
{
//success
}
}
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
//Check if the key that was pressed was the Enter key.
if (e.Key == Key.Enter)
{
label2.Content = coursenumber;
}
}
I have a timer event as given below and I have got some suggestions from this post. Could you suggest me what is wrong with this? I am getting the following crash:
Unable to cast object of type System.Windows.Forms.Timer to type System.Windows.Forms.Button.
Any suggestions on where I am going wrong??
public MainForm()
{
InitializeComponent();
ButtonTimer.Tick += new EventHandler(ButtonTimer_Tick);
ButtonTimer.Interval = 100;
}
private void ButtonTimer_Tick(object sender, EventArgs e)
{
Button CurrentButton = (Button)sender;
string PressedButton = CurrentButton.Name;
switch (PressedButton)
{
case "BoomUp":break;
}
}
private void BoomUp_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//ButtonTimer.Enabled = true;
ButtonTimer.Start();
}
}
private void BoomUp_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ButtonTimer.Stop();
}
}
You have a problem in ButtomTime_Tick method :
Button CurrentButton = (Button)sender;
sender is not a Button, it's a Timer.
So what you gonna do now ?
You could add a private field in your class
private Button currentButton_;
and
private void BoomUp_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
currentButton_ = e.Button;//but I don't know if e.Button has a Name "BoomUp"
//you can set the name manually if needed :
currentButton_.Name = "BoomUp";
//ButtonTimer.Enabled = true;
ButtonTimer.Start();
}
}
and
private void ButtonTimer_Tick(object sender, EventArgs e)
{
switch (currentButton_.Name)
{
case "BoomUp":break;
}
}
In ButtonTimer_Tick the sender is the timer, not a button. Hence you cast a timer into a button (1st line in that method).
Following code, doesn't get triggered when Esc is pressed. Can somebody give me insight?
I like to change the cursor (let's say from drawing mode turn it into pointer mode)
public override void OnKeyDown(MyCanvas dc, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
_line = null;
e.Handled = true;
}
}
Thanks.
#amit's solution:
public override void OnKeyDown(MyCanvas dc, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
_line = null;
dc.CurrentTool = ToolType.Pointer;
dc.Cursor = Cursors.Arrow;
dc.ReleaseMouseCapture();
}
}
Is there any event that is raised when a window is restored in C#/.NET?
I noticed that there is an event that is raised when a window is activated, but I can't find a corresponding event for a window being restored, such as from a maximized or minimized state.
If you don't like using the form's WindowState property and don't want to have to keep around a flag indicating the form's previous state, you can achieve the same result at a slightly lower level.
You'll need to override your form's window procedure (WndProc) and listen for a WM_SYSCOMMAND message indicating SC_RESTORE. For example:
protected override void WndProc(ref Message m)
{
const int WM_SYSCOMMAND = 0x0112;
const int SC_RESTORE = 0xF120;
if (m.Msg == WM_SYSCOMMAND && (int)m.WParam == SC_RESTORE)
{
// Do whatever processing you need here, or raise an event
// ...
MessageBox.Show("Window was restored");
}
base.WndProc(ref m);
}
You can check it this way:
private void Form1_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
...
}
else if ....
{
}
}
Pretty unsure. You'd have to handle the SizeChanged event and detect if WindowState changed from Minimized to Normal or Maximized to Normal. Source
I know this question is quite old but it works this way:
public Form1()
{
InitializeComponent();
this.SizeChanged += new EventHandler(Form1_WindowStateTrigger);
}
private void Form1_WindowStateTrigger(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
MessageBox.Show("FORM1 MINIMIZED!");
}
if (this.WindowState == FormWindowState.Normal)
{
MessageBox.Show("FORM1 RESTORED!");
}
if (this.WindowState == FormWindowState.Maximized)
{
MessageBox.Show("FORM1 MAXIMIZED!");
}
}
Using the SizeChanged event, coupled with a check of WindowState does the trick here.
Check:
private void Form1_Activated(Object o, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized) {
...
}
}
The answer from Redhart is good. This is the same but slightly simpler:
public Form1()
{
InitializeComponent();
this.SizeChanged += Form1_SizeChanged;
}
#region Event-Handlers
void Form1_SizeChanged(object sender, EventArgs e)
{
var state = this.WindowState;
switch (state)
{
case FormWindowState.Maximized:
ClearMyView();
DisplayMyStuff();
break;
case FormWindowState.Minimized:
break;
case FormWindowState.Normal:
ClearMyView();
DisplayMyStuff();
break;
default:
break;
}
}
#endregion Event-Handlers
It's easy enough to add:
public partial class Form1 : Form {
private FormWindowState mLastState;
public Form1() {
InitializeComponent();
mLastState = this.WindowState;
}
protected override void OnClientSizeChanged(EventArgs e) {
if (this.WindowState != mLastState) {
mLastState = this.WindowState;
OnWindowStateChanged(e);
}
base.OnClientSizeChanged(e);
}
protected void OnWindowStateChanged(EventArgs e) {
// Do your stuff
}
go to this link
winforms-windowstate-changed-how-to-detect-this?