标题:C# 未将对象引用设置到对象的实例 怎么改?
只看楼主
nancy小方
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2013-12-23
结帖率:0
已结贴  问题点数:20 回复次数:2 
C# 未将对象引用设置到对象的实例 怎么改?
请各位大神指点,调试的时候总是在加红的那2行出现“未将对象引用设置到对象的实例”的提示。应该怎么改啊?
namespace 界面1
{
    public delegate void ChangeImageHandler(String message);
    public partial class Form4 : Form
    {
        private ShowPic _ShowPic;
        public Form4()
        {
            InitializeComponent();
        }
         private void Form4_Load(object sender, EventArgs e)
        {
            _ShowPic = new ShowPic(pictureBox1);
            _ShowPic.ChangeImage += new ChangeImageHandler(ChangeImage);
            _ShowPic.Load(@"E:\2\现场图片\1", SearchOption.TopDirectoryOnly);
        }

        private void label2_Click(object sender, EventArgs e)
        {
            _ShowPic.Last();
        }

        private void label3_Click(object sender, EventArgs e)
        {
            _ShowPic.Next();
        }
         private void ChangeImage(String message)
        {
            this.Text = message;
        }
        private void Form4_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (_ShowPic != null)
            {
                _ShowPic = null;
            }
        }
    }

        public sealed class ShowPic
        {
            public event ChangeImageHandler ChangeImage;
            private String[] _ArrPicPath;
            private Int32 _Index;
            private PictureBox _PictureBox;

            private void OnChangeImage(String message)
           {
                ChangeImageHandler temp = (ref ChangeImage, null, null);
                if (temp != null)
                {
                   temp(message);
                 }
            }

            public ShowPic(PictureBox pictureBox)
            {
                _PictureBox = pictureBox;
                _Index = 0;
            }
            
            public void Load(String path, SearchOption searchOption)
            {
                if (!Directory.Exists(path))
               {
                    OnChangeImage(path + " 路径不存在");
                    throw new DirectoryNotFoundException(path + " 路径不存在");
                }
               _ArrPicPath = (path,"*.jpg", searchOption);
               LoadImage();
            }

            public void Last()
            {
                lock (_ArrPicPath)
                {
                   if (_Index > 0)
                   {
                       _Index--;
                    }
                    else
                    {
                        OnChangeImage("已经是第一张");
                        return;
                     }
                }
            
                LoadImage();
            }

            public void Next()
           {
              lock (_ArrPicPath)
              {
                 if (_Index < _ArrPicPath.Length - 1)
                 {
                     _Index++;
                  }
                 else
                 {
                    OnChangeImage("已经是最后一张");
                    return;
                  }
               }
               LoadImage();
            }
            private void LoadImage()
            {
                if (_ArrPicPath == null || _ArrPicPath.Length <= 0)
                {
                    OnChangeImage("没有图片");
                    return;
                }
                String path = _ArrPicPath[_Index];
                if (File.Exists(path))
                {
                    _PictureBox.Load(path);
                    OnChangeImage(path);
                }
            }
        }
      
}
本人新手,还没有分可给,不好意思!
搜索更多相关主题的帖子: message private public 
2013-12-23 10:58
xunmengwuya
Rank: 2
等 级:论坛游民
帖 子:1
专家分:10
注 册:2013-12-24
得分:10 
_ShowPic这个东东没有找到,为null,再去调用 方法当然会报错了
2013-12-24 16:42
moridiansha
Rank: 6Rank: 6
来 自:承德
等 级:侠之大者
威 望:4
帖 子:254
专家分:417
注 册:2009-10-21
得分:10 
你在load里面初始化_ShowPic,在点击事件里是获取不到的,用别的方法再试试吧,
可以写个属性,如果是空的话自动实例化一个

www.qysy.tk
2013-12-25 11:16



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-425575-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.053988 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved