Link to home
Start Free TrialLog in
Avatar of Marc Davis
Marc DavisFlag for United States of America

asked on

Custom control with label - change

Hi all,

I have a question that hopefully someone can hopefully assist with...

I have a Windows application and a CustomControl on that application. The custom control is for a fully name. So, it will display a label of "First name" and a textbox field for it. Then it will have a "Last name" label and a textbox field for it.

My question is there is one area where I want to change the font of the control to bold BUT that will change the labels to bold as well. I don't want that. I want the values in the textbox to be bold but I want the labels to be normal.

What I was thinking was that I change the font of the control (when used in this instance) to bold and THEN change the font of the labels in the custom control to not be bold.

If there a way to achieve that?

Any assistance on that aspect would be greatly appreciated.

Thanks

Avatar of joriszwaenepoel
joriszwaenepoel
Flag of Belgium image

In the designer of the user control:
select the label controls, go to the properties window and set the Font.Bold property to True, then back to False.  You will see that the in the properties-window, the Font property for the label is now diplayed in Bold, meaning it is no longer the default value.  It is now fixed to "not bold".

Recompile and test
Avatar of MikeDotNet555
MikeDotNet555

You cannot change the Font of the textbox?
Avatar of Marc Davis

ASKER

joriszwaenepoel,

Yes, that is a standard aspect. Even if I do that in the designer of the window where I'm USING the custom control I can do that. But that doesn't dynamically change it for when I need it.

So the default is regular but when I change the property of the control (being used in a another window) then it will change BOTH First name and Last name to bold. I do not want that. I want the values in the textboxes to be bold but the labels to be regular.

Does that makes sense?
ASKER CERTIFIED SOLUTION
Avatar of joriszwaenepoel
joriszwaenepoel
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
MikeDotNet555,

Yes, if I could I would do that but HOW do I/can I access the controls within a custom control? Basically, how can I either access the label control or the textbox control?

For instance, if I have a customer usercontrol defined as ucFullname and that ucFullname is defined as having 2 labels and 2 text boxes:

First name ______________
Last name ______________

Note: the "_" represents a text box.

Now, I have a window called like Demographics and in there I use the ucFullName control.

In the Demographics, how can I access the textbox of the First name so I can change the font or what have you? (Or how can I access the label of "First name")?

I can't do something like:

ucFullName.lblFirstName.Font = Bold;

Or anything like that.
joriszwaenepoel,

That control is used in other forms as well, I can't change the control itself at a global level.

In the message, I responded to MikeDotNet555, I tried to further explain it.

I tried what you mentioned and either I'm just not understanding or am missing something because that is not doing it.
joriszwaenepoel,

Ok, I see what you're talking about. However, that really, really doesn't make a lot of sense. Can you explain that one a little as to why/how that works?

I'm still very interesting in knowing how that can work out with accessing the elements outside of the control like what I mentioned to MikeDotNet555.
I wasn't aware you couldn't modify the control itself. Are the modifiers protected by any chance? You might be able to access the textbox if it is protected and not private, Otherway I would suggest maby making a new control (if it's only a control that hold 2 labels and 2 textbox it shouldn't be really hard to change it).
Hi,

If you set the Label's font.bold property explicitly to false, then it is written in de the code behind the usercontrol (usercontrol.designer.vb).  
If you later change the font of the usercontrol itself, that only changes for those child-controls on the usercontrol that do not have their own font-settings written explicitly.

If you try what I suggested, and then go looking in the "disigner.vb" code, you wil see code like this:

        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
        Me.Label1.Location = New System.Drawing.Point(14, 17)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(39, 13)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Label1"
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(90, 16)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(110, 20)
        Me.TextBox1.TabIndex = 1
        Me.TextBox1.Text = "demo"

You see that the Label control has an explicit Font setting, and the Textbox has not.

--

Other suggestion: if you change the Modifiers property (in the property grid) of the TextBoxes, then you can control those textboxes from the Form where you use the UserControl, like this:

Me.MyUserControl1.TextBox1.Font = ....

But this is not considered to be a good practice.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have confirmed the custom control has some private settings on the scope. I have to see about rethinking some of this because I'm not so sure I can change the user control due to dependencies on other windows forms.

I'll take a look and provide mroe detail on what tack may be most beneficial in this situation.

More to come ASAP.
I was able to take a different tact. Actually, I had to due to dependencies.

Thanks for the info and greatly appreciate it.