2011-02-16 2 views

답변

1

답변 :

매우 간단한 재귀 호출을 사용 중입니다. # 코드들 C 성가신 매우 복잡한 3 개 페이지에 대한 필요가 없습니다, 여기에 내가 쓴 코드이며, 그것을 작동 :

모든 폼의 컨트롤을 반복하는 각 루프를 만들고 루프 내에서,이 전화 :

Private Shared Sub recurseTranslateControls(ByVal lang As String, ByVal c As Control) 

    Dim newtxt as string = getLangItem(c.name, lang)  ' This function performs string translation 
                 ' Nothing to do with the current post/answer 
    ' This will work for "normal" controls 
    If newtxt <> "" Then 
     c.Text = newtxt    ' Apply the translated text to the control 
    End If 

    If c.HasChildren Then 
     For Each co In c.Controls 
       ' This will work for Toolstrip. You should do same for Menustrip etc. 
       If "toolstrip".Contains(co.GetType.Name.ToLower) Then 
       Dim ts As ToolStrip = co    ' Toolstrip doesn't have child controls, but it DOES have ITEMS! 
       For Each itm As ToolStripItem In ts.Items 
        ' No need for recursivity: toolstrip items doesn't have children 
        Call TranslateToolstrip(lang, itm)   ' Apply the translated text to the toolstrip item 
       Next 
      Else 
       Call recurseTranslateControls(lang, co) 
      End If 
     Next 
    End If 

End Sub 

Private Shared Sub TranslateToolstrip(ByVal lang As String, ByVal t As ToolStripItem) 

    Dim newtxt = getLangItem(t.name, lang) 

    If newtxt <> "" Then 
     t.Text = newtxt 
    End If 

End Sub 

중요 발언 : 나는 VB 및 NOT C# .NET을 선택이 끝난 한있는 이유 중 하나는 C#을 난독, 복잡한 하드에 다시 읽을 코드를 빌려, 그 위에, C#을은 "소위"이다 전문가 (진짜 사람이 아니라)는 아무도 이해할 수없는 코드를 작성하는 데 너무나 행복합니다.

문제에 대한 복잡한 C# 해결책을 찾을 때마다 나는 그것을 받아들이지 않으며 항상 작업을 수행하는 더 간단한 방법을 찾습니다. 예, 언제나, 항상 ...

0

질문에 잘못이 있습니다. Toolstrip의 항목은 ToolStripItem에서 상속되며이 ToolStripItem은 차례대로 구성 요소에서 파생됩니다. 이 컨트롤은 컨트롤이 아니므로 ToolStrip.hasChildren이 항상 false를 반환하는 이유는 컨트롤을 일반적으로 컨트롤로 처리 할 수없는 이유입니다. 나는 동일한 작업을 가지고있다. ToolStripItem, MenuItems 등은 재귀 적 메서드로 구분되어야한다는 것이 분명하다. 편리한 것은 아니지만 다른 방법은 없습니다.

1

나는 최근에 비슷한 것을해야했고,이 질문을 발견했습니다. 다음 코드 조각은 항목 이름에 sType 변수가 들어 있는지 여부에 따라 도구 상자의 항목을 사용하거나 사용하지 않도록 설정합니다. 이름이 "고객"를 포함하지 않는 ToolStrip의 항목을 비활성화 할 것 -

Friend Sub ModifyEnabledControls(ByVal ts As ToolStrip, ByVal sType As String) 
    For Each c As ToolStripItem In ts.Items 
     If c.Name.Contains(sType) Then 
      c.Enabled = True 
     Else 
      c.Enabled = False 
     End If 
    Next 
End Sub 

기능은 ModifyEnabledControls (ToolStrip1, "고객")를 사용하여 호출한다.

관련 문제