[英]C# Selecting text in form1 from form2
Okay, so I have my main form ( Form1
) and SearchReplace
form. 好的,所以我有主窗体(
Form1
)和SearchReplace
窗体。 My SearchReplace
form contains a textbox and a button. 我的
SearchReplace
表单包含一个文本框和一个按钮。 When the button is pressed, it is supposed to select whatever was in the textbox in Form1
, but just does nothing. 当按下按钮时,应该选择
Form1
文本框中的内容,但什么也不做。 Can anyone help me? 谁能帮我? Not throwing me an error, just not doing anything runtime.
不会抛出错误,只是不执行任何运行时。
SearchReplace 搜索替换
public void button1_Click(object sender, EventArgs e)
{
Form1.searchT = textBox1.Text;
Form1 form1 = new Form1();
form1.searchText();
this.Close();
}
Form1 searchText Form1 searchText
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 is a public string created in Form1
because when I asked perviously about passing data from one form to another, someone informed me that it was easier to do it directly through Form1
instead using the form1
object. searchT是在
Form1
创建的公共字符串,因为当我反复询问有关将数据从一种表单传递到另一种表单时,有人告诉我,直接通过Form1
而不是使用form1
对象进行操作会更容易。
Based in your recent comment it's best to hide your current form (SearchReplace) before calling Form1. 根据您最近的评论,最好在调用Form1之前隐藏当前表单(SearchReplace)。 And then close it once Form1 also is closed.
一旦Form1也关闭,然后关闭它。 Check my code below:
检查下面的代码:
Let's say this is your SearchReplace
form: 假设这是您的
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
}
}
And your Form1
would be like: 您的
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;
}
}
}
}
}
The problem is that you're creating an entirely new Form
on the button click. 问题是您要在单击按钮时创建一个全新的
Form
。 You should be able to accomplish what you want with just this: 您应该能够完成以下任务:
public void button1_Click(object sender, EventArgs e)
{
Form1.searchT = textBox1.Text;
searchText(); //Calls this instance's searchText() function
this.Close();
}
You might want to make searchT
non-static, otherwise its value will apply to all instances of Form1
. 您可能希望使
searchT
为非静态,否则其值将应用于Form1
所有实例。
You can pass a reference to the textbox in form1 to the constructor of form searchreplace. 您可以将对form1中文本框的引用传递给searchreplace表单的构造函数。 Then use this reference to select the given text.
然后使用此引用选择给定的文本。
Depending how you create your SearchReplace
Form you can assign Form1
as the Owner by using the Show
or ShowDialog
Method that allows you to set the owning form, then instead of relying on setting a variable in Form1 just pass the parameter to your Function. 根据创建
SearchReplace
表单的方式,可以通过使用Show
或ShowDialog
方法(它允许您设置拥有的表单)将Form1
分配为所有者,然后不依赖于在Form1中设置变量,而只需将参数传递给Function。 Something like this should work for you. 这样的事情应该为您工作。
Form1 表格1
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
}
}
SearchReplace Form SearchReplace表格
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();
}
}
There's nothing wrong with your code, except that you only declare which text will be selected, don't expect it to automatically highlight, I suggest you change the backgroundcolor or the selected text to highlight it. 您的代码没有任何问题,除了您只声明要选择的文本,不要指望它会自动突出显示外,我建议您更改背景色或选中的文本来突出显示它。 try my example.
试试我的例子。
and I SUGGEST that you use RichTextBox
我建议你使用
RichTextBox
in your Form1 is suppose you declare a 假设您在Form1中声明了一个
public static string searchT;
then in your Form1
show the searchReplace form as a Dialog
然后在您的
Form1
中将searchReplace表单显示为Dialog
private void searchReplaceBtn_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
searchText();
}
}
then in your SearchReplace Form
然后在您的
SearchReplace Form
private void button1_Click(object sender, EventArgs e)
{
Form1.searchT = textBox1.Text;
this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
finally in your searchtext Method
最后在您的
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;
}
}
}
}
NOTE: you can't select multiple text in textbox, that's why changing the backcolor of each of the text that matches your searchT
will be the best option. 注意:您不能在文本框中选择多个文本,这就是为什么更改与
searchT
匹配的每个文本的背景色将是最佳选择的原因。
Hope this helps :) 希望这可以帮助 :)
you can do this one by using a property and giving the value of that certain textbox. 您可以通过使用属性并提供特定文本框的值来实现这一目的。
In your form1: 以您的形式1:
public static string Text
{
get { return textbox1.Text; }
}
And you can get it at your other form by 您可以通过其他形式获取它
form1.Text;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.