所以我在visual studio中编写脚本并设计一个小时间表,并添加了一些富文本编辑器,如粗体和斜体。但我只希望这些复选框影响突出显示的文本,而不是整个组框。
我尝试在.Font = new Font之前添加“Selected”或“Highlighted”,但没有结果。我是这个领域的初学者。
using System;
using System.Drawing;
using System.Windows.Forms;
namespace timetable
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
groupBox1.Font = new Font(this.Font, FontStyle.Bold);
}
else
{
groupBox1.Font = new Font(this.Font, FontStyle.Regular);
}
if (checkBox2.Checked)
{
groupBox1.Font = new Font(this.Font, FontStyle.Bold | FontStyle.Italic);
}
}
private void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
if (checkBox2.Checked)
{
groupBox1.Font = new Font(this.Font, FontStyle.Italic);
}
else
{
groupBox1.Font = new Font(this.Font, FontStyle.Regular);
}
if (checkBox1.Checked)
{
groupBox1.Font = new Font(this.Font, FontStyle.Bold | FontStyle.Italic);
}
}
private void GroupBox1_Enter(object sender, EventArgs e)
{
}
}
}
我只想通过复选框来编辑由光标突出显示的文本.This is why I am using a group box.
我知道这可能很烦人,但是你能告诉我如何在我的代码中实现foreach
语句吗?喜欢我的整个代码?
添加这样的foreach之后:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace timetable
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
foreach (Control ctrl in groupBox1.Controls)
{
if (ctrl is RichTextBox)
{
RichTextBox box = ctrl as RichTextBox;
box.SelectionFont = new Font(box.SelectionFont, box.SelectionFont.Style | FontStyle.Bold);
}
}
}
else
{
foreach (Control ctrl in groupBox1.Controls)
{
if (ctrl is RichTextBox)
{
RichTextBox box = ctrl as RichTextBox;
box.SelectionFont = new Font(box.SelectionFont, box.SelectionFont.Style | FontStyle.Regular);
}
}
}
if (checkBox2.Checked)
{
foreach (Control ctrl in groupBox1.Controls)
{
if (ctrl is RichTextBox)
{
RichTextBox box = ctrl as RichTextBox;
box.SelectionFont = new Font(box.SelectionFont, box.SelectionFont.Style | FontStyle.Italic);
}
}
}
}
}
}
我有这个:System.NotImplementedException: 'The method or operation is not implemented.'
这在设计师代码中突出显示:
private void GroupBox1_Enter(object sender, EventArgs e)
{
throw new NotImplementedException();
}
你在这些组框中有什么包含字体?您需要一个RichTextBox然后您可以使用以下更改所选字体...
private void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, richTextBox1.SelectionFont.Style | FontStyle.Bold);
}
else
{
richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, richTextBox1.SelectionFont.Style | FontStyle.Regular);
}
if (checkBox2.Checked)
{
richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont, richTextBox1.SelectionFont.Style | FontStyle.Italic);
}
}
添加:
根据评论你需要一个像这样的foreach循环给小组...
foreach(Control ctrl in groupBox1.Controls)
{
if(ctrl is RichTextBox)
{
RichTextBox box = ctrl as RichTextBox;
box.SelectionFont = new Font(box.SelectionFont, box.SelectionFont.Style | FontStyle.Bold);
}
}
离开你......
private void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
foreach(Control ctrl in groupBox1.Controls)
{
if (ctrl is RichTextBox)
{
RichTextBox box = ctrl as RichTextBox;
box.SelectionFont = new Font(box.SelectionFont, box.SelectionFont.Style | FontStyle.Bold);
}
}
}
else
{
foreach(Control ctrl in groupBox1.Controls)
{
if (ctrl is RichTextBox)
{
RichTextBox box = ctrl as RichTextBox;
box.SelectionFont = new Font(box.SelectionFont, box.SelectionFont.Style | FontStyle.Regular);
}
}
}
if (checkBox2.Checked)
{
foreach(Control ctrl in groupBox1.Controls)
{
if (ctrl is RichTextBox)
{
RichTextBox box = ctrl as RichTextBox;
box.SelectionFont = new Font(box.SelectionFont, box.SelectionFont.Style | FontStyle.Italic);
}
}
}