ich möchte aus einem anderen Thread auf ein Form zugreifen.
Das funktioniert über BeginInvoke() auf einen Delegaten auch ganz gut, die Methode wird aufgerufen und Form.Text und ein Label nehmen die neuen Werte an.
Warum wird nun aber mein Panel bzw. der Button darauf nicht angezeigt? Ich nehme an ich habe etwas wesentliches übersehen und finde es nur nicht da ich mich seit einer ganzen Weile nicht mehr mit Forms beschäftigt habe.
Noch eine Frage hinterher:
Wenn ich einen neuen Thread starte und das Form mit ShowDialog() anzeige, wird der Thread beendet wenn das Form geschlossen wird?
Danke schonmal für eure Antworten.
Formerstellung, Updatemethode und Threadstart
protected override void startProcessing()
{
//Create the form that is showed on screen
thread = new Thread(new ThreadStart(startFormThread));
thread.Start();
}
void outputMgr_ContainerChange(ContainerChangeEventArgs e)
{
form.BeginInvoke(form.delegateUpdateForm, e.NewContainer);
}
private void startFormThread()
{
form = new TSForm();
form.ShowDialog();
}
protected override void shutDownDevice()
{
form.BeginInvoke(delegateCloseForm);
}
Das Formelement:
class TSForm:Form
{
private Label infoline;
private Panel p;
public delegate void DelegateUpdateForm(Container c);
public DelegateUpdateForm delegateUpdateForm;
public delegate void DelegateCloseForm();
public DelegateCloseForm delegateCloseForm;
public TSForm()
{
SystemSettings.ScreenOrientation = ScreenOrientation.Angle90;
this.ControlBox = false;
this.Width = 320;
this.Height = 200;
this.Text = "Testform";
infoline = new Label();
infoline.Text = "Info:";
infoline.Width=200;
infoline.Height=30;
infoline.Anchor = AnchorStyles.Top;
Controls.Add(infoline);
delegateUpdateForm = new DelegateUpdateForm(updateContainer);
delegateCloseForm = new DelegateCloseForm(closeForm);
}
private void updateContainer(Container c)
{
ListSelector ls = (ListSelector)c.Elements[0];
this.Text = c.Name + "<->" + ls.Name + "<->" + ls.Options[ls.SelectedOption].Name;
this.infoline.Text = ls.Options[ls.SelectedOption].Name;
p = new Panel();
p.Size = new Size(200, 200);
p.Visible = true;
p.Anchor = AnchorStyles.Bottom;
Button b1 = new Button();
b1.Text = ls.Options[ls.SelectedOption].Name;
b1.Size = new Size(100, 20);
p.Controls.Add(b1);
Controls.Add(p);
this.Refresh();
this.Update();
this.Show();
UICore.Logger.Info("Anzahl Controls: " + this.Controls.Count);
}
}