2011-12-23 4 views
1

RichTextBox에서 글꼴 스타일을 조정하는 데 문제가 있습니다. 단일 속성 (예 : 굵은 글꼴 켜고 끄기)에 대해 이야기하는 몇 가지 다른 접근 방식을 보았습니다 ... 하지만 내 글꼴 클래스가 속성 (굵게, 기울임 꼴, 밑줄)을 조정할 수 있도록하려고합니다.vb.net에서 RichTextBox의 글꼴 스타일 조정

Font.Style은 부울 플래그 집합 (bitfield?)입니다 ...하지만 모든 속성을 한꺼번에 처리하는 방법을 모르겠습니다. 텍스트를 굵게 간다 -

  • 내가 (사실로) 대담한 전환 : 여기

    Public Sub ModifyFontStyle(Optional ByVal Plain As Object = Nothing, Optional ByVal Bold As Object = Nothing, _ 
              Optional ByVal Italics As Object = Nothing, Optional ByVal Underlined As Object = Nothing) 
        Dim newFontStyle As System.Drawing.FontStyle 
    
    
        If Plain Then 
         newFontStyle = Drawing.FontStyle.Regular 
         GivenFont = New Drawing.Font(GivenFont.FontFamily, GivenFont.Size, newFontStyle) 
         Exit Sub 
        End If 
    
        If Bold IsNot Nothing Then 
         If Bold Then 
          newFontStyle = GivenFont.Style + Drawing.FontStyle.Bold 
         Else 
          newFontStyle = GivenFont.Style - Drawing.FontStyle.Bold 
         End If 
        End If 
    
    
        If Italics IsNot Nothing Then 
         If Italics Then 
          newFontStyle = GivenFont.Style + Drawing.FontStyle.Italic 
         Else 
          newFontStyle = GivenFont.Style - Drawing.FontStyle.Italic 
         End If 
        End If 
    
        If Underlined IsNot Nothing Then 
         If Underlined Then 
          newFontStyle = GivenFont.Style + Drawing.FontStyle.Underline 
         Else 
          newFontStyle = GivenFont.Style - Drawing.FontStyle.Underline 
         End If 
        End If 
    
        GivenFont = New Drawing.Font(GivenFont.FontFamily, GivenFont.Size, newFontStyle) 
    
    End Sub 
    

    그리고이 코드 최종 문제는 다음과 같습니다

    다음은 귀찮은 코드입니다.

  • 밑줄이 표시됨 - 이제 텍스트에 굵게 표시되고 밑줄이 그어집니다.
  • 기울임 꼴을 전환합니다. 텍스트는 이제 굵게 표시되고 밑줄이 그어지고 기울임 꼴로 표시됩니다.
  • 다시 굵게 전환합니다 (false로 설정). 텍스트가 취소 선으로 바뀝니다.

글꼴에 어떤 현상이 있습니까? 텍스트에 밑줄이 그어지고 이탤릭체로 취소 선이 없어야합니다. ...

내 부분에는 논리 오류 또는 간단한 오해가 있습니까?

글쎄, 나는 그것이 작동 할 때까지 그것으로 주변에 땜질하겠습니다, 시간 내 주셔서 감사합니다 아니면 작동하는 대답, 당신은 잘못된 연산자를 사용하는

+0

나는 내가해야 할 일을 알아 냈다 : 내가 조정하고있는 특정 속성을 확인하고 글꼴 스타일을 두 번 이상 추가하지 않도록한다. 나는이 질문에 대한 답변을 잠깐 게시 할 것이다. – Dominick

+0

기호가 제거되었습니다. FAQ를 참조하십시오. –

답변

1

를 얻을. 실제로 비트 플래그와 비슷하지만 열거 형은 [Flags] 특성을가집니다. Or 연산자를 사용하여 스타일을 켜고 And 연산자를 사용하여 스타일을 해제해야합니다. 이와 같이 :

Dim style = Me.Font.Style 
    '--- turn bold on 
    style = style Or FontStyle.Bold 
    '--- turn bold off 
    style = style And Not FontStyle.Bold 
-1

글쎄, 나는 그것을 얻었다. 나는 그것이 성공적으로 작동하도록했습니다.

Public Sub ModifyFontStyle(Optional ByVal Plain As Object = Nothing, Optional ByVal Bold As Object = Nothing, _ 
          Optional ByVal Italics As Object = Nothing, Optional ByVal Underlined As Object = Nothing) 
    Dim newFontStyle As System.Drawing.FontStyle 


    If Plain Then 
     newFontStyle = Drawing.FontStyle.Regular 
     GivenFont = New Drawing.Font(GivenFont.FontFamily, GivenFont.Size, newFontStyle) 
     Exit Sub 
    End If 

    If Bold IsNot Nothing Then 
     If Bold And Not GivenFont.Bold Then 
      newFontStyle = GivenFont.Style + Drawing.FontStyle.Bold 
     Else 
      newFontStyle = GivenFont.Style - Drawing.FontStyle.Bold 
     End If 
    End If 


    If Italics IsNot Nothing Then 
     If Italics And Not GivenFont.Italic Then 
      newFontStyle = GivenFont.Style + Drawing.FontStyle.Italic 
     Else 
      newFontStyle = GivenFont.Style - Drawing.FontStyle.Italic 
     End If 
    End If 

    If Underlined IsNot Nothing Then 
     If Underlined And Not GivenFont.Underline Then 
      newFontStyle = GivenFont.Style + Drawing.FontStyle.Underline 
     Else 
      newFontStyle = GivenFont.Style - Drawing.FontStyle.Underline 
     End If 
    End If 

    GivenFont = New Drawing.Font(GivenFont.FontFamily, GivenFont.Size, newFontStyle) 

End Sub 

감사합니다.