【问题标题】:How can i make a label always show changing text? [closed]如何使标签始终显示不断变化的文本? [关闭]
【发布时间】:2021-01-21 05:06:54
【问题描述】:

我是一名新生,使用 C# 在 VStudio 上制作酒店预订窗口应用程序。 我想要一个标签,该标签始终显示根据用户为预订选择的内容而更改的文本。除了如何使文本 always 显示而不是必须先单击标签之外,我已经准备好了所有内容。我该怎么做?

我正在使用 Windows 窗体应用程序 (.NET Framework)。我的问题应该更清楚,但我拥有的是使用 RadioButtons 的多个房间选项,使用 CheckBoxes 的住宿,以及该人在文本框中停留的天数。在我的代码中,我有一些数学来计算预订的价格。而且我的价格总是在一个名为“价格”的变量中。基本上我想要的是该变量的值始终显示在标签上,无论它的值是什么,并且无需单击标签或执行任何其他操作,只需选择保留选项。

【问题讨论】:

  • 请显示当前的实现,它会更容易帮助你。见minimal reproducible example。
  • 我做到了。对不起,从一开始就没有这样做。
  • 更新price 时,将其新值分配给Label.Text。这可以在属性设置器中完成,也可以通过使用bindings 来实现。

标签: c# winforms label


【解决方案1】:

“在我的代码中,我有一些数学来计算预订的价格。而且我的价格总是在一个名为“price”的变量中。基本上我想要的是始终显示该变量的值标签,无论其值是多少,无需单击标签或执行任何操作,只需选择保留选项。”

在没有数据绑定的情况下执行此操作的一种方法是从price 变量中创建一个属性,然后使用该属性的set 方法来更新标签。 set 会在属性更改时调用,因此这将始终保持标签更新。

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
}

只需确保您的代码始终更改 Price 属性,而不是 price 支持字段!

【讨论】:

    【解决方案2】:

    如果您想使用数据绑定基础架构并反映对值所做的更改,您需要一种方法来通知 UI 对绑定值所做的更改。

    因此,最好的方法是使用属性并实现 INotifyPropertyChanged 接口,如下所示:

    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
    }
    

    还请记住,您需要先在标签上设置绑定:

    public frmFoo()
    {
        InitializeComponent();
        lblTest.DataBindings.Add(new Binding("Text", this, "Foo"));
    }
    

    【讨论】:

    【解决方案3】:

    我假设你设置的标签名称是:“testLabel”。并实现了文本框 TextChanged 监听器

    private void textBox_TextChanged(object sender, EventArgs e) {
            testLabel.Text = textBox.Text;
    }
    

    【讨论】:

    • 这对 WinForm 应用程序有效,但如果他正在使用 Wpf,则有更好的方法来管理它(参见此处:docs.microsoft.com/en-us/dotnet/desktop/wpf/data/…)
    • @CemSOYDING 你是对的,但我相信他只是在处理 WinForm 应用程序。所以上面的解决方案应该对他有效。
    • 我编辑了我的问题以提供更多我应该从一开始就提供的信息。如果你能再看看它会很棒:D
    • 好的,所以基本上你想将一个变量绑定到标签,对吗?
    • 是的,并且该变量的值不断显示在标签上
    猜你喜欢
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 2012-07-07
    相关资源
    最近更新 更多