函数没有被调用?

问题描述 投票:0回答:2

我有这些功能:

        public void Vars()
        {
            Console.WriteLine("vars called");
            path = textBox2.Text;          
            watcher.Path = path;
            watcher.NotifyFilter = NotifyFilters.DirectoryName | NotifyFilters.FileName; 
            watcher.Created += OnCreated;
            watcher.EnableRaisingEvents = true; 
        }

        public void OnCreated(object sender, FileSystemEventArgs file)
        {
            Console.WriteLine("created called");
            if (trueFalse == "true")
            {
                File.Copy(file.FullPath, Path.Combine($"C:\\Users\\{Environment.SpecialFolder.UserProfile}\\OneDrive", file.Name));
            }
            File.Move(file.FullPath, Path.Combine(textBox3.Text, file.Name));
        }
        private void button3_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(textBox2.Text) == false & string.IsNullOrEmpty(textBox3.Text) == false & string.IsNullOrEmpty(comboBox1.Text))
            {
                Vars(); //not being called, why?
            }              
            this.Hide();
        }
        public string trueFalse = "";
        public void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
            {
                Console.WriteLine("true");
                trueFalse = "true";
            }
            else
            {
                Console.WriteLine("false");
                trueFalse = "false";
            }
        }

但是函数 Vars() 没有被调用,即使文本不是 null 或空的。为什么?我错过了什么?解决方案是什么?

我已经尝试删除被调用函数的 if 语句,它确实调用了它。但我必须让文本充满内容,否则 EnableRaisingEvents 会出错。我该怎么办?

c# winforms filesystemwatcher
2个回答
0
投票

您正在使用按位运算符

&
而不是逻辑运算符
&&

    private void button3_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(textBox2.Text)  
            && !string.IsNullOrEmpty(textBox3.Text) 
            &&  string.IsNullOrEmpty(comboBox1.Text))
        {
            Vars(); //not being called, why?
        }              
        this.Hide();
    }

0
投票

谢谢@Fildor!对不起,我是个傻瓜,我错过了

if (string.IsNullOrEmpty(textBox2.Text) == false &
    string.IsNullOrEmpty(textBox3.Text) == false &
    string.IsNullOrEmpty(comboBox1.Text))

末尾缺少一个

== false

© www.soinside.com 2019 - 2024. All rights reserved.