【问题标题】:How do I make the correct alignment button selected when file is opened?打开文件时如何选择正确的对齐按钮?
【发布时间】:2013-04-18 06:07:42
【问题描述】:

我有一个 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;
        }

这需要不断更新,以便当用户单击一行时,它会获取对齐状态并检查相应的按钮。

【问题讨论】:

    标签: c# winforms visual-studio richtextbox text-align


    【解决方案1】:

    我创建了一种记事本,可以选择执行粗体、斜体格式等。当用户单击具有任何这种格式的单词时,我的按钮会突出显示。也许你可以在你的项目中使用它。

    首先我在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;
    

    当然要更改按钮的对齐方式和检查状态。

    【讨论】:

      猜你喜欢
      • 2022-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-08
      • 1970-01-01
      • 2013-10-26
      • 2012-01-19
      相关资源
      最近更新 更多