0

I've some event which fires. so I've to invoke method which makes a couple of changes in the state of instance and his property which contains multiple form controls, like panels, picturebox, button. And at last these couple of controls should be moved from one panel1 to another (panel2.add).

So I have a problem with threads. It's not problem to change one label with such function:

private void SetText(string valuefromdifferentthread) {

        if (this.label1.InvokeRequired)
        {
            SetTextCallback d = new SetTextCallback(SetText);
            this.Invoke(d, new object[] { rfid });
        }
        else
        {
            label1.text="valuefromdifferentthread"                
        }
    }

But how to make changes for a couple of controls? (without writing method for each of them and finally how to change parent control)

I've even tried to put to the SomeClass StateChanged another event handler which fires when "state" comes "true" (and state becomes true when fires first event) but anyway I have a problems with treading

"Controls created on one thread cannot be parented to a control on a different thread."

below I wrote fake (small) code of my project

//-------------------------------------------------------------------------- List<SomeClass> allinstancesinList;

Event is

SerialPort.DataReceived+=dothis;

private void dothis(...)
{
    var mystring=serialPort.ReadLine();
    var curInstance=allinstancesinList.Single(w=>w.id=mystring);
    curInstance.changed=true;
    runthisfunction(curInstance);
}






runthisfunction(SomeClass sc)
{
    //...here i make multiple operations with form controls of instance
    sc._ContainerMainPanelBackgroundImageDefault=somepicture;
    sc.__ContainerMainPanelBackColorSelected =backcolor;
    //and finally i have to add to another panel

    Panel2.add(sc.ContainerMainPanel);//which contains other panel and images
}


Class SomeClass
{
//...
        PictureBox _ControlPictureBox;
        Panel _ContainerMainPanel;
        Label _ControlLabelName;
        //BackgroundImage Clicked and default and ControlSelected;
        Image _ContainerMainPanelBackgroundImageDefault;
        Color _ContainerMainPanelBackColorDefault = Color.White;
        Image _ContainerMainPanelBackgroundImageSelected;
        Color _ContainerMainPanelBackColorSelected = Color.Yellow;



//...
}

Thanks for advance

I can't post my question as an answer (within 8 hours) so I edit it by the link of Martin Mulder (thanks for him) I've found a very simple way ))))

private void DoGUISwitch()
{ 
    Invoke( ( MethodInvoker ) delegate {
        object1.Visible = true;
        object2.Visible = false;
    }
} 
Community
  • 1
  • 1
Mirvel
  • 143
  • 2
  • 11

0 Answers0