Skip to main content
Commonmark migration
Source Link

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):

  1. Why didn't it break?
  2. 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:

  1. 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.

  2. Maybe I was not actually changing the property, but that seems a silly thing to consider since I was able to retrieve the value.

  3. 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

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 (like the surprised kid after he pushed a glass plate on the floor):

  1. Why didn't it break?
  2. 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:

  1. 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.

  2. Maybe I was not actually changing the property, but that seems a silly thing to consider since I was able to retrieve the value.

  3. 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:

(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

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

(like the surprised kid after he pushed a glass plate on the floor):

  1. Why didn't it break?
  2. 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:

  1. 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.

  2. Maybe I was not actually changing the property, but that seems a silly thing to consider since I was able to retrieve the value.

  3. 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:

(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
Added specificity for accuracy and fixed some grammar.
Source Link
Hawkeye
  • 147
  • 1
  • 7

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 (like the surprised kid after he pushed a glass plate on the floor):

  1. Why didn't it break?
  2. 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:

  1. I was thinking one possibility is that the .Name property "already served it'sits 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.

  2. Maybe I was not actually changing the property, but that seems a silly thing to consider since I was able to retrieve the value.

  3. 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; there's 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:

(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

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 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 (like the surprised kid after he pushed a glass plate on the floor):

  1. Why didn't it break?
  2. 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:

  1. I was thinking one possibility is that the .Name property "already served it's 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.

  2. Maybe I was not actually changing the property, but that seems a silly thing to consider since I was able to retrieve the value.

  3. 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; there's no need to edit my post)

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:

(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

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 (like the surprised kid after he pushed a glass plate on the floor):

  1. Why didn't it break?
  2. 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:

  1. 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.

  2. Maybe I was not actually changing the property, but that seems a silly thing to consider since I was able to retrieve the value.

  3. 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:

(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
Added a note and another tag
Source Link
Hawkeye
  • 147
  • 1
  • 7

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 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 (like the surprised kid after he pushed a glass plate on the floor):

  1. Why didn't it break?
  2. 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:

  1. I was thinking one possibility is that the .Name property "already served it's 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.

  2. Maybe I was not actually changing the property, but that seems a silly thing to consider since I was able to retrieve the value.

  3. 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; there's no need to edit my post)

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:

(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

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 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 (like the surprised kid after he pushed a glass plate on the floor):

  1. Why didn't it break?
  2. 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:

  1. I was thinking one possibility is that the .Name property "already served it's 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.

  2. Maybe I was not actually changing the property, but that seems a silly thing to consider since I was able to retrieve the value.

  3. 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; there's no need to edit my post)

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:

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

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 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 (like the surprised kid after he pushed a glass plate on the floor):

  1. Why didn't it break?
  2. 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:

  1. I was thinking one possibility is that the .Name property "already served it's 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.

  2. Maybe I was not actually changing the property, but that seems a silly thing to consider since I was able to retrieve the value.

  3. 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; there's no need to edit my post)

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:

(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
copy-edited
Source Link
Deduplicator
  • 9.1k
  • 5
  • 33
  • 52
Loading
Added code example
Source Link
Hawkeye
  • 147
  • 1
  • 7
Loading
Fixed word
Source Link
Hawkeye
  • 147
  • 1
  • 7
Loading
Source Link
Hawkeye
  • 147
  • 1
  • 7
Loading