0

I want to change the font of my label so that it is white with a black outline. And I have found a way to doing so for a label I create in code, but I would like to add the outlining for already existing control labels.

In a previous stackoverflow question I have found the class (the CustomLabel class) which adds an outline to a label.

I know I can create a new label with the following code, and use this class, but I am unable to find any information how I can use this class (CustomLabel) for my control class (label) and add the outline to already existing labels in my Windows Form.

Form newForm = new Form();
CustomLabel newLabel = new CustomLabel();
newForm.Controls.Add(newLabel);

newLabel.BackColor = Color.Black;
newLabel.Font = new System.Drawing.Font("Impact", 18F,
FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
newLabel.ForeColor = Color.Crimson;
newLabel.OutlineForeColor = Color.Black;
newLabel.OutlineWidth = 2;
newLabel.Text = "test text";

newForm.Show();
newForm.TopMost = true;
newLabel.AutoSize = true;
newLabel.Location = new Point(100, 250);

The problem I run into is I don't know how to apply this this class to the control (label), and several labels I have created in my design (GUI).

I would like if I could do something like:

// myLabel is made in the designer window
myLabel.OutlineForeColor = Color.Black;
myLabel.OutlineWidth = 2;

or another idea is, to create a function which takes all the custom properties for each label in my designer (a label is the argument for the function), adds them to newLabel (same as above), however newLabel has two custom properties like OutLineForeColor and OutlineWidth and then finally all the properties of newLabel is coppied over to the argument of the function (one of the custom labels). So something like this:

private void addNewProperties(Label myFormLabel)
{
    CustomLabel newLabel = new CustomLabel();
    newLabel.BackColor = myFormLabel.BackColor;
    newLabel.Font = myFormLabel.Font;
    newLabel.ForeColor = myFormLabel.ForeColor;
    newLabel.OutlineForeColor = Color.White;
    newLabel.OutlineWidth = 2;
    newLabel.Text = myFormLabel.Text;
    newLabel.Location = myFormLabel.Location;

    myFormLabel = newLabel;
}

Finally, I wonder if a different approach would be easier; if there's a way where I can just add the CustomLabel class to the Label class itself in C#, so that I directly can add the desired methods to my form labels.

Community
  • 1
  • 1
  • I also borrowed some ideas for the example code from [this post](http://stackoverflow.com/questions/4432772/how-can-i-add-borders-to-label-in-windows-forms/23354292#23354292) – Max Vaehrens Apr 10 '17 at 13:56
  • Are you looking for the rectangle of the label, or a shadow of text? – BugFinder Apr 10 '17 at 13:57
  • @BugFinder the shadow of the text. – Max Vaehrens Apr 10 '17 at 20:18
  • OK, so you have the example, what are you actually struggling with? – BugFinder Apr 11 '17 at 07:00
  • @BugFinder I'd like to apply the OutlineForeColor and OutlineWidth, for labels that exist in my designer of the windows form. So like I do in the example, but for an object that I did not instance in my code. – Max Vaehrens Apr 11 '17 at 07:07
  • But you havent said whats stopping you – BugFinder Apr 11 '17 at 07:07
  • @BugFinder what's stopping me is I don't know and haven't been able to find an answer to how I can apply a custom class and it's methods to a control (label) in C# – Max Vaehrens Apr 11 '17 at 07:10
  • I dont doubt this at all, but you dont seem to be able to say either what it is you dont get.. cos until you do, as Im the only one posting on here, I dont get what it is I can tell you so you can move on with it – BugFinder Apr 11 '17 at 07:12
  • @BugFinder you're right, when I'm back on my PC I'll try to edit and example of how I'd like to apply the class for my Windows form, and post the error message if I get any. – Max Vaehrens Apr 11 '17 at 07:15
  • part of me is guessing your problem is replacing your labels with custom labels.. but until you are a little specific of your issue.. I can guess all I like and be completely wrong. – BugFinder Apr 11 '17 at 07:18
  • @BugFinder I added a few examples of how I'd approach trying to fix the issue. You're right that one thing I would like to do is to copy the properties (for the outline) from the label to my forms' labels. When I tried this it gave me no error but nor did it have any effect on the labels. – Max Vaehrens Apr 11 '17 at 08:58
  • Then edit your form.cs and change the "Label" to "CustomLabel" – BugFinder Apr 11 '17 at 09:00
  • @BugFinder Do I simply change the "Label" objects to CustomLabel in my Form.Designer.cs file? And this makes sense, since the CustomLabel class already inherits the Label class, I can simply replace them. – Max Vaehrens Apr 11 '17 at 09:06
  • yes.. or you would normally have made them as customlabels in the first place – BugFinder Apr 11 '17 at 11:47

0 Answers0