无法更改标签文本!

发布于 2024-09-06 08:09:43 字数 528 浏览 1 评论 0原文

我创建了一个自定义控件并向其添加了标签属性,因此在设计时我们可以选择一个标签并将其分配给该控件。所以基本上我希望如果将标签分配给该控件,其文本应如下更改,并且其文本应更改为粗体字体,所以这里是该代码:

private Label assignedLabel;
public Label AssignedLabel
{
    get
    {
        return assignedLabel;
    }
    set
    {
        assignedLabel = value;
        assignedLabel.Text = @"*" + assignedLabel.Text; 
        assignedLabel.Font = new Font(AssignedLabel.Font, FontStyle.Bold);
        AssignedLabel.Refresh();
    }
}

问题是基于分配的字体上方的代码标签已正确更改为粗体字体,但其文本未生效。 为什么会发生这种情况?我该如何解决这个问题?

I have created a custom control and added a label property to it so at design time we can pick a Label and assign it to that control. so basically I want that if a label is assigned to that control, its text should change as below and also its text should change to bold font, so here is that code:

private Label assignedLabel;
public Label AssignedLabel
{
    get
    {
        return assignedLabel;
    }
    set
    {
        assignedLabel = value;
        assignedLabel.Text = @"*" + assignedLabel.Text; 
        assignedLabel.Font = new Font(AssignedLabel.Font, FontStyle.Bold);
        AssignedLabel.Refresh();
    }
}

the problem is that based on the code above the Font of that assigned label is correctly changing to Bold font, but its Text is not taking affect.
why is that happening? how can I fix this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神妖 2024-09-13 08:09:43

我认为除非在控件的 InitializeComponent() 子例程中设置它,否则您不能这样做。

实际上,在更改字体之前是否已将字体设置为默认值?

I don't think you can do that unless it is set in the InitializeComponent() subroutine for the control.

Actually, is the font being set to a default before you change it?

萌化 2024-09-13 08:09:43

听起来您确实应该探索数据绑定。这非常适合处理基于其他控件状态更新标签的内部结构。

例如,如果您有两个控件,一个 TextBox (textBox1) 和一个 Label (label1),则只要您想要绑定它们,您就可以调用以下代码行:

label1.DataBindings.Add("Text", textBox1, "Text");

这会将 label1 的“Text”属性绑定到“Text” textBox1 对象的属性。您可以在此处使用任何对象。 “正确”的方法是创建一个包含许多变量当前状态的基础数据源,并将所有控件绑定到该数据源。但这种类型的代码会让你快速上手。

It really sounds like you should explore DataBinding. This is perfect for handling the internals of updating a label based on some other control's state.

For example, if you have two controls, a TextBox (textBox1) and a Label (label1), you could call the following line of code whenever you want to bind them:

label1.DataBindings.Add("Text", textBox1, "Text");

This binds the "Text" property of label1 to the "Text" property of the textBox1 object. You can use any object here. The "correct" way to do it would be to create an underlying data source that contains the current state of many variables, and bind all controls to that data source. But this type of code will get you going quickly.

情深缘浅 2024-09-13 08:09:43

嗯!代码刚刚开始工作!有一个小问题,每次我运行表单时都会添加“*”,但这应该是一个简单的修复。
欢迎任何其他实现此目标的好方法:)
谢谢大家。

Hmmm! the code just started working! there is a minor issue that it is adding "*" every time I run the form, but it should be an easy fix.
any other nice ways to accomplish this goal are welcome :)
thanks all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文