question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use of `new` on Text property of Button

See original GitHub issue

Describe the bug Button has new on its Text property. This means that if you maintain a reference to it as a View (e.g. keeping a list of views in your class) and assign its .Text property then it is not properly preserved for use later on if you reference it as its explicit Type again.

This could be problematic in quite a few situations especially if using reflection. Is it possible to make Button work without the new keyword or make the Text property virtual and use override instead of new in Button if inheritence is the only way to make it work?

[Fact]
public void TestAssignTextToButton ()
{
	View b = new Button ();
	b.Text = "heya";
	Assert.Equal ("heya", b.Text);

	// fails here
	Assert.Equal ("heya", ((Button)b).Text);
}

image

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
tznindcommented, Mar 1, 2022

@tznind here my changes related with this: https://github.com/BDisp/gui.cs/tree/button-new-text

What do you think?

That looks great! I think I got a bit lost in the text formatter and where everything was stored. But that looks like it ticks all the boxes 👍

1reaction
tznindcommented, Feb 28, 2022

Hmnnn I looked into it in https://github.com/tznind/gui.cs/tree/button-new-text but looks like a lot of work. The Text property on View has the textFormatter and its own layout logic. I didn’t want to make a lot of changes for something so small like this.

In my branch I changed Button.Text it to use override and function as it does now (call base.Text) but this means that Text always returns the formatted name (which is nice and consistent at least). But thats a breaking change and could be bad if people have code like if(btn.Text == "Ok") so I think we just have to live with the replication for now as its less disruptive.

But to make crystal clear what the current situation is. The following test passes:

[Fact]
public void Constructors_Defaults ()
{
     var btn = new Button ();
   
     // If you have a reference to Button in a variable of Type Button you get this:
     Assert.Equal (string.Empty, btn.Text);

    // If you have a reference to Button in a variable of Type View you get this:
     Assert.Equal ("[  ]", ((View)btn).Text);

     // This was in the test previously but it is the same as doing a cast (above line)
     Assert.Equal ("[  ]", btn.GetType ().BaseType.GetProperty ("Text").GetValue (btn).ToString ());
      ...

I’m going to leave this for now and just put a workaround in my code 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Button.Text Property (System.Web.UI.WebControls)
Use the Text property to specify or determine the caption to display in the Button control. The value of this property, when set,...
Read more >
Creating a buttons within buttons with more than one text ...
What I want to do is add an extra Text property and some buttons in my default button and when the button is...
Read more >
The Button element - HTML: HyperText Markup Language
The <button> HTML element is an interactive element activated by a user with a mouse, keyboard, finger, voice command, or other assistive ...
Read more >
Changing Button's Text - JavaScript
It checks if the button can be changed with the childNodes property first. If not, then it will try again using the value...
Read more >
Providing button labels that describe the purpose of a button
The objective of this technique is to describe the purpose of a button by providing descriptive text as the button's accessible name. The...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found