【发布时间】:2014-05-11 23:41:58
【问题描述】:
我正在尝试制作一个简单的程序来复制、移动或同步(更新和替换)文件。我有一个组合框,您可以在其中选择复制、移动或同步,到目前为止,我只写了一个关于何时选择“复制”的语句,它调用一个包含进程的函数并发送 2 个参数,即源和目标对于文件。
在调试期间,当我单击开始按钮时它没有做任何事情,所以我一步一步地运行代码,从组合框中选择“复制”,输入源和目标并按下开始,因为它突出显示每一行,复制的 IF 语句被突出显示,但随后继续到下一个 ELSE IF 并完全忽略了“复制”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不是正确使用的属性。SelectedBox获取或设置在 ComboBox 的可编辑部分中选择的文本。请改用SelectedValue见msdn.microsoft.com/en-us/library/… -
首先检查字符串
string.Equals(comboBox1.SelectedText, "Copy", StringComparison.OrdinalIgnoreCase) -
不,但我刚刚尝试过它仍然忽略它。
-
在 if 条件下应用断点,看看你在
SelectedText得到什么 -
@unlimit 我刚刚尝试了你的建议,它给了我一个构建错误---->错误1'System.Windows.Forms.ComboBox'不包含'SelectedBox'的定义并且没有可以找到接受“System.Windows.Forms.ComboBox”类型的第一个参数的扩展方法“SelectedBox”(您是否缺少 using 指令或程序集引用?)。
标签: c# visual-studio-2012 if-statement combobox