© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
1 reply
dandida

Event handlers not registering

hello I have the following code that makes use of invoke and its called in a separate thread

 private void Disconnect()
   {
     textBox1.Invoke(changeStateTextBoxDisconnect);
     button1.Invoke(changeStateButton1Disconnect);
     button2.Invoke(changeStateButton2Disconnect);
     button3.Invoke(changeStateButton3Disconnect);
   }
 private void Disconnect()
   {
     textBox1.Invoke(changeStateTextBoxDisconnect);
     button1.Invoke(changeStateButton1Disconnect);
     button2.Invoke(changeStateButton2Disconnect);
     button3.Invoke(changeStateButton3Disconnect);
   }


The events that are not being registered are in button 1 this is the code for the function being called.

  private void changeStateButton1Disconnect()
  {
    button1.Text = "Ingresar";
    button1.MouseClick -= Disconnect;
    button1.MouseClick += ConnectionRequest;
  }
  private void changeStateButton1Disconnect()
  {
    button1.Text = "Ingresar";
    button1.MouseClick -= Disconnect;
    button1.MouseClick += ConnectionRequest;
  }

The problem here is those events dont get registered and when I try to click the control once again it does not return to the appropiate flow given by this function

   private void ConnectionRequest(object sender, MouseEventArgs e)
   {
     if (textBox1.Text.Equals(string.Empty))
     {
       MessageBox.Show("Ingrese una identificacion");
       return;
     }
     _childThread = new Thread(new ParameterizedThreadStart(StartConnection)) { IsBackground = true };
     _childThread.Start(this.textBox1.Text);
     _connected = true;
     if (_connected)
     {
       button1.MouseClick -= ConnectionRequest;
       button1.MouseClick += Disconnect;
       textBox1.Enabled = false;
       button1.Text = "Desconectar";
       button2.Enabled = true;
       button3.Enabled = true;
     }
   }
   private void ConnectionRequest(object sender, MouseEventArgs e)
   {
     if (textBox1.Text.Equals(string.Empty))
     {
       MessageBox.Show("Ingrese una identificacion");
       return;
     }
     _childThread = new Thread(new ParameterizedThreadStart(StartConnection)) { IsBackground = true };
     _childThread.Start(this.textBox1.Text);
     _connected = true;
     if (_connected)
     {
       button1.MouseClick -= ConnectionRequest;
       button1.MouseClick += Disconnect;
       textBox1.Enabled = false;
       button1.Text = "Desconectar";
       button2.Enabled = true;
       button3.Enabled = true;
     }
   }


now the thing here is this function is actually the one that will start this whole thing in the fp, im sorta confused cause apparently this will not be called cause the events are not registered in the fp
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Blazor - No event handlers associated
C#CC# / help
2y ago
✅ Win Form Event Handlers question
C#CC# / help
3y ago
❔ Need help with Event Handlers to update a field.
C#CC# / help
3y ago
Generic IH Handlers?
C#CC# / help
5mo ago