?
Solved

Custom control with label  - change

Posted on 2009-12-18
12
Medium Priority
?
320 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:davism
  • 6
  • 3
  • 3
12 Comments
 
LVL 10

Expert Comment

by:joriszwaenepoel
ID: 26081986
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
0
 
LVL 3

Expert Comment

by:MikeDotNet555
ID: 26082018
You cannot change the Font of the textbox?
0
 
LVL 1

Author Comment

by:davism
ID: 26082044
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?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 10

Accepted Solution

by:
joriszwaenepoel earned 280 total points
ID: 26082093
Have you tried it?
I tried it before I posted, and if you do what I described, you can modify the Font property of the UserControl on the Form where it is used, without affecting the font of the label controls.

0
 
LVL 1

Author Comment

by:davism
ID: 26082104
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.
0
 
LVL 1

Author Comment

by:davism
ID: 26082156
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.
0
 
LVL 1

Author Comment

by:davism
ID: 26082276
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.
0
 
LVL 3

Expert Comment

by:MikeDotNet555
ID: 26082308
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).
0
 
LVL 10

Expert Comment

by:joriszwaenepoel
ID: 26082664
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.
0
 
LVL 3

Assisted Solution

by:MikeDotNet555
MikeDotNet555 earned 220 total points
ID: 26083306
I don't think he have access to edit the control with an editor (or then he could simply change the value of the textbox itself).

I think another solution could be using the Control.Controls to find the right control, I think the Control.Controls.Find method take the name of the control as argument so you could probably do something like this:

((TextBox)Control.Controls.Find("myTextBox", false)).Font = SomeFont;

or loop thru all of them:

foreach(Control control in Control.Controls)
{
  if(control is TextBox)
  {
    //check if its the right textbox and change the font accordingly
  }
}

Even then I am not sure if you can modify a control that is private using that way, but it might be worth a try.
0
 
LVL 1

Author Comment

by:davism
ID: 26083695
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.
0
 
LVL 1

Author Closing Comment

by:davism
ID: 31667810
I was able to take a different tact. Actually, I had to due to dependencies.

Thanks for the info and greatly appreciate it.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Suggested Courses
Course of the Month8 days, 15 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question