윈도우 폼 애플리케이션을 개발 중입니다. 일부 서비스에 대한 UI 작성. 무슨 일이 일어나는가는 응용 프로그램을 만들 때 한 번의 클릭으로 값을 가져오고 설정하는 다양한 버튼이 있지만 코드 완료 후 모든 버튼이 두 번 클릭으로 반응하는 것입니다. 몇 가지 중단 점을 적용하고 테스트 할 때 값은 한 번 클릭으로 만 설정되지만 런타임에는 두 번 클릭해야합니다. 아무도 도와 줄 수 있니?C#. 단일 클릭이 필요할 때 더블 클릭을 피하는 방법
답변
코드 조각을 작성하여 해결책을 얻었습니다. 첫 번째 클릭시 버튼을 활성화했습니다.
bool firstClick = true; {if (firstClick) {button.select(); // 버튼 활성화}}
답장을 보내 주셔서 감사합니다.
MSDN의 Article (전체)은 한 번만 사용하는 방법과 두 번 클릭하는 방법에 대한 것입니다. 또한 이벤트를 처리하고 한 번의 클릭과 두 번 클릭을 구별하는 방법을 설명합니다.
이 기사에서는이를 수행하기 위해 부울 및 타이머를 사용합니다. 여러 개의 버튼이있는 경우 Dictionary<Button, boolean>
을 사용해야 할 수도 있습니다. 희망이 도움이됩니다.
가 MouseDown 이벤트를 처리하고 해당 SystemInformation 적절 타이머 컴포넌트를 사용하여 클릭 사이에서 위치 및 시간 간격 을 결정 : 링크 꿇고 또는 일부 reaosn 제거하는 경우 여기
는 예이다. 클릭 또는 두 번 클릭 여부에 따라 적절한 조치를 수행하십시오. 다음 코드 예제 은이 작업을 수행하는 방법을 보여줍니다.
class Form1 : Form
{
private Rectangle hitTestRectangle = new Rectangle();
private Rectangle doubleClickRectangle = new Rectangle();
private TextBox textBox1 = new TextBox();
private Timer doubleClickTimer = new Timer();
private ProgressBar doubleClickBar = new ProgressBar();
private Label label1 = new Label();
private Label label2 = new Label();
private bool isFirstClick = true;
private bool isDoubleClick = false;
private int milliseconds = 0;
[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}
public Form1()
{
label1.Location = new Point(30, 5);
label1.Size = new Size(100, 15);
label1.Text = "Hit test rectangle:";
label2.Location = new Point(30, 70);
label2.Size = new Size(100, 15);
label2.Text = "Double click timer:";
hitTestRectangle.Location = new Point(30, 20);
hitTestRectangle.Size = new Size(100, 40);
doubleClickTimer.Interval = 100;
doubleClickTimer.Tick +=
new EventHandler(doubleClickTimer_Tick);
doubleClickBar.Location = new Point(30, 85);
doubleClickBar.Minimum = 0;
doubleClickBar.Maximum = SystemInformation.DoubleClickTime;
textBox1.Location = new Point(30, 120);
textBox1.Size = new Size(200, 100);
textBox1.AutoSize = false;
textBox1.Multiline = true;
this.Paint += new PaintEventHandler(Form1_Paint);
this.MouseDown += new MouseEventHandler(Form1_MouseDown);
this.Controls.AddRange(new Control[] { doubleClickBar, textBox1,
label1, label2 });
}
// Detect a valid single click or double click.
void Form1_MouseDown(object sender, MouseEventArgs e)
{
// Verify that the mouse click is in the main hit
// test rectangle.
if (!hitTestRectangle.Contains(e.Location))
{
return;
}
// This is the first mouse click.
if (isFirstClick)
{
isFirstClick = false;
// Determine the location and size of the double click
// rectangle area to draw around the cursor point.
doubleClickRectangle = new Rectangle(
e.X - (SystemInformation.DoubleClickSize.Width/2),
e.Y - (SystemInformation.DoubleClickSize.Height/2),
SystemInformation.DoubleClickSize.Width,
SystemInformation.DoubleClickSize.Height);
Invalidate();
// Start the double click timer.
doubleClickTimer.Start();
}
// This is the second mouse click.
else
{
// Verify that the mouse click is within the double click
// rectangle and is within the system-defined double
// click period.
if (doubleClickRectangle.Contains(e.Location) &&
milliseconds < SystemInformation.DoubleClickTime)
{
isDoubleClick = true;
}
}
}
void doubleClickTimer_Tick(object sender, EventArgs e)
{
milliseconds += 100;
doubleClickBar.Increment(100);
// The timer has reached the double click time limit.
if (milliseconds >= SystemInformation.DoubleClickTime)
{
doubleClickTimer.Stop();
if (isDoubleClick)
{
textBox1.AppendText("Perform double click action");
textBox1.AppendText(Environment.NewLine);
}
else
{
textBox1.AppendText("Perform single click action");
textBox1.AppendText(Environment.NewLine);
}
// Allow the MouseDown event handler to process clicks again.
isFirstClick = true;
isDoubleClick = false;
milliseconds = 0;
doubleClickBar.Value = 0;
}
}
// Paint the hit test and double click rectangles.
void Form1_Paint(object sender, PaintEventArgs e)
{
// Draw the border of the main hit test rectangle.
e.Graphics.DrawRectangle(Pens.Black, hitTestRectangle);
// Fill in the double click rectangle.
e.Graphics.FillRectangle(Brushes.Blue, doubleClickRectangle);
}
}
당신은 단순히 타이머를 사용하여 한 번의 클릭과 더블을 식별 할 수 있습니다. 여기에 예제가 있습니다
class Form1 : Form
{
Timer timer;
public Form1()
{
InitializeComponent();
timer = new Timer();
timer.Interval = 500;
timer.Tick += new EventHandler(Timer_Tick);
}
private void App_MouseDown(object sender, MouseEventArgs e)
{
if (e.Clicks == 1)
{
timer.Start();
}
else
{
timer.Stop();
doubleClick();
}
}
private void Timer_Tick(object sender, EventArgs e)
{
timer.Stop();
singleClick();
}
//Single click
public void singleClick()
{
MessageBox.Show("Single Click.");
}
//Double click
public void doubleClick()
{
MessageBox.Show("Double Click.");
}
}
한 가지 더. 정확히 두 번 클릭만으로 작동하는지 확신 할 수 없습니다. 우리가 한 번 클릭하고 잠시 시간을내어 2 초라고 말하면 다시 한 번 클릭하면됩니다. 양식로드가 문제가 될 수 있습니다. 해줘서 고맙습니다. 그러나 아직도이 의견을 고려해야합니다. – AutoEng
이와 같이 반응하는 샘플 코드를 제공해 주실 수 있습니까? –
죄송하지만 전체 코드가 포함되어 있습니다. 이 코드를 완료하면 더블 클릭으로 반응이 시작됩니다. CAN 버스에서 값을 가져오고 설정하고 있습니다. 처음에는 신호 값을 가져오고 설정해야하며 값이 double 유형이면 모든 것이 올바르게되었습니다. 그러나 값 유형이 객체 유형으로 변경되면 두 번 클릭으로 레 케이션을 시작했습니다. – AutoEng
일부 발췌 문장은 단어를 더 많이 말할 수 있습니다. 그러면 우리에게 더 명확해질 것입니다 –