简体   繁体   中英

Multiple buttons inside loop doesn't hide and show with .hide() in windows form c# application

I'm trying to cover the image with buttons like this :

图片1

Underneath that there's an arrow section like this :

图像2

I have a for loop and I'm trying to hide and show buttons with buttonx.hide() and buttonx.show() function.

for (int i = 0; i < 100; i++) {

    if (i % 3 == 0) {
      button2.Hide();
      button3.Show();
      button4.Show();
      button2.Refresh();
      button3.Refresh();
      button4.Refresh();
    } else if (i % 3 == 1) {
      button2.Show();
      button3.Hide();
      button4.Show();
      button2.Refresh();
      button3.Refresh();
      button4.Refresh();

    } else if (i % 3 == 2) {
      button2.Show();
      button3.Show();
      button4.Hide();
      button2.Refresh();
      button3.Refresh();
      button4.Refresh();

    }

    clientMessage(i);

  }

But the form freezes and the buttons don't go invisible (hiden). but the program works and gives output.

  • I tried putting this hiding function in the client message function it doesn't work.
  • I tried putting refresh for all buttons in form1_load it doesn't work.
  • I tried using buttonx.visible = false; it doesn't work.
  • I tried using label and imagebox as a cover it doesn't work

Inside the clientMessage function, there's an TCP/Ip connection and the picturebox function works. here's what the client-side looks like :

void clientMessage(int i) {

   try {

     TcpClient client = new TcpClient("127.0.0.1", 1071);
     string message = "Portakal";

     pictureBox2.Image = ConniesClient.Properties.Resources.redButton;
     pictureBox2.Refresh();
     System.Threading.Thread.Sleep(100);
     Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
     NetworkStream stream = client.GetStream();

     stream.Write(data, 0, data.Length);

     Debug.WriteLine("Sent from client : " + message);

     data = new byte[256];

     String responseData = String.Empty;
     Int32 bytes = stream.Read(data, 0, data.Length);
     responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
     Debug.WriteLine("Received at client : " + responseData);

     pictureBox3.Image = ConniesClient.Properties.Resources.redButton;
     pictureBox3.Refresh();

     System.Threading.Thread.Sleep(100);

     stream.Close();
     client.Close();

   } catch (ArgumentNullException e) {
     Console.WriteLine("ArgumentNullException: {0}", e);
   } catch (SocketException e) {
     Console.WriteLine("SocketException: {0}", e);
   }

 }

As @Bent Tranberg and @Steve mentioned in the comments the problem is caused by threads so here's the little come around about this problem it's probably not the best way to do it but it's working :

private void button1_Click(object sender, EventArgs e)
{

    for (int i = 0; i < 400; i++)
    {

        if (i % 3 == 0)
        {
            pictureBox9.Hide();
            pictureBox9.Refresh();
            pictureBox10.Show();
            pictureBox10.Refresh();
            pictureBox11.Show();
            pictureBox11.Refresh();
            System.Threading.Thread.Sleep(100);
        }
        else if (i % 3 == 1)
        {

            pictureBox9.Show();
            pictureBox9.Refresh();
            pictureBox10.Hide();
            pictureBox10.Refresh();
            pictureBox11.Show();
            pictureBox11.Refresh();
            System.Threading.Thread.Sleep(100);

        }
        else if (i % 3 == 2)
        {
            pictureBox9.Show();
            pictureBox9.Refresh();
            pictureBox10.Show();
            pictureBox10.Refresh();
            pictureBox11.Hide();
            pictureBox11.Refresh();
            System.Threading.Thread.Sleep(100);
        }
     

        clientMessage();
    }
}

As I mentioned PictureBox was working so I used 3 separate PictureBox and hide them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM