locked
search and show results RRS feed

  • Question

  • I need to get my program to search for a certain extention type and display them in the list box  or whatever box will work better. someone please help..


    alose I need to know how to make the picture box work. like if this search returns results I want to be ables to highlight one of the results and have the imagebox display the image.... Visual Basic
    Saturday, October 24, 2009 4:50 PM

Answers

  • Hello Thomas.G,

    Based on my understanding, you want to list all the image files and load them into ListBox, when select an item in the listbox, the PictureBox will display the image. If my understanding is correct, the following sample can implement this function.
    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                listBox1.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);
            }

            void listBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                string fileName = listBox1.SelectedItem.ToString();
                Image img = Image.FromFile(fileName);
                pictureBox1.Image = img;
            }

            private void btnSearch_Click(object sender, EventArgs e)
            {
                LoadAllFiles(@"D:\MyPicture", this.listBox1);
            }

            private void LoadAllFiles(string folderPath, ListBox listBox)
            {
                DirectoryInfo dirInfo = new DirectoryInfo(folderPath);
                FileInfo[] fileInfo = dirInfo.GetFiles("*.jpg", SearchOption.AllDirectories);
                listBox.Items.Clear();
                foreach (FileInfo file in fileInfo)
                {
                    listBox.Items.Add(file.FullName);
                }
            }
        }
    Hope this helps. If you have any question with my code, please feel free to tell me.

    Sincerely,
    Kira Qian
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    • Marked as answer by Kira Qian Monday, November 9, 2009 8:47 AM
    Friday, October 30, 2009 3:56 AM