本文介绍了C#在folderbrowser.exe中发生了'system.nullreferenceexception'类型的未处理异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;

using WMPLib;

using System.Media;

namespace folderBrowser
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        internal class ListItem
        {
            string path;
            private string v;

            public ListItem(string v)
            {
                this.v = v;
            }
        }
        private string v;
        public ListBox.SelectedIndexCollection SelectedIndices { get; }
        SoundPlayer player = new SoundPlayer();
        WindowsMediaPlayer myplayer = new WindowsMediaPlayer();

        private void button1_Click(object sender, EventArgs e)
        {
            string[] filters = { "*.jpg", "*.jpeg", "*.png", "*.gif", "*.bmp" };

            var directory = new DirectoryInfo(@"D:\Dogukan\Ingcart");

            var files = new List<fileinfo>();

            foreach (var filter in filters)
            {
                var results = directory.GetFiles(filter, SearchOption.AllDirectories);
                files.AddRange(results);
            }

            foreach (FileInfo file in files)
            {
                //ListItem a = new ListItem(Path.GetFileNameWithoutExtension(file.FullName));
                //listBox1.Items.Add(a);
                listBox1.Items.Add(Path.GetFileNameWithoutExtension(file.FullName));
            }
        }

        private void listBox1_MouseClick(object sender, MouseEventArgs e)
        {

                 pictureBox1.Image = Image.FromFile(((FileInfo)listBox1.SelectedValue).FullName);
                // pictureBox1.Image = Image.FromFile(((FileInfo)listBox1.SelectedItem).ToString());
                textBox1.Text = listBox1.SelectedItem.ToString();


        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //pictureBox1.Image = Image.FromFile(((FileInfo)listBox1.SelectedItem).FullName);
            pictureBox1.Image = Image.FromFile(((FileInfo)listBox1.SelectedValue).FullName);
            textBox1.Text = listBox1.SelectedItem.ToString();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            myplayer.URL = @"D:\Dogukan\" + "//" + listBox1.SelectedItem.ToString() + ".mp3";

            myplayer.controls.play();
        }
        private void FindMyString(string searchString)
        {
            for (int i = 0; i < listBox1.Items.Count; i++)
            {
                if (listBox1.Items[i].ToString().IndexOf(searchString, StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    listBox1.SetSelected(i, true);
                }
                else {
                    // Do this if you want to select in the ListBox only the results of the latest search.
                    listBox1.SetSelected(i, false);
                }
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            //int index = listBox1.FindString(this.textBox2.Text);
            //if (0 <= index)
            //{
            //    listBox1.SelectedIndex = index;
            //}

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
 }





我尝试了什么:





What I have tried:

private void listBox1_MouseClick(object sender, MouseEventArgs e)

我写 pictureBox1.Image = Image.FromFile(((FileInfo)listBox1.SelectedItem).FullName); 代码,当我启动程序并加载图片时,查看listBox1上的文件比我点击鼠标上的文件错误System.NullReferenceException'发生在folderBrowser.exe中。我该怎么办?

I write pictureBox1.Image = Image.FromFile(((FileInfo)listBox1.SelectedItem).FullName); code, when i started program and load picture, view files on the listBox1 than i clicked mouse on file error System.NullReferenceException' occurred in folderBrowser.exe. What must i do ?

推荐答案




这篇关于C#在folderbrowser.exe中发生了'system.nullreferenceexception'类型的未处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:18