using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace sound
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.button1.Enabled = false; this.textBox1.Tag = false;
this.TextBoxAss.Tag = false;
this.textBox2.Tag = false;
this.textBox3.Tag = false; this.textBox1.Validating += new System.ComponentModel.CancelEventHandler(this.textBox_Validating);
this.TextBoxAss.Validating += new System.ComponentModel.CancelEventHandler(this.textBox_Validating);
this.textBox2.Validating += new System.ComponentModel.CancelEventHandler(this.textBox2_Validating);
this.textBox3.Validating += new System.ComponentModel.CancelEventHandler(this.textBox_Validating); this.textBox1.TextChanged += new System.EventHandler(this.TextBox_TextChanged);
this.TextBoxAss.TextChanged += new System.EventHandler(this.TextBox_TextChanged);
this.textBox2.TextChanged += new System.EventHandler(this.TextBox_TextChanged);
this.textBox3.TextChanged += new System.EventHandler(this.TextBox_TextChanged);
} private void button1_Click(object sender, EventArgs e)
{
string output; output = "姓名:" + this.textBox1.Text + "\r\n";
output += "地址:" + this.TextBoxAss.Text + "\r\n";
output += "职业:" + this.textBox2.Text + "\r\n";
output += "年龄:" + this.textBox3.Text + "\r\n"; this.richTextBox1.Text = output;
} private void button2_Click(object sender, EventArgs e)
{
string output;
output = "姓名=你的名字\r\n";
output += "地址=你的地址\r\n";
output += "职业=你的职业\r\n";
output += "年龄=你的年龄\r\n"; this.richTextBox1.Text = output;
} private void textBox_Validating(object sender, CancelEventArgs e)
{
TextBox tb = (TextBox)sender;
if (tb.Text.Length == 0)
{
tb.BackColor = Color.Red;
tb.Tag = false;
}
else
{
tb.BackColor = System.Drawing.SystemColors.Window;
tb.Tag = true;
} ValidateOK(); } private void textBox2_Validating(object sender, CancelEventArgs e)
{
TextBox tb = (TextBox)sender;
if (tb.Text.CompareTo("程序员")==0||tb.Text.Length==0)
{
tb.Tag = true;
tb.BackColor = System.Drawing.SystemColors.Window;
}
else
{
tb.Tag = false;
tb.BackColor = Color.Red;
}
ValidateOK();
} private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
{
if((e.KeyChar<48||e.KeyChar>57)&&e.KeyChar!=8)
{
e.Handled = true;
}
}
private void ValidateOK()
{
this.button1.Enabled = ((bool)(this.textBox1.Tag)
&& (bool)(this.textBox2.Tag) && (bool)(this.textBox3.Tag)
&& (bool)(this.TextBoxAss.Tag));
} private void TextBox_TextChanged(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender; if (tb.Text.Length == 0 && tb != textBox2)
{
tb.Tag = false;
tb.BackColor = Color.Red;
}
else if (tb == textBox2 && (tb.Text.Length != 0 && tb.Text.CompareTo("程序员") != 0))
{
tb.Tag = false;
}
else
{
tb.Tag = true;
tb.BackColor = SystemColors.Window;
}
ValidateOK();
}
}
}我是C#的初学者,按照C#入门经典里面的例子,我做了上面这个,在RICHTEXTBOX那输入时(也就是代码里面的TextBoxAss),总是提示RichTextBox对象不能赋值给TextBox?然后提示我在上面红色字体的那句错误?请教各位,帮我解答解答,先谢谢了。
也顺带请教下学习C#的方法,本人对那些很多的接口,不是很了解。
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace sound
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.button1.Enabled = false; this.textBox1.Tag = false;
this.TextBoxAss.Tag = false;
this.textBox2.Tag = false;
this.textBox3.Tag = false; this.textBox1.Validating += new System.ComponentModel.CancelEventHandler(this.textBox_Validating);
this.TextBoxAss.Validating += new System.ComponentModel.CancelEventHandler(this.textBox_Validating);
this.textBox2.Validating += new System.ComponentModel.CancelEventHandler(this.textBox2_Validating);
this.textBox3.Validating += new System.ComponentModel.CancelEventHandler(this.textBox_Validating); this.textBox1.TextChanged += new System.EventHandler(this.TextBox_TextChanged);
this.TextBoxAss.TextChanged += new System.EventHandler(this.TextBox_TextChanged);
this.textBox2.TextChanged += new System.EventHandler(this.TextBox_TextChanged);
this.textBox3.TextChanged += new System.EventHandler(this.TextBox_TextChanged);
} private void button1_Click(object sender, EventArgs e)
{
string output; output = "姓名:" + this.textBox1.Text + "\r\n";
output += "地址:" + this.TextBoxAss.Text + "\r\n";
output += "职业:" + this.textBox2.Text + "\r\n";
output += "年龄:" + this.textBox3.Text + "\r\n"; this.richTextBox1.Text = output;
} private void button2_Click(object sender, EventArgs e)
{
string output;
output = "姓名=你的名字\r\n";
output += "地址=你的地址\r\n";
output += "职业=你的职业\r\n";
output += "年龄=你的年龄\r\n"; this.richTextBox1.Text = output;
} private void textBox_Validating(object sender, CancelEventArgs e)
{
TextBox tb = (TextBox)sender;
if (tb.Text.Length == 0)
{
tb.BackColor = Color.Red;
tb.Tag = false;
}
else
{
tb.BackColor = System.Drawing.SystemColors.Window;
tb.Tag = true;
} ValidateOK(); } private void textBox2_Validating(object sender, CancelEventArgs e)
{
TextBox tb = (TextBox)sender;
if (tb.Text.CompareTo("程序员")==0||tb.Text.Length==0)
{
tb.Tag = true;
tb.BackColor = System.Drawing.SystemColors.Window;
}
else
{
tb.Tag = false;
tb.BackColor = Color.Red;
}
ValidateOK();
} private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
{
if((e.KeyChar<48||e.KeyChar>57)&&e.KeyChar!=8)
{
e.Handled = true;
}
}
private void ValidateOK()
{
this.button1.Enabled = ((bool)(this.textBox1.Tag)
&& (bool)(this.textBox2.Tag) && (bool)(this.textBox3.Tag)
&& (bool)(this.TextBoxAss.Tag));
} private void TextBox_TextChanged(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender; if (tb.Text.Length == 0 && tb != textBox2)
{
tb.Tag = false;
tb.BackColor = Color.Red;
}
else if (tb == textBox2 && (tb.Text.Length != 0 && tb.Text.CompareTo("程序员") != 0))
{
tb.Tag = false;
}
else
{
tb.Tag = true;
tb.BackColor = SystemColors.Window;
}
ValidateOK();
}
}
}我是C#的初学者,按照C#入门经典里面的例子,我做了上面这个,在RICHTEXTBOX那输入时(也就是代码里面的TextBoxAss),总是提示RichTextBox对象不能赋值给TextBox?然后提示我在上面红色字体的那句错误?请教各位,帮我解答解答,先谢谢了。
也顺带请教下学习C#的方法,本人对那些很多的接口,不是很了解。
if(tb!=null)
{
//说明sender是一个TextBox
}
else
{
TextBox tb=new TextBox();
tb.Text= (sender as RichTexBox).Text;
}
可是我是按照书上的做法来的?哎,出来下面这样的提示未处理的“System.InvalidCastException”类型的异常出现在 sound.exe 中。其他信息: 无法将类型为“System.Windows.Forms.RichTextBox”的对象强制转换为类型“System.Windows.Forms.TextBox”。
出来如下这样的提示。未处理的“System.InvalidCastException”类型的异常出现在 sound.exe 中。其他信息: 无法将类型为“System.Windows.Forms.RichTextBox”的对象强制转换为类型
对象不可能相互赋值 除非是字符串相互赋值
这是必然的吧!
你给RichTextBox邦定了TextChanged事件,在事件方法里面sender就是那个RichTextBox,当然不能将它强制转换为TextBox。
楼主是初学者的话还可以理解,在这里可以把RichTextBox比作男人,TextBox比作女人,而两个类的共同的基类TextBoxBase比作人,虽然两个类都继承自同一基类,即男人和女人都是人,但不能直接拿男人看做女人来用(汗),但可以把男人(RichTextBox)看作为人来用(再汗),所以本例你把那个强转改成这样就行了:
TextBoxBase tb = (TextBoxBase)sender
这个,楼主不能理解的话还是看看书再实践实践吧~~
为了积分......