C# C从表格2中选择表格1中的文本

C# C从表格2中选择表格1中的文本,c#,C#,好的,我有我的主表单Form1和SearchReplace表单。我的SearchReplace表单包含一个文本框和一个按钮。当按下按钮时,它应该选择Form1中文本框中的内容,但什么也不做。有人能帮我吗?没有给我一个错误,只是没有在运行时做任何事情 搜索替换 Form1搜索文本 searchT是在Form1中创建的一个公共字符串,因为当我明确询问如何将数据从一个表单传递到另一个表单时,有人告诉我,直接通过Form1而不是使用Form1对象更容易做到这一点。问题是,您在单击按钮时创建了一个全新的表

好的,我有我的主表单Form1和SearchReplace表单。我的SearchReplace表单包含一个文本框和一个按钮。当按下按钮时,它应该选择Form1中文本框中的内容,但什么也不做。有人能帮我吗?没有给我一个错误,只是没有在运行时做任何事情

搜索替换

Form1搜索文本


searchT是在Form1中创建的一个公共字符串,因为当我明确询问如何将数据从一个表单传递到另一个表单时,有人告诉我,直接通过Form1而不是使用Form1对象更容易做到这一点。

问题是,您在单击按钮时创建了一个全新的表单。您应该能够通过以下方式实现您想要的:

public void button1_Click(object sender, EventArgs e)
{
    Form1.searchT = textBox1.Text;
    searchText(); //Calls this instance's searchText() function
    this.Close();
}

您可能希望将searchT设置为非静态,否则其值将应用于Form1的所有实例。

根据您最近的评论,最好在SearchReplace之前隐藏当前表单 呼叫Form1。然后在Form1关闭后将其关闭。请检查下面的我的代码:

假设这是您的SearchReplace表单:

你的表格1是这样的:

public partial class Form1 : Form
{
    public static string searchT; // Static searchT string as per your requirement

    public Form1()
    {
        InitializeComponent();
    }


   public void searchText() // search function
   {

    if (searchT != null)
    {
        if (textBox1.TextLength > 0)
        {
            if (textBox1.Text.Contains(searchT))
            {
                textBox1.SelectionStart = textBox1.Text.IndexOf(searchT);
                textBox1.SelectionLength = searchT.Length;
            }
        }
    }
  }    
}

您可以将对form1中文本框的引用传递给FormSearchReplace的构造函数。
然后使用此引用选择给定的文本。

根据创建SearchReplace表单的方式,您可以使用允许您设置所属表单的or方法将Form1指定为所有者,然后不依赖于在Form1中设置变量,只需将参数传递给您的函数即可。像这样的东西应该适合你

表格1

搜索替换表单


你的代码没有问题,除了你只声明要选择哪个文本,不要期望它自动高亮显示,我建议你改变背景色或所选文本以高亮显示它。 试试我的例子

我建议您使用RichTextBox

在您的表格1中,假设您声明了

public static string searchT;
那么在你的表格里 将searchReplace表单显示为对话框

然后以你的搜索替换形式

最后,在searchtext方法中

注意:您不能在文本框中选择多个文本,这就是为什么更改与您的搜索匹配的每个文本的背景色将是最好的选择


希望这对您有所帮助:

您可以通过使用属性并给出特定文本框的值来完成此操作

在您的表格1中:

public static string Text
{
   get { return textbox1.Text; }
}
你可以在你的另一张表格上

form1.Text;

我必须为它创建一个新对象,因为只需键入searchText;给我一个错误,说它不存在。我必须创建对象来调用它。抛出一个错误,说无法使用实例引用访问;改为使用类型名称限定它。我如何将其限定为字符串?@请使用此答案中的代码,它不会产生该错误。您的是,因为您将searchT声明为静态。此代码的问题是,打开SearchReplace时,用户已经在文本框1中有数据,因此,如果我关闭它并打开另一个文本框,它将删除data@Mercifies:检查我的更新。我在这个上面使用了静态字符串搜索。我已经测试过了,效果很好。我在Form1上的textbox1上添加了值,尽管是在设计时,以与SearchReplace表单中textbox1上的输入相匹配。我更改了代码,以测试searchText是否有效,以及在我打开新表单时是否有效。重新键入信息并搜索它。问题是当我试图改变Form1 Form1=新Form1时;形成1形成1;它让我意识到它永远不会被赋值,并且总是有空值。form1.searchT=textBox1.Text;无法使用实例引用访问抛出错误;改为使用类型名称限定它。我在Form1中把它作为一个公共静态字符串,不知道为什么它会给我这个问题。你是如何创建SearchReplace表单的,是从Form1中创建的吗?通过运行Form1 Form1=new Form1;您正在创建窗体的新不可见实例。这与正在显示的主窗体的实例不同。
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    public void searchText(string searchT) // search function
    {

        if (searchT != null)
        {
            if (textBox1.TextLength > 0)
            {
                if (textBox1.Text.Contains(searchT))
                {
                    textBox1.Focus(); // put focus to the Textbox so we can see our selection
                    textBox1.SelectionStart = textBox1.Text.IndexOf(searchT);
                    textBox1.SelectionLength = searchT.Length;

                }
            }
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        SearchReplace sr = new SearchReplace(); // Creating your SearchReplace Form
        sr.Show(this);  // Pass the creating form in as the Owner
    }
}
public partial class SearchReplace : Form
{
    public SearchReplace()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        ((Form1)this.Owner).searchText(textBox1.Text); //Cast the Owner to Form1 and access the Function 
                                                       //passing in your search Parameter
        this.Close();
    }
}
public static string searchT;
        private void searchReplaceBtn_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2();
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                searchText();
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Form1.searchT = textBox1.Text;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
        private void searchText()
        {

            if (searchT != null)
            {
                if (textBox1.TextLength > 0)
                {
                    int index = 0;
                    //this will loop through the entire richTextBox
                    while (index < textBox1.Text.LastIndexOf(searchT))
                    {
                        //this will select the text that matches the searchT
                        textBox1.Find(searchT, index, textBox1.TextLength, RichTextBoxFinds.None);
                        //this will change the background color of each of the selected text
                        textBox1.SelectionBackColor = Color.Yellow;
                        //this will continue searching to the end of the text
                        index = textBox1.Text.IndexOf(searchT, index) + 1;
                    }
                }
            }
        }
public static string Text
{
   get { return textbox1.Text; }
}
form1.Text;