0

I wonder, if there is any way to bind a string to a Label or any other control.

I thought about this kind of pseudo code:

//label        class            value
label1.Text => LocalizedStrings.someString;

This should also edit the text shown at label1 when someString's value changes. Is there any way to do this either by something similar to the above pseudo code, using the Designer or editing the Designer.cs file?

PS: I'm using Visual Studio 15 RC, C# 6.0 and .NET 4.6.

cramopy
  • 3,459
  • 6
  • 28
  • 42

2 Answers2

1

I think there is no simple way to do that in WinForms (but if there is, I would like to know).

But you can do the contrary.

For example, you can adapt your class "LocalizedString" or extend it to get a reference to the Label. Than you add code to it so that when its content changes, it changes the content in the label.

Hum...

But also, you maybe can use this: https://msdn.microsoft.com/en-us/library/system.windows.forms.binding%28v=vs.110%29.aspx

It enables the binding of a property on a control and a property on an object. It seems exactly what you want...

Here there is an example: Bind a label to a "variable"

Community
  • 1
  • 1
Jauch
  • 1,500
  • 9
  • 19
0

Try this:

Binding binding = new Binding( "Text", Properties.Settings.Default, "TranSvcAddr" );
txtTranSvcAddr.DataBindings.Add( binding );

The TextBox is bound to a Property and the value will change whenever the underlying value is changed. Works the same for a Label.

Scott Norberg
  • 389
  • 1
  • 4
  • 11