zetar
zetar

Reputation: 1233

What is the correct method for passing strings to labels?

I have a winform and I want to pass a series of strings to a series of labels.

This is the code:

   public partial class CourierDeliveringEnemyReport : Form
{
    public static string Label1 { get; set; }
    public static string Label2 { get; set; }
    public static string Label3 { get; set; }
    public static string Label4 { get; set; }
    public static string Label5 { get; set; }
    public string Label6 { get; set; }

    public CourierDeliveringEnemyReport()
    {

        InitializeComponent();

        label1.Text = Label1;
        label2.Text = Label2;
        label3.Text = Label3;
        label4.Text = Label4;
        label5.Text = Label5;
        label6.Text = "This is a test!";
    }

The values get set here:

CourierDeliveringEnemyReport dlg = new CourierDeliveringEnemyReport();
CourierDeliveringEnemyReport.Label1 = "Report from " + BlueArmy[GameEventList[i].ObservingUnit].Name; ;
                                string temp2 = "Enemy unit (" + RedArmy[GameEventList[i].Unit].Name + ") observed!";
CourierDeliveringEnemyReport.Label2 = temp2;
                                string temp3 = "This intelligence is " + RedArmy[GameEventList[i].Unit].LastTimeObservedByEnemy + " minutes old.";
                                CourierDeliveringEnemyReport.Label3 = temp3;

Using the debugger I can confirm that valid strings are being passed. Label1, for example, contains the string "Report from...".

The problem is that the labels do not take the string values except for label6 (the test case).

What am I doing wrong?

Thanks!

Upvotes: 1

Views: 837

Answers (5)

Reza Aghaei
Reza Aghaei

Reputation: 125257

Why not simply create public properties which get and set those labels text?

When the properties are members of the form and you want to use them only for updating those labels, using data-binding and INotifyPropertyChanged is pointless and too much work. It's enough to create some public properties in the form which assign values to labels:

public string Value1
{
    get { return this.label1.Text; }
    set { this.label1.Text = value; }
}

int value2;
public int Value2
{
    get { return value2; }
    set 
    {
        value2 = value;
        this.label2.Text = string.Format("Here is the value {0}", value);
    }
}

Then it's enough to set those properties to see is as text of those labels.

var f = new ReportForm() {Value1 = "text for label1", Value2 = 100};
f.Show();

Upvotes: 1

montelof
montelof

Reputation: 501

you have to implement INotifyPropertyChanged

  public partial class CourierDeliveringEnemyReport : Form, INotifyPropertyChanged
{
    public static string Label1 
    { 
        get
        {
            return Label1;
        } 
        set
        {
            Label1=value;
            OnPropertyChanged("Label1");
        } 
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(property));
    }
    public CourierDeliveringEnemyReport()
    {

        InitializeComponent();

        label1.Text = Label1;
        label1.DataBindings.Add(new Binding("Text", this, "Label1"));       
    }
}

Now when you change the value of Label1 it will be automatically updated to the actual label binds to it.

CourierDeliveringEnemyReport dlg = new CourierDeliveringEnemyReport();
CourierDeliveringEnemyReport.Label1 = "Report from " + BlueArmy[GameEventList[i].ObservingUnit].Name; ;

Upvotes: 0

Roman
Roman

Reputation: 1867

You are running a constructor:

CourierDeliveringEnemyReport dlg = new CourierDeliveringEnemyReport();

Which assigns label controls unassigned Label# properties. Either do as PaulF suggested, by updating label controls afterwards, or pass string values for your Label# properties into constructor, assign them, and then do your :label1.Text = Label1;

Pseudo example:

public CourierDeliveringEnemyReport(string l1, string l2, string l3, string l4, string l5, string l6)
{

    InitializeComponent();
    Label1 = l1;
    Label2 = l2;
    /* and so on */
    label1.Text = Label1;
    label2.Text = Label2;
    label3.Text = Label3;
    label4.Text = Label4;
    label5.Text = Label5;
    label6.Text = "This is a test!";
}

Upvotes: 0

keyboardP
keyboardP

Reputation: 69372

You are not binding the label to the variable, you are only setting it once. It seems you're expecting the label to update anytime the relative LabelX string is updated but this would only happen if there was a notification of some sort.

Your form, for example, can implement INotifyPropertyChanged and your Label properties can raise the notification when they're changed.

public partial class CourierDeliveringEnemyReport : Form, INotifyPropertyChanged

Then it can be implemented in a manner similar to this

private string _label1;

public string Label1
{
    get { return _label1; }
    set
    {
        if(_label1 == value) return;

        _label1 = value;
        OnPropertyChanged("Label1");
    }
}

//implement the INotifyPropertyChanged interface
public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged(string prop)
{
    if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(prop));
}

Since this is WinForms, there's no XAML markup to add bindings so you can do it in the code behind with Binding.

public CourierDeliveringEnemyReport()
{

    InitializeComponent();
    //"Text" is the property on the control to get/set (i.e. Label1.Text)
    //this is the datasource
    //"Label1" is the property in our code
    label1.DataBindings.Add(new Binding("Text", this, "Label1"))

    //add bindings for other labels
}

(Code is untested but should give an idea)

Upvotes: 2

PaulF
PaulF

Reputation: 6773

Looking at your code, you are initialising the Labelx properties BEFORE they have been assigned in the constructor. At that point they will return null, so the label has no content.

You may need another method to update the labels AFTER any update to the Labelx property.

public partial class CourierDeliveringEnemyReport : Form
{
public static string Label1 { get; set; }
public static string Label2 { get; set; }
public static string Label3 { get; set; }
public static string Label4 { get; set; }
public static string Label5 { get; set; }
public string Label6 { get; set; }

public CourierDeliveringEnemyReport()
{
    InitializeComponent();
    UpdateLabels();  // this will clear all labels except Label6
}
public void UpdateLabels()
{
    label1.Text = Label1;
    label2.Text = Label2;
    label3.Text = Label3;
    label4.Text = Label4;
    label5.Text = Label5;
    label6.Text = "This is a test!";
}


CourierDeliveringEnemyReport dlg = new CourierDeliveringEnemyReport();
CourierDeliveringEnemyReport.Label1 = "Report from " + BlueArmy[GameEventList[i].ObservingUnit].Name; ;
string temp2 = "Enemy unit (" + RedArmy[GameEventList[i].Unit].Name + ") observed!";
CourierDeliveringEnemyReport.Label2 = temp2;
string temp3 = "This intelligence is " + RedArmy[GameEventList[i].Unit].LastTimeObservedByEnemy + " minutes old.";
CourierDeliveringEnemyReport.Label3 = temp3;
CourierDeliveringEnemyReport.UpdateLabels();

Upvotes: 0

Related Questions