一:构造函数的复用
如果继承自某类可以使用关键字base:
class baseclass
{
.......//其他类成员,方法属性等
string _str;
baseclass(){} //无参数的构造函数,在此忽略成员函数的初始化
baseclass(string str):this() //有参数的构造函数,调用上面的无参数的构造函数实现复用
{
_str = str;
}
......//类的实现部分
}
public class driveclass : baseclass
{
.......//其他类成员,方法属性等
string _dstr;
baseclass():base() //无参数的构造函数,调用基类无参数的构造函数,在此也忽略成员函数的初始化
{}
baseclass(string str):base(str) //有参数的构造函数,调用基类的带参数的构造函数实现复用
{
_dstr = str;
}
......//类的实现部分
}
二:好将图片保存到一个XML文件
WinForm的资源文件中,将PictureBox的Image属性等非文字内容都转变成文本保存,这是通过序列化(Serialization)实现的,
例子:
using System.Runtime.Serialization.Formatters.Soap;
Stream stream = new FileStream("D://Image.xml",FileMode.Create,FileAccess.Write,FileShare.None);
SoapFormatter sf = new SoapFormatter();
Image img = Image.FromFile("D://Image.bmp");
sf.Serialize(stream,img);
stream.Close();
三:屏蔽CTRL-V
在WinForm中的TextBox控件没有办法屏蔽CTRL-V的剪贴板粘贴动作,如果需要一个输入框,但是不希望用户粘贴剪贴板的内容,可以改用RichTextBox控件,并且在KeyDown中屏蔽掉CTRL-V键,例子:
private void richTextBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.Control && e.KeyCode==Keys.V)
e.Handled = true;
}