【问题标题】:C# Selecting text in form1 from form2C#从form2中选择form1中的文本
【发布时间】:2013-10-22 01:44:29
【问题描述】:

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

搜索替换

    public void button1_Click(object sender, EventArgs e)
    {
        Form1.searchT = textBox1.Text;


        Form1 form1 = new Form1();
        form1.searchText();
        this.Close();
    }

Form1 搜索文本

    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;
                }
            }
        }
    }

searchT 是在Form1 中创建的公共字符串,因为当我以前询问有关将数据从一种形式传递到另一种形式时,有人告诉我直接通过Form1 而不是使用form1 对象更容易做到这一点。

【问题讨论】:

  • 您是如何创建 SearchReplace 表单的,它来自 Form1 吗?
  • 通过运行Form1 form1 = new Form1();,您正在创建一个新的(不可见的)表单实例。这与您正在显示的 mian 表单不同。

标签: c#


【解决方案1】:

根据您最近的评论,最好在之前隐藏您当前的表单 (SearchReplace) 调用 Form1。然后在 Form1 也关闭后关闭它。检查下面的代码:

假设这是您的SearchReplace 表单:

public partial class SearchReplace : Form
{
    Form1 form1;

    public SearchReplace()
    {
        InitializeComponent();
    }

    void form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        this.Close(); // When form1 is closed then close also SearchReplace form
    }

    private void button1_Click(object sender, EventArgs e)
    {

        Form1.searchT = textBox1.Text; // assign textbox1 to searchT
        form1 = new Form1(); // instantiate first
        form1.FormClosed += new FormClosedEventHandler(form1_FormClosed); // When Form1 Close
        form1.searchText(); // Run searchText function
        form1.Show(); // Show Form1
        this.Hide(); // Make SearchReplace form invisible but still in memory
    }
}

你的Form1 会是这样的:

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;
            }
        }
    }
  }    
}

【讨论】:

  • 给我一个错误,说不能用实例引用访问;而是用类型名称来限定它。我如何将其限定为字符串?
  • @Mercifies 使用此答案中的代码,它不会产生该错误。你这样做是因为你将searchT 声明为静态的。
  • 这段代码的问题是,要打开SearchReplace,用户已经在form 的文本框1 中有数据,所以如果我关闭它并打开另一个它会删除数据跨度>
  • @Mercifies:检查我的更新。我在这个上使用了一个静态字符串 searchT。我已经对其进行了测试,并且可以正常工作。我在设计时将值放在 Form1 上的 textbox1 上,以与 SearchReplace 表单中 Textbox1 上的输入相匹配。
  • 我更改了代码以测试searchText() 是否有效,并且当我打开一个新表单并重新输入信息并搜索它时它是否有效。问题是当我尝试将Form1 form1 = new Form1(); 更改为Form1 form1; 时,它会抛出我从未分配过它并且将始终具有空值。 form1.searchT = textBox1.Text; 抛出错误不能用实例引用访问;而是用类型名称来限定它。我在Form1 中有它作为公共静态字符串,不知道为什么它给我这个问题
【解决方案2】:

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

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

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

【讨论】:

  • 我必须为它创建一个新对象,因为只输入searchText(); 就会抛出一个错误,说它不存在。我必须创建对象来调用它。
【解决方案3】:

您可以将 form1 中的文本框的引用传递给表单 searchreplace 的构造函数。 然后使用这个引用来选择给定的文本。

【讨论】:

    【解决方案4】:

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

    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();
        }
    }
    

    【讨论】:

      【解决方案5】:

      你的代码没有问题,只是你只声明了会选择哪些文本,不要指望它会自动高亮,我建议你改变背景颜色或选择的文本来高亮。 试试我的例子。

      建议你使用RichTextBox

      在您的 Form1 中假设您声明了一个

      public static string searchT;
      

      然后在你的Form1 将 searchReplace 表单显示为 Dialog

              private void searchReplaceBtn_Click(object sender, EventArgs e)
              {
                  Form2 frm = new Form2();
                  if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                  {
                      searchText();
                  }
              }
      

      然后在你的SearchReplace Form

              private void button1_Click(object sender, EventArgs e)
              {
                  Form1.searchT = textBox1.Text;
                  this.DialogResult = System.Windows.Forms.DialogResult.OK;
              }
      

      终于在你的searchtext Method

              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;
                          }
                      }
                  }
              }
      

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

      希望这会有所帮助:)

      【讨论】:

        【解决方案6】:

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

        在您的表单中1:

        public static string Text
        {
           get { return textbox1.Text; }
        }
        

        您可以通过以下方式以其他形式获取它

        form1.Text;
        

        【讨论】:

          猜你喜欢
          • 2014-06-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多