2013-04-24 49 views
0

我有一個richTextBox,用戶可以加載一個文本文件(rtf或txt)。我有用於對齊richTextBox中的文本的按鈕。問題出在用戶加載新文檔時,如果文本與中心對齊,左對齊(或最後一次點擊哪個對齊按鈕)仍處於選中狀態。我如何使正確的方框自動突出顯示(所以如果第一行文本與中心對齊,則選擇中央按鈕,並且如果第二行文本向右居中然後單擊,則右側按鈕是強調和其他人都取消如何在打開文件時選擇正確的對齊按鈕?

當前代碼:

左對齊

private void Left() 
    { 
     richTextBoxPrintCtrl1.GetLineFromCharIndex(1); 
     richTextBoxPrintCtrl1.SelectionAlignment = HorizontalAlignment.Left; 
     if (left.Checked == true) 
     { 
      right.Checked = false; 
      center.Checked = false; 
     } 

中心對齊

private void Center() 
    { 
     richTextBoxPrintCtrl1.GetLineFromCharIndex(1); 
     richTextBoxPrintCtrl1.SelectionAlignment = HorizontalAlignment.Left; 
     if (left.Checked == true) 
     { 
      right.Checked = false; 
      center.Checked = false; 
     } 

右對齊

private void Right() 
    { 
     richTextBoxPrintCtrl1.GetLineFromCharIndex(1); 
     richTextBoxPrintCtrl1.SelectionAlignment = HorizontalAlignment.Right; 
     if (left.Checked == true) 
     { 
      right.Checked = false; 
      center.Checked = false; 
     } 

這需要不斷更新,以使得當用戶點擊一條線,它得到的取向狀態,並檢查相應的按鈕。

回答

0

我已經創建了一種記事本,可以執行粗體,斜體格式化等選項。當用戶點擊一個單詞時,這個格式化我的按鈕會突出顯示。也許你可以在你的項目中使用同樣的東西。

首先我在richtextbox(selectionchanged)上創建一個事件。

DET後,我已經插入此代碼:

if (Document.SelectionFont != null) 
     { 

      tb_Bold.Checked = Document.SelectionFont.Bold; 
      tb_Italic.Checked = Document.SelectionFont.Italic; 
      tb_UnderLine.Checked = Document.SelectionFont.Underline; 
      tb_FontSize.Text = Document.SelectionFont.SizeInPoints.ToString(); 
      tb_Font.Text = Document.SelectionFont.FontFamily.Name; 
      if (Document.SelectionAlignment.ToString() == "Right") 
      { 
       Center.Checked = false; 
       Right.Checked = true; 
       Left.Checked = false; 
      } 
      else if (Document.SelectionAlignment.ToString() == "Center") 
      { 
       Center.Checked = true; 
       Right.Checked = false; 
       Left.Checked = false; 
      } 
      else 
      { 
       Center.Checked = false; 
       Right.Checked = false; 
       Left.Checked = true; 

      } 

     } 

而且每個按鈕下面我有這樣的代碼:

Document.SelectionAlignment = HorizontalAlignment.Center; 
     Center.Checked = true; 
     Right.Checked = false; 
     Left.Checked = false; 

當然改變alignement,併爲的checkStatus按鈕。