-1

I am a new student making a hotel reservation windows application on VStudio using C#. I want to have a label that always shows text that changed depending on what the user has chosen for their reservation. I have everything down except how to make the text always show rather than having to click on the label first. How do I do this?

i am using Windows Form App (.NET Framework). I should have been clearer in my question but what i have is multiple room options using RadioButtons, accomodations using CheckBoxes, and the number of days the person is staying for in a textbox. And in my code i have some math to calculate the price of the reservation. And i have the price always in a variable named "price". Basically what i want is the value of that variable to always show on the label, whatever its value may be, and without having to click on the label or do anything other than select the options for the reservation.

Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • 1
    Please show current implementation, it would be easier to help you. See [mcve]. – Sinatr Oct 06 '20 at 10:17
  • i did. sry for not doing it from the start. – MooHY MooJY Oct 06 '20 at 12:00
  • When updating `price` assign its new value to a `Label.Text`. This can be done in property setter or achieved by using [bindings](https://stackoverflow.com/q/3147043/1997232). – Sinatr Oct 06 '20 at 12:05

3 Answers3

1

"in my code i have some math to calculate the price of the reservation. And i have the price always in a variable named "price". Basically what i want is the value of that variable to always show on the label, whatever its value may be, and without having to click on the label or do anything other than select the options for the reservation."

One way to do this without databinding is to make a property out of the price variable, and then use the set method of the property to update the label. set is called any time the property is changed, so this will always keep the label updated.

public partial class Form1 : Form
{
    // Backing field holds the actual price. Only the 
    // property setter should change this value.
    private decimal price;

    // Price property allows you to take action whenever 
    // the field is accessed (retrieved or set)
    private decimal Price
    {
        get { return price; }

        set
        {
            // In the property setter we not only set 
            // the value, we also update the label
            price = value;
            lblPrice.Text = price.ToString();
        }
    }

    // Sample method that calculates price
    private void CalculatePrice()
    {
        // Do some math to calculate the price here
        decimal result = (itemCost + markup) * quantity;

        // Set our property (not the backing field), 
        // which will automatically update the label
        Price = result;
    }

    // Rest of class code omitted
}

Just make sure that your code always changes the Price property, and not the price backing field!

Rufus L
  • 36,127
  • 5
  • 30
  • 43
0

If you want to use the Databinding infrastructure, and reflect the changes made to a value, you need a way to notify the UI about the changes made to the binding value.

So the best way to do that is to use a property and implement the INotifyPropertyChanged interface, like this:

class frmFoo : Form, INotifyPropertyChanged
{        
    private string _foo;

    public string Foo
    {
        get { return _foo; }
        set
        {
            _foo = value;
            OnPropertyChanged("Foo");
        }
    }

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

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
}

Also remember that you need to setup the binding on the label first:

public frmFoo()
{
    InitializeComponent();
    lblTest.DataBindings.Add(new Binding("Text", this, "Foo"));
}
Saud Khan
  • 786
  • 9
  • 24
-1

I'm assuming that you have set a name of the label is : "testLabel". And have implemented textbox TextChanged Listener

private void textBox_TextChanged(object sender, EventArgs e) {
        testLabel.Text = textBox.Text;
}
Saud Khan
  • 786
  • 9
  • 24
  • This is valid for WinForm applications but if he's working with Wpf, there are better ways to manage it (see here: https://learn.microsoft.com/en-us/dotnet/desktop/wpf/data/data-binding-overview?view=netdesktop-5.0) – Cem.S Oct 06 '20 at 11:00
  • @CemSOYDING You are right, but what I believe is he is working on WinForm applications only. so above solution should be a valid for him. – Saud Khan Oct 06 '20 at 11:09
  • i edited my question to give more information that i should have given from the start. if you could take another look at it that would be great :D – MooHY MooJY Oct 06 '20 at 12:01
  • OK, so basically you want to bind a variable to label, am I right? – Saud Khan Oct 06 '20 at 12:03
  • yes, and have the value of that variable constantly displayed on the label – MooHY MooJY Oct 06 '20 at 12:30
  • Please check https://stackoverflow.com/a/64225465/4345141 solution – Saud Khan Oct 06 '20 at 12:59