Click here to Skip to main content
15,881,281 members
Please Sign up or sign in to vote.
3.40/5 (2 votes)
See more:
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

C#
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 ?
Posted
Updated 17-May-20 8:36am
Comments
[no name] 23-May-14 15:18pm    
Even if you did not get whatever error you are getting, you are creating a new label, setting the forecolor and then letting go out of scope without doing anything else with it. What error is it that you are getting?
Abhishek Sivasubramanian 23-May-14 15:22pm    
Are you getting some error on that line you highlighted OR your problem is that you cannot see the forecolor being applied ?

If later is the case, you need to place the dynamically generated label to the form's control collection or a panel. Then only you can see that.
[no name] 23-May-14 15:29pm    
Try to solve it step by step.
Are you sure that you ask the right property from ColorDialog?
Maybe not, so the next step is _not_ to assign the Color returned from the dlg, assign a fix value you are shure to recognize. If your Label does still not show this fix Color....then I'm out of order ;)

uups jsut have a look to solution 1 ;)
Emre Ataseven 25-May-14 7:45am    
It doesn't look like a good way whatever you're trying to do

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.


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"
 
Share this answer
 
Label1.ForeColor = System.Drawing.Color.DodgerBlue;
Label1.ForeColor = System.Drawing.Color.red;

etc...
 
Share this answer
 
v2
Comments
Dave Kreskowiak 17-May-20 15:02pm    
Not an answer to the 6 year old question. You failed to understand the problem with the OP's code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900