C# C Windows fourms VS2012-如果语句未执行
我正在尝试制作一个简单的程序来复制、移动或同步更新和替换文件。我有一个组合框,您可以在其中选择Copy、Move或Sync,到目前为止,我只为选择Copy时编写了一条语句,它调用了一个包含进程的函数,并在2个参数中发送,这是文件的源和目标 在调试过程中,当我单击开始按钮时,它没有做任何事情,因此我一步一步地运行代码从组合框中选择复制输入源和目标并按下开始,当它高亮显示每一行时,copy的IF语句被突出显示,但随后继续到下一个ELSE IF,并完全忽略copy IF语句下的说明。你能帮我调试这个问题吗?我检查了拼写错误和打字错误,但我似乎找不到任何错误,我不明白为什么它不执行它 谢谢,我的代码在下面C# C Windows fourms VS2012-如果语句未执行,c#,visual-studio-2012,if-statement,combobox,C#,Visual Studio 2012,If Statement,Combobox,我正在尝试制作一个简单的程序来复制、移动或同步更新和替换文件。我有一个组合框,您可以在其中选择Copy、Move或Sync,到目前为止,我只为选择Copy时编写了一条语句,它调用了一个包含进程的函数,并在2个参数中发送,这是文件的源和目标 在调试过程中,当我单击开始按钮时,它没有做任何事情,因此我一步一步地运行代码从组合框中选择复制输入源和目标并按下开始,当它高亮显示每一行时,copy的IF语句被突出显示,但随后继续到下一个ELSE IF,并完全忽略copy IF语句下的说明。你能帮我调试这个问
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace File_Operator
{
public partial class FileOperatorV1 : Form
{
public FileOperatorV1()
{
InitializeComponent();
}
//Browse for source directory
private void sBrowse_Click(object sender, EventArgs e)
{
// Create a new instance of FolderBrowserDialog.
FolderBrowserDialog folderBrowserDlg = new FolderBrowserDialog();
// A new folder button will display in FolderBrowserDialog.
folderBrowserDlg.ShowNewFolderButton = true;
//Show FolderBrowserDialog
DialogResult dlgResult = folderBrowserDlg.ShowDialog();
if (dlgResult.Equals(DialogResult.OK))
{
//Show selected folder path in textbox1.
textBox1.Text = folderBrowserDlg.SelectedPath;
//Browsing start from root folder.
Environment.SpecialFolder rootFolder = folderBrowserDlg.RootFolder;
}
}
//Browse for destination directory
private void dBrowse_Click(object sender, EventArgs e)
{
// Create a new instance of FolderBrowserDialog.
FolderBrowserDialog folderBrowserDlg = new FolderBrowserDialog();
// A new folder button will display in FolderBrowserDialog.
folderBrowserDlg.ShowNewFolderButton = true;
//Show FolderBrowserDialog
DialogResult dlgResult = folderBrowserDlg.ShowDialog();
if (dlgResult.Equals(DialogResult.OK))
{
//Show selected folder path in textbox2.
textBox2.Text = folderBrowserDlg.SelectedPath;
//Browsing start from root folder.
Environment.SpecialFolder rootFolder = folderBrowserDlg.RootFolder;
}
}
//Start button
private void button1_Click(object sender, EventArgs e)
{
//If comboBox1 is equal to "Copy" do this
if (comboBox1.SelectedText == "Copy")
{
//Set var "s" to the contents of textBox1
string s = textBox1.Text;
//Set var "d" to the contents of textBox2
string d = textBox2.Text;
//Run function copyDirectory with var "d" and "s" as the args
copyDirectory(s,d);
}
//If comboBox1 is equal to "Move" do this
else if (comboBox1.SelectedText == "Move")
{
}
//If comboBox1 is equal to "Sync" do this
else if (comboBox1.SelectedText == "Sync")
{
}
}
//copyDirectory function
public static void copyDirectory(string Src, string Dst)
{
String[] Files;
if (Dst[Dst.Length - 1] != Path.DirectorySeparatorChar)
Dst += Path.DirectorySeparatorChar;
if (!Directory.Exists(Dst)) Directory.CreateDirectory(Dst);
Files = Directory.GetFileSystemEntries(Src);
foreach (string Element in Files)
{
// Sub directories
if (Directory.Exists(Element))
copyDirectory(Element, Dst + Path.GetFileName(Element));
// Files in directory
else
File.Copy(Element, Dst + Path.GetFileName(Element), true);
}
}
}
}
它读这行
if (comboBox1.SelectedText == "Copy")
忽视这一点
//Set var "s" to the contents of textBox1
string s = textBox1.Text;
//Set var "d" to the contents of textBox2
string d = textBox2.Text;
//Run function copyDirectory with var "d" and "s" as the args
copyDirectory(s,d);
然后继续到这里
else if (comboBox1.SelectedText == "Move")
我真的不明白为什么它被忽略了,我很确定在任何请求之前我已经在组合框中选择了复制。感谢您的帮助。非常感谢。我认为您应该使用文本,而不是使用SelectedText。它获取或设置与此控件关联的文本
if (comboBox1.Text == "Copy")
这里,ComboxBox 1.SelectedText的问题在于它获取在组合框的可编辑部分中选择的文本
选中。SelectedText不是要使用的正确属性。SelectedBox获取或设置在组合框的可编辑部分中选择的文本。请改为使用SelectedValue。请首先查看check string string.EqualsCombox1.SelectedText、Copy、StringComparison.OrdinalIgnoreCaseNo、,但我只是尝试了一下,它仍然忽略了它。在if条件上应用一个断点,看看你得到了什么SelectedText@unlimit我刚刚尝试了您的建议,但它给了我一个生成错误-->错误1'System.Windows.Forms.ComboBox'不包含'SelectedBox'的定义,并且没有扩展方法'SelectedBox'接受第一个参数可以找到类型为“System.Windows.Forms.ComboBox”的。是否缺少using指令或程序集引用?谢谢,这非常有效!我的变量现在包含源和目标,并且正在执行条件。非常感谢。