2013-04-24 45 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按钮。