2015-01-05 3 views
-1

텍스트가 변경되는 버튼이 있습니다. 한 번에 원래 텍스트로 다시 바꿀 때 그룹이 필요합니다.버튼 텍스트를 원래 텍스트로 복원

나는 이것을 수동으로 할 수 있으며 각각을 button.text=originalText"으로 설정할 수는 있지만 어쨌든 이것을 빨리 할 수 ​​있습니까?

원본 텍스트를 속성에서 가져 와서 텍스트를 바꾸는 루프가 있습니까? (모두 동일한 태그)

+0

WinForms 또는 WPF? – Steve

+0

@ Steve Winforms – Dman

+1

원래 속성이 어디에도 저장되어 있지 않다고 생각하기 때문에 다시 할당하면 기본적으로 손실됩니다. 예를 들어,'Tag' 속성과 같이'Button'의 다른 곳에 문자열을 저장해야합니다. 예를 들어 나중에 버튼을 순환하여'button.Text = button.Tag'을 할 수 있습니다. – Andrew

답변

3

InitializeComponent을 호출 한 직후에 Button 사전과 해당 Text 속성을 만들 수 있습니다.

는이 세 개의 버튼이 원래 텍스트를 복원해야 할 때, 물론

for each pair as KeyValuePair(Of Button, string) in _originalTexts 
    pair.Key.Text = pair.Value 
Next 

쓰기이 방법에서 사용할 수있는 사전을 가지고 수 Button1, Button2, Button3

Dim _originalTexts As Dictionary(Of Button, string) 
Public Sub New() 
    InitializeComponents() 
    _originalTexts = new Dictionary(Of Button, string)() From _ 
    { _ 
     {Button1, Button1.Text}, _ 
     {Button2, Button2.Text}, _ 
     {Button3, Button3.Text} _ 
    } 
End Sub 

이름에 또한 허용했다고 가정하자 특정 버튼을 검색하십시오. 여기
당신이 태그 특성 값으로 설정하는 버튼을 검색하는 예

Dim b = tt.Where(Function (x) x.Key.Tag.ToString = "b1").SingleOrDefault() 
if b.Key IsNot Nothing Then 
    Console.WriteLine(b.Value) 
End If 

참고 : 폼 클래스의 InitializeComponent 전화가 표시되지 않는 경우 그냥 생성자를 입력

공개 Sub New()

그리고 IDE가 누락 된 코드를 표시합니다.

0

단지에서 복원,

Private DefaultButtonTexts as Dictionary(Of string, string) 

InitializeComponent() 후 각 버튼의 기본 텍스트를 저장하기 위해 인스턴스 변수를 선언 사전

'Sub Main 
    'dim form = new Form() 
    'form.Controls.Add(new TextBox() With { .Name = "txt1" }) 
    'form.Controls.Add(new Button() With { .Name = "btn1", .Text = "Button 1" }) 
    'form.Controls.Add(new Button() With { .Name = "btn2", .Text = "Button 2" }) 

    'DefaultButtonTexts = form.Controls.Cast(Of Control) 
    DefaultButtonTexts = this.Controls.Cast(Of Control) _ 
     .OfType(Of Button) _ 
     .ToDictionary(Function(x) x.Name, Function(x) x.Text) 

    'DefaultButtonTexts.Dump() 

    'form.ShowDialog() 
'End Sub 

에 이름과 각 버튼의 텍스트를 저장하고 필요시 사전

Private Sub RestoreText(button as Button) 
    button.Text = DefaultButtonTexts(button.name) 
End Sub 

또는 Tag 속성

this.Controls.Cast(Of Control) _ 
    .OfType(Of Button) _ 
    .Where(Function(x) x.Tag = "tag1") _ 
    .ToList() _ 
    .ForEach(Function(x) x.Text = DefaultButtonTexts(x.name)) 
관련 문제