我正在用C#开发文本编辑器,我有两个问题,
1)我想在richtextbox中选择文本时打开一个对话框(一个表格可以说是一个表格2)。 2)我想访问richtextbox中的所选文本(在main form1中)以对其执行格式化。
我无法执行这些任务,请尽快给予帮助。 代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Text_Editor {
public partial class Form1: Form {
public Form1() {
InitializeComponent();
}
private void newToolStripMenuItem_Click(object sender, EventArgs e) {
Form1 newForm = new Form1();
newForm.Show();
}
private void openToolStripMenuItem_Click(object sender, EventArgs e) {
int Count = 1;
string Input_Name = "";
FileOpen.Title = "Open a Text File"; // Change the title
FileOpen.FileName = ""; // Blank space for the file name
FileOpen.Filter = "Text Files|*.txt"; // Only files having txt extension
FileOpen.Multiselect = true;
// Check about Cancel
if (FileOpen.ShowDialog() != DialogResult.Cancel) {
Count = FileOpen.FileName.Count();
if (Count > 1) {
foreach(string item in FileOpen.FileNames) {
Input_Name = item;
Form1 newForm = new Form1();
newForm.richTextBox1.LoadFile(Input_Name, RichTextBoxStreamType.PlainText);
newForm.Text = item;
newForm.Show();
}
} else {
Input_Name = FileOpen.FileName;
richTextBox1.LoadFile(Input_Name, RichTextBoxStreamType.PlainText);
}
} else {
MessageBox.Show("No File Selected", "Message")
}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e) {
string Output_Name = "";
FileOpen.Title = "Save a Text File"; // Change the title
FileOpen.FileName = ""; // Blank space for the file name
FileOpen.Filter = "Text Files|*.txt"; // Only files having txt extension
if (FileOpen.ShowDialog() != DialogResult.Cancel) // Check Dialog Box
{
Output_Name = FileOpen.FileName;
richTextBox1.SaveFile(Output_Name, RichTextBoxStreamType.PlainText);
}
}
private void cutToolStripMenuItem_Click(object sender, EventArgs e) {
if (richTextBox1.SelectedText != "") {
richTextBox1.Cut();
} else {
MessageBox.Show("No text selected", "Message");
}
}
private void copyToolStripMenuItem_Click(object sender, EventArgs e) {
if (richTextBox1.SelectedText != "") {
richTextBox1.Copy();
} else {
MessageBox.Show("No text selected", "Message");
}
}
private void pasteToolStripMenuItem_Click(object sender, EventArgs e) {
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text)) {
richTextBox1.Paste();
Clipboard.Clear();
} else {
MessageBox.Show("No text to paste", "Message");
}
}
private void richTextBox1_SelectionChanged(object sender, EventArgs e) {
Style newStyle = new Style();
newStyle.Show();
}
}
}
form2 Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Text_Editor {
public partial class Style: Form {
public Style() {
InitializeComponent();
}
private Form1 newDoc;
private void fontDialog1_Apply(object sender, EventArgs e) {
fontDialog1.ShowDialog();
}
private void button1_Click(object sender, EventArgs e) {
Font new1, old;
old = newDoc.richtextBox.selectdText;
if (old.Bold) {
new1 = new Font(old, old.Style & ~FontStyle.Bold);
} else {
new1 = new Font(old, old.Style | FontStyle.Bold);
}
newDoc.Text.SelectionFont = new1;
}
}
}