我设计了一个图片管理起,
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里面,而是出现消息框“未将对象引用到对象的实例”,但是还是会在应用程序的目录下新建目录。本人大一。
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里面,而是出现消息框“未将对象引用到对象的实例”,但是还是会在应用程序的目录下新建目录。本人大一。
他出现的是listBox1.Items.Add(textBox1.Text);这个未将对象引用到对象的实例要怎么修改呀,
{
Form2 aa = new Form2();
try
{
aa.ShowDialog(this);
}
finally
{
aa.Dispose();
}
}
}
}
我想知道的是你这样做的目的啊,
直接这样写 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); }试试效果啊,在那个里面显示了,同样创建文件夹了,就是没什么用啊
这里listBox1是Null
private ListBox listBox1=new ListBox();
但是添加进来,listbox1里面还是不显示那个添加的名字。
listBox1.Items.Add(textBox1.Text);
http://developer.51cto.com/art/200909/150444.htm
学会查资料,自己就能解决问题。
找不到命名空间?我添加了命名空间using System.Web;的
{
InitializeComponent();
}
public Form2(ListBox lst)
{
InitializeComponent();
listBox1 = lst;
}
两个构造函数, form1里调用的第一个,listBox1 没赋值,根据你程序逻辑应该调用第二个。
额的神啊,你就try和finally就想排错啊。看书去!就看抓取异常那一章!