积极答复者
C#的问题---“未将对象引用设置转到对象引用”

问题
-
//画笔功能
private void btPencil_Click(object sender, EventArgs e)
{
try
{
MyDrawType = 0;
MyEditType = 2;
step = 0;
if (this.groupBox1.Visible)
ResetMicro();
if (this.groupBox4.Visible)
this.groupBox4.Visible = false;
PictureBox pictureBox1 = new PictureBox();
string str ="PenCursor.cur";
pictureBox1.Cursor =new Cursor(GetType(), str);//未将对象引用设置转到对象引用”,请教各位大师,这个问题怎么去解决?
}
catch (NullReferenceException err)
{
MessageBox.Show(err.Message);
}}
答案
-
你好,
根据我的经验,你所用的Cursor类构造函数是直接从资源文件获取的,因此无法直接这样使用。以下是源码:
/// <summary>Initializes a new instance of the <see cref="T:System.Windows.Forms.Cursor" /> class from the specified resource with the specified resource type.</summary> /// <param name="type">The resource <see cref="T:System.Type" />. </param> /// <param name="resource">The name of the resource. </param> public Cursor(Type type, string resource) : this(type.Module.Assembly.GetManifestResourceStream(type, resource)) { }
建议:
comboBox1.Cursor = Cursors.Hand;
要不就是使用另外一个构造函数(注意相对路径):
Cursor.Current = new Cursor("MyWait.cur");
- 已建议为答案 Mike FengModerator 2012年11月30日 9:04
- 已编辑 ThankfulHeartModerator 2012年11月30日 9:11
- 已标记为答案 Mike FengModerator 2012年12月10日 8:09
-
嗯,根据你的提示,找到了另外一种解决方案:
pictureBox1.Cursor =new Cursor("PenCursor.cur");把文件PenCursor.cur放在当前程序运行的Debug文件夹里就可以解决问题了,谢谢啦!
我的邮件
- 已标记为答案 Mike FengModerator 2012年12月10日 8:09
全部回复
-
你好,
根据我的经验,你所用的Cursor类构造函数是直接从资源文件获取的,因此无法直接这样使用。以下是源码:
/// <summary>Initializes a new instance of the <see cref="T:System.Windows.Forms.Cursor" /> class from the specified resource with the specified resource type.</summary> /// <param name="type">The resource <see cref="T:System.Type" />. </param> /// <param name="resource">The name of the resource. </param> public Cursor(Type type, string resource) : this(type.Module.Assembly.GetManifestResourceStream(type, resource)) { }
建议:
comboBox1.Cursor = Cursors.Hand;
要不就是使用另外一个构造函数(注意相对路径):
Cursor.Current = new Cursor("MyWait.cur");
- 已建议为答案 Mike FengModerator 2012年11月30日 9:04
- 已编辑 ThankfulHeartModerator 2012年11月30日 9:11
- 已标记为答案 Mike FengModerator 2012年12月10日 8:09
-
嗯,根据你的提示,找到了另外一种解决方案:
pictureBox1.Cursor =new Cursor("PenCursor.cur");把文件PenCursor.cur放在当前程序运行的Debug文件夹里就可以解决问题了,谢谢啦!
我的邮件
- 已标记为答案 Mike FengModerator 2012年12月10日 8:09