2013-10-22 21 views
0

好的,所以我有我的主窗體(Form1)和SearchReplace窗體。我的SearchReplace表單包含一個文本框和一個按鈕。當按下按鈕時,它應該選擇Form1中的文本框中的任何內容,但什麼也不做。誰能幫我?不要拋出一個錯誤,只是不做任何運行時。C#在form1中從form1中選擇文本

SearchReplace

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


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

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是Form1創建,因爲當我問perviously大約從一種形式到另一種傳遞數據,有人告訴我一個公共字符串直接通過Form1而不是使用form1對象更容易。

+0

你如何創建SearchReplace形式,它從Form1中在你的其他形式得到它? –

+0

通過運行Form1 form1 = new Form1();您正在創建一個新的(不可見的)窗體實例。這與您正在顯示的mian表單不一樣。 –

回答

0

問題是,您正在按鈕單擊上創建一個全新的Form。你應該能夠完成你想要與眼前這個是什麼:

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

你可能想使searchT非靜態,否則其價值將適用於Form1所有實例。

+0

我不得不創建它的一個新對象,因爲只要輸入'searchText();'就會拋出一個錯誤,說它不存在。我必須創建對象來調用它。 – Mercifies

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

拋出一個錯誤,說不能用實例引用訪問;相反,使用類型名稱來限定它。我如何將它定義爲一個字符串? – Mercifies

+1

@Mercifies使用此答案中的代碼,它不會產生該錯誤。你的做法是因爲你聲明'searchT'是靜態的。 –

+0

這段代碼的問題在於,要打開'SearchReplace',用戶將已經在'form'的textbox1中有數據,所以如果我要關閉它並打開另一個,它將清除數據 – Mercifies

0

您可以在Form1傳遞給到文本框的引用窗體searchreplace的構造函數。 然後使用此引用來選擇給定的文本。

0

取決於你如何創建SearchReplace表格,你可以指定Form1通過使用ShowShowDialog方法,它允許您設置的擁有形式,然後而不是依靠設置在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 
    } 
} 

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(); 
    } 
} 
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將是最好的背景色選項。

希望這有助於:)

0

您可以通過使用屬性和給某些文本框的值做到這一點的。

在你Form1中:

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

,您可以通過

form1.Text; 
相關問題