As mentioned above, you should not call the Paint
event handler directly, so there is no way to pass in a string directly to it. It's the system that fires the Paint
event when the area under the control needs to be rendered. And even if you could invoke any kind of event handler, the way you would normally pass parameters to the event handler is through the second parameter of the handler which takes an EventArgs
(or derived type). The PaintEventArgs
doesn't have any room in it for you to pass in a string.
Anyway, the Label
class has a public Text
property. Just set the Text
value of the string that you want to be rendered. That is the correct way to pass a string to a Label
. Your paint event handler can read that property when it gets invoked.
So the issue you have though, is that your Paint
event handler will likely be called by the default Label
Paint
event handler so you'll have the text rendered normally horizontally first, and then when your Paint
event handler is called, you'll also render the text rotated 90 degrees (and apparently offset and enlarged if I'm reading your call to TranslateTransform()
correctly.
What you need to do is not use a standard Label
. The absolutely most correct object-oriented thing to do is write a custom class derived from the Label
. Call it a RotatedLabel
if you want. In your derived class, you should override the Paint()
method of the class, not call the base implementation, and instead use your own painting code.
A cheat that you could potentially perform without following object-oriented principles, is instead use a Panel
instead of a Label
. You can then simply attach your paint event handler to the Panel
's Paint
event. Since the Panel
also has a Text
property, you can still pass in your string that way and pick it up in your event handler. Additionally, the Panel
's default Paint
event handler doesn't draw the Text
value, you don't have to contend with the horizonally rendered text.
So that's how you pass in values to a control where it so happened that an exposed property's name actually fits perfectly with your needs. But going back to the RotatedLabel
, what if you wanted to be able to specify the actual angle of rotation? In that case, your derived class should expose a new property called Rotation
, and again your Paint()
method override should now read that property along with Text
property when rendering stuff. The nice thing about is that this approach scales up to when you need more than one thing to be passed on to the control. Just add more properties.
Okay, but what if you are feeling cheap and/or don't believe in this object-oriented thing? Then this is where Tag
property that ever WinForms control has comes into play. You can put anything into the Tag
property, and your event handlers can later read those values (and also change them if needed). So like in the example above, you could put the rotation angle into Tag
. If you have more than one value to pass in, then you'll need to create a class (or struct) that holds all the things that you want to pass in. Instantiate this class, and store the reference in the Tag
.