none
How do we read full path from the list box in C#.. RRS feed

  • Question

  • I am using list box in windows c#.

    I am loading bulk xls or mdb file in to the list box.

    Here i don't want to display full path in the list box. It's enough to display only file name.

    But, while reading the file from the list box, i want to get the full path.

    How to do this in list box c#....?

    Note : i just want to display the full path while mouse hover...

    Thursday, February 13, 2020 6:20 AM

Answers

  • For example, with a ListBox  listBox1 :

    private ToolTip toolTip1 = new ToolTip();

    string sDirectory = @"c:\";           
    listBox1.Items.Clear();           
    
    toolTip1.AutoPopDelay = 5000;
    toolTip1.InitialDelay = 1000;
    toolTip1.ReshowDelay = 500;
    toolTip1.ShowAlways = true;
    
    try
    {
        var txtFiles = Directory.EnumerateFiles(sDirectory, "*.*", SearchOption.AllDirectories);
        foreach (string currentFile in txtFiles)
        {
            string sFileName = Path.GetFileName(currentFile.ToString());
            listBox1.Items.Add(new ItemData(currentFile.ToString(), sFileName));
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }


    public struct ItemData
    {
        public string sPath;
        public string sDisplayPath;
    
        public ItemData(string _sPath, string _sDisplayPath)
        {
            sPath = _sPath;
            sDisplayPath = _sDisplayPath;
        }
    
        public override string ToString()
        {
            return this.sDisplayPath;
        }  
    }

    // listBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(listBox1_MouseMove);
    private void listBox1_MouseMove(object sender, MouseEventArgs e)
    {
        int nIndex = listBox1.IndexFromPoint(e.Location);
        if (nIndex != ListBox.NoMatches)
        {
            string sTip = ((ItemData)listBox1.Items[nIndex]).sPath;
            if (toolTip1.GetToolTip(listBox1) != sTip)
                toolTip1.SetToolTip(listBox1, sTip);
        }
    }



    • Edited by Castorix31 Thursday, February 13, 2020 9:51 AM
    • Marked as answer by Gani tpt Thursday, February 13, 2020 12:16 PM
    Thursday, February 13, 2020 9:50 AM

All replies

  • For example, with a ListBox  listBox1 :

    private ToolTip toolTip1 = new ToolTip();

    string sDirectory = @"c:\";           
    listBox1.Items.Clear();           
    
    toolTip1.AutoPopDelay = 5000;
    toolTip1.InitialDelay = 1000;
    toolTip1.ReshowDelay = 500;
    toolTip1.ShowAlways = true;
    
    try
    {
        var txtFiles = Directory.EnumerateFiles(sDirectory, "*.*", SearchOption.AllDirectories);
        foreach (string currentFile in txtFiles)
        {
            string sFileName = Path.GetFileName(currentFile.ToString());
            listBox1.Items.Add(new ItemData(currentFile.ToString(), sFileName));
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }


    public struct ItemData
    {
        public string sPath;
        public string sDisplayPath;
    
        public ItemData(string _sPath, string _sDisplayPath)
        {
            sPath = _sPath;
            sDisplayPath = _sDisplayPath;
        }
    
        public override string ToString()
        {
            return this.sDisplayPath;
        }  
    }

    // listBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(listBox1_MouseMove);
    private void listBox1_MouseMove(object sender, MouseEventArgs e)
    {
        int nIndex = listBox1.IndexFromPoint(e.Location);
        if (nIndex != ListBox.NoMatches)
        {
            string sTip = ((ItemData)listBox1.Items[nIndex]).sPath;
            if (toolTip1.GetToolTip(listBox1) != sTip)
                toolTip1.SetToolTip(listBox1, sTip);
        }
    }



    • Edited by Castorix31 Thursday, February 13, 2020 9:51 AM
    • Marked as answer by Gani tpt Thursday, February 13, 2020 12:16 PM
    Thursday, February 13, 2020 9:50 AM
  • Hi Gani tpt,
    Based on your description, what type of your project? And could you provide related some code?
    Here is winform code example that you can click the filename of the listBox and then Pop up a popup window displaying the full path.

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        var selectedItems = listBox1.SelectedItems.Cast<FileItem>();
        var all = string.Join(Environment.NewLine, selectedItems.Select(x => x.Path));
        MessageBox.Show(all);
    }
    private void button1_Click(object sender, EventArgs e)
    {
        string[] files = Directory.GetFiles(@"C:\Test", "*.xls", SearchOption.AllDirectories);
        foreach (string f in files)
        {
            var fileItem = new FileItem { Title = Path.GetFileName(f), Path = Path.GetFullPath(f) };
            listBox1.Items.Add(fileItem);
        }
        listBox1.DisplayMember = "Title";
        listBox1.ValueMember = "Path";
    }
    public class FileItem
    {
        public string Title { get; set; }
        public string Path { get; set; }
    }

    Hope it is helpful for you.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 13, 2020 9:58 AM
  • Thanks.

    MouseMove not working...

    Thursday, February 13, 2020 11:30 AM
  • This windows form based project.

    i am browsing the file and adding (btnadd_click) into a list box.

    After clicking some event (btnok_click), then it should read the full path.

    pls. refer the below code.

    Highlighted below code in btnok_click to get full path......?

     private void btnadd_Click(object sender, EventArgs e)
            {
                DialogResult Dialogresult;
                openFileDialog1.Multiselect = true;
                openFileDialog1.Filter = "MDB files (*.mdb)|*.mdb;";
                Dialogresult = openFileDialog1.ShowDialog();
                if (Dialogresult == DialogResult.OK)
                {
                    string[] files;
                    files = openFileDialog1.FileNames;
                    foreach (string f in files)
                    {
                        var fileItem = new FileItem { Title = Path.GetFileName(f), Path = Path.GetFullPath(f) };
                        listBox1.Items.Add(fileItem);
                    }
                    listBox1.DisplayMember = "Title";
                    listBox1.ValueMember = "Path";
                }
            }
    
            private void btnok_Click(object sender, EventArgs e)
            {
                for (int i = 0; i < listBox1.Items.Count; i++)
                {
                   
                    string MDBFullpath = listBox1.Items[i].ToString(); // Need to get full path...?
                }
            }

    Thursday, February 13, 2020 11:36 AM
  • Thanks.

    MouseMove not working...

    You can see in the GIF that it works...

    Have you added by Designer or by code :

    listBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(listBox1_MouseMove);


    • Edited by Castorix31 Thursday, February 13, 2020 11:46 AM
    Thursday, February 13, 2020 11:45 AM
  • where we want to add the below code. it is inside the button click event or form load...?

    listBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(listBox1_MouseMove);

    You can add it on Form1_Load

    but it is usually added automatically by double-clicking on the MouseMove event of the Listbox in the Designer

    Thursday, February 13, 2020 12:01 PM
  • Yes. Everything working...You rock it...

    Thanks a lot.

    Thursday, February 13, 2020 12:16 PM