2013-12-08 97 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); 
    
    }  
    
相关问题