1

So... The user has to click on the button, after he clicks it, the program should wait for another click on Panel and get the coordinates of that click. But as soon as I click the button, everything becomes unresponsive. Am I doing something wrong?

    private void Surbutton_Click(object sender, EventArgs e)
    {
        panel1.Cursor = Cursors.Cross;
        Cursor.Position = new Point(Left + panel1.Left + panel1.Width / 2, Top + panel1.Top + panel1.Height / 2);
        ziskavanie_pozicie = true;

        //Button ABCD = sender as Button;
        string ABCD = ((Button)sender).Name;

        switch (ABCD)
        {
            case "button_A":
                //cakaj.WaitOne();
                cakaj_manual.WaitOne();
                suradnica_Ax.Text = x.ToString();
                suradnica_Ay.Text = x.ToString();
                break;

            case "button_B":
                suradnica_Bx.Text = x.ToString();
                suradnica_By.Text = x.ToString();
                break;

            case "button_C":
                suradnica_Cx.Text = x.ToString();
                suradnica_Cy.Text = x.ToString();
                break;

            case "button_D":
                suradnica_Dx.Text = x.ToString();
                suradnica_Dy.Text = x.ToString();
                break;
        }
    }

    public void panel1_MouseClick(object sender, MouseEventArgs e)
    {
        MessageBox.Show("Hehe");
        if (ziskavanie_pozicie == true)
        {
            x = e.X;
            y = e.Y;
            //panel1.PointToClient(Cursor.Position);

            ziskavanie_pozicie = false;
            panel1.Cursor = Cursors.Default;
            //cakaj.Set();      
            cakaj_manual.Set();
        }
    }
Matej
  • 75
  • 10
  • I wrote you the reason why your GUI freezes. But if you add more descriptive information of why you need the program to wait for another click, then I can probably help you and probably many more. –  Mar 30 '18 at 11:41
  • So I am drawing a two lines between two points. To do that I have 4 buttons for each Point. After user clicks one of the buttons which is the first event I need to read mouse coordinates from click on the Panel and add them to corresponding Point. – Matej Mar 30 '18 at 12:12
  • Mind sending the whole code so i get better overview? btw, It's probably a better idea using english variable names in programming in general. –  Mar 30 '18 at 12:30
  • 1
    I know, I normally do that, but our teacher is kinda special, when it comes to this. Yeah, I could share the code with you asap. – Matej Mar 30 '18 at 12:35

2 Answers2

0

If you just want to know for the reason your programs freezes, It's because you using for some reason the

ManualResetEvent or AutoResetEvent

which are not expected to be used on the main thread as it's not the reason they created for. If you say for example manualResetEvent.WaitOne(); on the main thread, everything will freeze. They are just made for synchronizing threads just like Mutex, Semphores, TPL->Await etc..

  • Is there any other way, I could make the first event wait for the second event? What is your advice? – Matej Mar 30 '18 at 12:44
  • You're wondering why the UI freezes, It's because everything runs on the main thread and this isn't an advice, it's like like explaining to you why it freezes so you understand that you use it the wrong way. It's hard to tell exactly what you're trying to do from the info you wrote to give you an advice. –  Mar 30 '18 at 12:48
  • Ok I am gonna try to explain it better in my answer. – Matej Mar 30 '18 at 13:35
  • Never mind... Just realized I spend 3 hours on something I could easier way... Fix it in literally 10 seconds... Thanks for help anyway :) – Matej Mar 30 '18 at 15:08
0

So when the user clicks on the button shown by cursor on the image (or any of the buttons with the arrow) the cursor is gonna be moved to center of the white panel. At that point I need the user to click somewhere on the panel and get the coordinates of that click. When I have the coordinates, I assign them to textboxes to the left side of the buttons.

https://imgur.com/a/il1FI

Matej
  • 75
  • 10