How to control a label from an external class

  • Thread starter Thread starter Dr_PoLish (the schnitzel)
  • Start date Start date
D

Dr_PoLish (the schnitzel)

I want to be able to control a label from an external class - i tried
to put in a 'private void LabelChange(string Text2Change2) {
label1.Text = Text2Change2 }' into my FrmMain class, and the only way i
can get it working in another class is by

class ExternalClassThing
{
public void ChangeThatLabel(string Text2Change2)
{
FrmMain mainfrm = new FrmMain();
mainfrm.Show();
mainfrm.LabelChange(Text2Change2);
}
}

quite obviously, i cant have a new window pop up each time i want to
change the label's text, so is there any way for the ExternalClassThing
to take control of the current instance of FrmMain ? and maybe even
directly use the label1 ?
 
Dr_PoLish (the schnitzel) said:
I want to be able to control a label from an external class - i tried
to put in a 'private void LabelChange(string Text2Change2) {
label1.Text = Text2Change2 }' into my FrmMain class, and the only way i
can get it working in another class is by

class ExternalClassThing
{
public void ChangeThatLabel(string Text2Change2)
{
FrmMain mainfrm = new FrmMain();
mainfrm.Show();
mainfrm.LabelChange(Text2Change2);
}
}

quite obviously, i cant have a new window pop up each time i want to
change the label's text, so is there any way for the ExternalClassThing
to take control of the current instance of FrmMain ? and maybe even
directly use the label1 ?
_if non-static_ then use:
class ExternalClassThing
{
private FrmMain mainfrm;

public ExternalClassThing(FrmMain mainfrm)
{
this.mainfrm = mainfrm;
}

public void ChangeThatLabel(string Text2Change2)
{
mainfrm.LabelChange(Text2Change2);
}
}

_if static_ then use:
class ExternalClassThing
{
public static FrmMain mainfrm;
public static void ChangeThatLabel(string Text2Change2)
{
mainfrm.LabelChange(Text2Change2);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top