【问题标题】:C# RichTextBox change selected to bold or none selected to boldC# RichTextBox 将选中的更改为粗体或未选中的更改为粗体
【发布时间】:2017-07-27 06:29:41
【问题描述】:

嗨,我正在做一个项目,其中一部分是。如果我在 RichTextBox 中输入 somthing 并按下我的粗体按钮,每一次都应该是粗体,但如果我选择了文本,那么只有那个应该是粗体。如果 RichTextBox 中没有任何内容,则应显示一条消息。我还是个学生,所以我的编码不是那么有经验。 这是我到目前为止或正在尝试的:

I took a snippet of my code

private void button2_Click(object sender, EventArgs e)
{
    if (rtbText.TextLength == 0)
    {
        MessageBox.Show("Textbox is empty");
    }
    else if 
    {
        rtbText.Font = new System.Drawing.Font (rtbText.Font.Name,
        rtbText.Font.Size, rtbText.Font.Style ^ System.Drawing.FontStyle.Bold);
    }
    else
    {
        rtbText.SelectionFont = new Font(rtbText.Font, FontStyle.Bold);
        rtbText.SelectionStart = rtbText.SelectionStart + rtbText.SelectionLength;
        rtbText.SelectionLength = 0;
        rtbText.SelectionFont = rtbText.Font;
    }
}

【问题讨论】:

  • 将您的代码作为文本添加到问题
  • else if 和 else 没有完成,但我只需要一个指南

标签: c# fonts richtextbox


【解决方案1】:

假设您正在寻找正确的if 条件,请尝试以下代码:

if (rtbText.TextLength == 0)
{
}
else if (rtbText.SelectionLength == 0)
{
}
else
{
}

【讨论】:

  • @Dreyer 我回答你的问题了吗?如果是这样接受它。如果不是,请编辑答案并阐明要求
  • 对不起,我还是新手。你确实回答了我的问题,再次感谢你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-27
  • 1970-01-01
  • 1970-01-01
  • 2019-04-08
  • 2017-09-05
  • 2019-12-27
  • 1970-01-01
相关资源
最近更新 更多