View Full Version : [C#] Click con Destro + Sinistro
bedo2991
09-11-2008, 18:21
Come faccio a dire al sistema che è stato fatto click sia col tasto destro che col sinistro? (Stile prato fiorito insomma...)
Ho provato con
void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button==MouseButtons.Right)
{
if (e.Button == MouseButtons.Left)
{
//Premuti entrambi i tasti
Ma niente...
void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button==MouseButtons.Right)
{
if (e.Button == MouseButtons.Left)
{
//Premuti entrambi i tasti
In quel modo bisogna per forza prima premere il destro, poi il sinistro nel momento esatto in cui C# entra nel secondo if... Un miracolo probabilistico... :D .
Io la farei così :
1. Creo 2 var bool a livello di Form :
public partial class Form1 : Form
{
private bool DX = false;
private bool SN = false;
2. Gestisco gli eventi MouseDown e Up :
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
SN = true;
}
if (e.Button == MouseButtons.Right)
{
DX = true;
}
if (SN == true && DX == true)
{
//Codice da eseguire...
this.BackColor = Color.Green;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
SN = false;
}
if (e.Button == MouseButtons.Right)
{
DX = false;
}
this.BackColor = SystemColors.Control;
}
;)
bedo2991
10-11-2008, 09:31
e a quel punto come faccio a distinguere il click destro dal sinistro?
Ho tre situazioni:
Click con entrambi
Click con il destro
Click col sinistro
Tu onmouseUp riporti tutte e due le situazioni a false... Lì magari devo controllare che se erano già a false devo fare quello del tasto opposto... Uno dei due deve essere comunque a true dato che sennò non partiva l'evento.
Ho tre situazioni:
Click con entrambi
Click con il destro
Click col sinistro
In tal caso così dovrebbe andare :
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
SN = true;
}
if (e.Button == MouseButtons.Right)
{
DX = true;
}
if (SN == true && DX == true)
{
//Codice da eseguire...
this.BackColor = Color.Green;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
SN = false;
}
if (e.Button == MouseButtons.Right)
{
DX = false;
}
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && DX==false)
{
//Codice per Click Sinistro:
this.BackColor = Color.Yellow;
}
if (e.Button == MouseButtons.Right && SN==false)
{
//Codice per Click Destro:
this.BackColor = Color.Red;
}
}
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.