To make a long story short, I needed a property of a button to act as a "secondary text property" and retain the original .Text
value of a button, and the only String
property that wasn't ReadOnly
was the .Name
property.
I was sure it was going to break on runtime (as tons of code references the name of the control) but just for kicks, I wanted to run it and see what it would do.
I my shock and surprise, IT DIDN'T BREAK! And it actually served my purpose of temporarily holding the original text of the button, in time for me to retrieve it again.
Thus my two questions
###Thus my two questions (like the surprised kid after he pushed a glass plate on the floor):
- Why didn't it break?
- And why shouldn't I do it again?
I have a few guesses, but I wouldn't know how to confirm them, so this is my first venture for answers:
I was thinking one possibility is that the
.Name
property "already served its purpose" by runtime as the code is already compiled, and all references are already made to the instance, and therefore, altering the.Name
property does nothing in runtime. But that almost seems too simple an answer.Maybe I was not actually changing the property, but that seems a silly thing to consider since I was able to retrieve the value.
I finally considered that I've entered the twilight zone and I'll be hunted down and imprisoned by Microsoft for not learning the moral lesson of following proper coding conventions. (Sarcasm; no need to edit my post for this)
I'm inclined to think #1 is the answer, but it seems too simple, and I can't help but think that I'm going to get scolded for messing with it.
Any insight or notes about conventions (even a slap on the wrist) would be appreciated.
###Added code example:
Added code example:
(NOTE: I now understand .Tag
is a better to use than .Name
for this purpose, but this is still a good example for my question.)
Private Sub btn_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs)
Dim btn As Button = DirectCast(sender, Button)
'Make sure to only pull the text when it actually has text.
If btn.Text > " " Then
btn.Name = btn.Text
End If
btn.Text = String.Empty
'Set flags to center text on button
Dim flags As TextFormatFlags = TextFormatFlags.HorizontalCenter Or TextFormatFlags.VerticalCenter Or TextFormatFlags.WordBreak 'center the text
'Render the text onto the button
TextRenderer.DrawText(e.Graphics, btn.Name, btn.Font, e.ClipRectangle, btn.ForeColor, flags)
End Sub