我设计了一个图片管理起,
form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace 图片管理器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Form2 aa = new Form2();
            try
            {
                aa.ShowDialog(this);
            }
            finally
            {
                aa.Dispose();
            }
        }
    }
}
form2using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;namespace 图片管理器
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        public Form2(ListBox lst)
        {
            InitializeComponent();
            listBox1 = lst;
            
        }
        private ListBox listBox1;
        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("没有输入任何字符","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                return;
            }
            try
            {
                string path = Application.StartupPath + "//图片目录//" + textBox1.Text;
                if (Directory.Exists(path))
                {
                    MessageBox.Show("已存在该目录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                Directory.CreateDirectory(path);
                listBox1.Items.Add(textBox1.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
            Close();
;        }
    }
}出的问题是
我新建的目录他没有出现在listbox1里面,而是出现消息框“未将对象引用到对象的实例”,但是还是会在应用程序的目录下新建目录。本人大一。

解决方案 »

  1.   

    把trycatch先注释掉,调试运行看异常中断附近哪个对象为空
      

  2.   


    他出现的是listBox1.Items.Add(textBox1.Text);这个未将对象引用到对象的实例要怎么修改呀,
      

  3.   

    你这个textBox1.Text的textBox1是你随便写的没实例化吧,就是没拖在界面上吧?
      

  4.   

    太阳的,问题在这private void toolStripButton1_Click(object sender, EventArgs e)
      {
      Form2 aa = new Form2();
      try
      {
      aa.ShowDialog(this);
      }
      finally
      {
      aa.Dispose();
      }
      }
      }
    }
      

  5.   

    没有事啊,我怎么没看到你的错误呢
    我想知道的是你这样做的目的啊,
    直接这样写   if (textBox1.Text == "")
                {
                    MessageBox.Show("不能为空");
                }
                else
                {
                    string path = Application.StartupPath + "//客户管理//" + textBox1.Text;
                    if (Directory.Exists(path))
                    {
                        MessageBox.Show("已存在该目录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    else
                    {
                        Directory.CreateDirectory(path);
                        listBox1.Items.Add(textBox1.Text);                }试试效果啊,在那个里面显示了,同样创建文件夹了,就是没什么用啊
      

  6.   

    private ListBox listBox1;
    这里listBox1是Null
    private ListBox listBox1=new ListBox();
      

  7.   


    但是添加进来,listbox1里面还是不显示那个添加的名字。
     listBox1.Items.Add(textBox1.Text);
      

  8.   

    自己学学Listbox如何使用
    http://developer.51cto.com/art/200909/150444.htm
    学会查资料,自己就能解决问题。
      

  9.   

    listBox1.Items.Add(textBox1.Text);你可以先new 一个 ListItem 然后在添加进去。你可以先把try catch去掉看看错误有助于你提高
      

  10.   

    有错误,LIstItem
    找不到命名空间?我添加了命名空间using System.Web;的
      

  11.   

      public Form2()
       {
       InitializeComponent();
       }
       public Form2(ListBox lst)
       {
       InitializeComponent();
       listBox1 = lst;
         
      }
     两个构造函数, form1里调用的第一个,listBox1 没赋值,根据你程序逻辑应该调用第二个。
      

  12.   


    额的神啊,你就try和finally就想排错啊。看书去!就看抓取异常那一章!