Question

I'm trying to make a simple program to either copy, move or sync (update and replace) files. I have a combobox where you select Copy, Move or Sync and so far I've only written a statement for when "Copy" is selected, and it calls a function that contains the process and sends in 2 args which is the source and destination for the files.

During debug it wasn't doing anything when I clicked the start button, so I ran through the code step by step selected "Copy" from the combobox entered the source and destination and pressed start and as it was highlighting each line, the IF statement for copy got highlighted but then carried on to the next ELSE IF and completely ignored the instructions under the "Copy" IF statement. Could you help me debug this problem please? I've checked for spelling errors and typos but I can't seem to find any, I don't understand why it's not executing it.

Thanks, my code is below.

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);
        }
    }
}
}

It reads this line

if (comboBox1.SelectedText == "Copy")

Ignores this

//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);

And then carries on to here

else if (comboBox1.SelectedText == "Move")

I really don't understand why it's being ignored, and I'm very sure I've been selecting "Copy" in the combobox before any asks. Any help is appreciated. Many thanks.

Was it helpful?

Solution

I think instead of using SelectedText you should use Text. It gets or sets the text associated with this control.

if (comboBox1.Text == "Copy")

Here, problem with comboxBox1.SelectedText is that it gets the text that is selected in the editable portion of a ComboBox.

Check MSDN Reference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top