2013-12-08 89 views
0
public void FilenameHasWord(string filename, string word) 
    { 
     Boolean red = filename.Contains(word); 
     if (red == true) 
     { 
      MessageBox.Show(" the filename contains the word"); 
      if (red == false) 
      { 
     MessageBox.Show(" the filename does not contain the word"); 
      } 


private void button2_Click(object sender, EventArgs e) 
    { 
     FilenameHasWord(); 
     string filename = "textBox1.Text"; 
     string word = "textBox2.Text"; 
    }  
+1

*它有什麼錯誤? (確切的消息是什麼?)*錯誤發生在哪裏?它是編譯器錯誤還是運行時錯誤? –

+1

'if(red)'和'if(!red)',請。 – JensG

+0

或更好在這種情況下'if(red){...} else {....}' –

回答

-1

1.您需要分配TextBox1TextBox2控件中的值。但在您的代碼中,您將TextBox1和TextBox2分配爲字符串。

2.您需要將這些值分配給FilenameHasWord()函數,因爲它需要兩個參數。

試試這個:

public void FilenameHasWord(string filename, string word) 
{ 

    Boolean red = filename.Contains(word); 
    if (red == true) 
    { 
    MessageBox.Show(" the filename contains the word");  
    } 

    if (red == false) 
    { 
     MessageBox.Show(" the filename does not contain the word"); 
    } 
} 
private void button2_Click(object sender, EventArgs e) 
{ 

    string filename = textBox1.Text; 
    string word = textBox2.Text; 
    FilenameHasWord(filename,word); 

}  
+0

@Downvoter:請在這裏解釋錯誤。 –

+0

我不明白你爲什麼被低估 - 這是代碼中的錯誤之一。還有另一個,但由於OP沒有具體說明他在說什麼錯誤,所以這與另一個答案一樣正確。 – Ilan321

+0

FilenameHasWord()需要2個參數。這不會編譯 – geedubb

1

你的功能FilenameHasWord()需要兩個參數。當你打電話時你沒有任何傳球。
此外,您聲明兩個字符串分別包含「textBox1.Text」和「textBox2.Text」,也許您需要刪除這些引號。

+0

我同意。大聲笑... – uSeRnAmEhAhAhAhAhA

3

這裏混亂的一點,我想你想:

public void FilenameHasWord(string filename, string word) 
{ 

    Boolean red = filename.Contains(word); 
    if (red == true) 
    { 
     MessageBox.Show(" the filename contains the word"); 
    } 
    else 
    { 
     MessageBox.Show(" the filename does not contain the word"); 
    } 
} 

private void button2_Click(object sender, EventArgs e) 
{ 
    string filename = textBox1.Text; 
    string word = textBox2.Text; 
    FilenameHasWord(filename, word); 
}  

首先,你的測試包含返回TRUE或FALSE這樣一個if/else語句來顯示結果,而不是塊內的if(false)然後,當你調用函數時,你應該傳遞TWO字符串參數。這兩個字符串參數應該是文本框的內容,並且這個內容被表達爲寫入表示文本框的變量,後面是沒有雙引號的屬性Text

現在,重新閱讀您的留言箱文字,我有疑問。 the filename (does not) contains the word是什麼意思? string.Contains方法檢查一個字符串是否包含另一個字符串。

string.Contains不嘗試使用實例字符串,假設它是文件的名稱,並且不檢查該假定文件是否包含作爲參數傳遞的單詞。

+1

該死的,你只是打敗我吧!:) +1 –

+3

+1我只會寫'如果(紅色)'單獨 –

+0

@SriramSakthivel是的,它足夠簡潔。我留給OP來說很清楚 – Steve

0

請嘗試這一點 - 它解析:

  1. 問題與不讀文本框的值正確
  2. FilenameHasWord方法需要2個PARAMS
  3. 撐不正確匹配

    public void FilenameHasWord(string filename, string word) 
    { 
        if (filename.Contains(word)) 
        { 
         MessageBox.Show(" the filename contains the word"); 
        } 
        else 
        { 
         MessageBox.Show(" the filename does not contain the word"); 
        } 
    } 
    
    private void button2_Click(object sender, EventArgs e) 
    { 
        string filename = textBox1.Text; 
        string word = textBox2.Text; 
        FilenameHasWord(filename,word); 
    
    }  
    
相關問題