如何在运行时更改标签forecolor [英] how to change label forecolor at runtime

查看:113
本文介绍了如何在运行时更改标签forecolor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试制作MDI application.i在运行时添加标签,我想用颜色对话框更改文本标签的颜色。我尝试使用以下代码



i try to make MDI application.i add a label at runtime and i want to change color of text label with color dialog. i try to use the following code

private void colorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form actif = this.ActiveMdiChild;
            Label label = (Label)actif.ActiveControl;
            Label lbl = new Label();
            
            ColorDialog colordialog1 = new ColorDialog();
            if (colordialog1.ShowDialog()!=System.Windows.Forms.DialogResult.Cancel)
            {
                lbl.ForeColor = colordialog1.Color; //error
            }      

        }



我如何在运行时更改标签的属性?


how can i change properties of label at runtime ?

推荐答案

你不会在那里得到一个错误的确切代码你正在显示 - 它不会做任何事情,但你也不会得到错误。

如果你试图设置的ForeColor属性,你几乎肯定会收到错误标签但是,因为MSDN页面非常明确地说:

You won't get an error there with the exact code you are showing - it won't do anything, but you won't get an error either.
You almost certainly will get an error if you try to set the ForeColor property of label though, as the MSDN page says very explicitly:
In order to receive a valid value from this property, the object that calls it must either contain or be contained in the control it is calling. If one form tries to call another form's ActiveControl properties, it will receive an undefined value. In this case, you need to define your own communication mechanism between the forms to pass this data.





因为绝对不是这种情况在这里,你会发现一些奇怪的错误...



不要做那样的事情:正确地做这件事并不是很难。

请参阅:在两种表格之间传递信息,第1部分:父母对儿童 [ ^ ]并实现属性方法



Since that is definitely not the case here, you will get some odd errors occuring...

Don't do things like that: it's not exactly difficult to do it properly.
See here: Transferring information between two forms, Part 1: Parent to Child[^] and implement the "Property method"


这篇关于如何在运行时更改标签forecolor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆