1

我有两种形式,一种用于文本编辑器,一种用于搜索表单。

在我的 Form1 中,我定义了GetRichTextBox()函数。它在那里工作,我怎样才能将它检索到 Form2 (和其他东西)?

我的表格1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TextEditor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private RichTextBox GetRichTextBox()
        {
            RichTextBox rtb = null;
            TabPage tp = tabControl1.SelectedTab;

            if (tp != null)
            {
                rtb = tp.Controls[0] as RichTextBox;
            }

            return rtb;
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TabPage tp = new TabPage("doc");
            RichTextBox rtb = new RichTextBox();
            rtb.Dock = DockStyle.Fill;

            tp.Controls.Add(rtb);
            tabControl1.TabPages.Add(tp);
        }

        private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GetRichTextBox().Cut();
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GetRichTextBox().Copy();
        }

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GetRichTextBox().Paste();
        }

        private void undoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GetRichTextBox().Undo();
        }

        private void redoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GetRichTextBox().Redo();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFD = new OpenFileDialog();
            string Chosen_File = "";

            openFD.InitialDirectory = "C:";
            openFD.Title = "Open a Text File";
            openFD.FileName = "";

            openFD.Filter = "Text Files|*.txt|Word Documents|*.doc";

            if (openFD.ShowDialog() != DialogResult.Cancel)
            {

                Chosen_File = openFD.FileName;
                TabPage tab = new TabPage() { Text = System.IO.Path.GetFileName(Chosen_File) };
                tabControl1.TabPages.Add(tab);
                tabControl1.SelectedTab = tab;
                RichTextBox box = new RichTextBox { Parent = tab, Dock = DockStyle.Fill };
                box.LoadFile(Chosen_File, RichTextBoxStreamType.PlainText);

            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFD = new SaveFileDialog();

            string saved_file = "";

            saveFD.InitialDirectory = "C:";
            saveFD.Title = "Save a Text File";
            saveFD.FileName = "";

            saveFD.Filter = "Text Files|*.txt|Word Documents|*.doc";

            if (saveFD.ShowDialog() != DialogResult.Cancel)
            {

                saved_file = saveFD.FileName;
                GetRichTextBox().SaveFile(saved_file, RichTextBoxStreamType.PlainText);
                MessageBox.Show("Your file has been successfully saved!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {

                Application.Exit();

            }
        }

        private void closeTabToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TabPage active_tab = tabControl1.SelectedTab;
            tabControl1.TabPages.Remove(active_tab);
        }

        private void searchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var search = new Form2();
            search.ShowDialog();
        }
    }
}

当然,我的 Form2:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TextEditor
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int index = 0; string temp = richTextBox1.Text; richTextBox1.Text = ""; richTextBox1.Text = temp;

            while (index < richTextBox1.Text.LastIndexOf(textBox1.Text))
            {

                richTextBox1.Find(textBox1.Text, index, richTextBox1.TextLength, RichTextBoxFinds.None);

                richTextBox1.SelectionBackColor = Color.Orange;

                index = richTextBox1.Text.IndexOf(textBox1.Text, index) + 1;
            }
        }
    }
}
4

0 回答 0