tags:

views:

75

answers:

3

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?

A: 

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.

drharris
thanks, more explanations please :)
BDotA
Gave a quick example, let me know if you need more.
drharris
thanks, I am thinking of how to take advantage of this technique in my question but I learned a lot from this example. it is a nice way of updating controls based on change of other control just with one line of code.. nice, I should study more abut it , it is powerful.
BDotA
In your custom control, you may want to expose a Text property that is changed whenever the label should be updated. Then, you simply bind a label to the Text property of an instance of that custom control. No additional, messy code, and it properly encapsulates everything. The current design forces the user control to maintain a Label that it may or may not need. Passive binding eliminates such a requirement.
drharris
+1  A: 

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?

Anthony Potts
but why I could change the font to Bold, but cannot change its Text?
BDotA
I updated my response. Make sure you are setting some default font before changing it programmatically.
Anthony Potts
font was Ok... since I am reading it in AssignedLabel.Font and then regardless of what ever it is, I am just making it Bold
BDotA
A: 

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.

BDotA
you mean the " *" + assignedLabel.Text?
Anthony Potts
yes... correct.
BDotA