0

さて、メインフォーム ( 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;
                }
            }
        }
    }

Form1以前、あるフォームから別のフォームにデータを渡すことについて尋ねたとき、オブジェクトForm1を使用する代わりに直接渡す方が簡単だと誰かが私に言ったので、 searchT はで作成されたパブリック文字列です。form1

4

6 に答える 6

1

最近のコメントに基づいて、Form1 を呼び出す前に現在のフォーム (SearchReplace) を非表示にすることをお勧めします。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;
            }
        }
    }
  }    
}
于 2013-10-22T02:01:35.640 に答える
0

フォームの作成方法に応じて、所有フォームを設定できるまたはメソッドを使用して所有者としてSearchReplace割り当てることができます。次に、Form1 で変数を設定することに頼るのではなく、パラメーターを関数に渡します。このようなものがうまくいくはずです。Form1ShowShowDialog

フォーム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
    }
}

検索置換フォーム

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();
    }
}
于 2013-10-22T02:58:51.183 に答える
0

コードに問題はありませんが、選択するテキストを宣言するだけで、自動的に強調表示されるとは思わないでください。背景色または選択したテキストを変更して強調表示することをお勧めします。私の例を試してください。

そして、私はあなたが使用することをお勧めします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ことが最適なオプションになります。

お役に立てれば :)

于 2013-10-22T03:20:56.427 に答える