如何编一个通用的类或者对象在多个解决方案中调用??? 在新增的工程里新增一個類如新增一個Class,在類里寫你需要的方法、屬性,然后你在其它的窗體或類里調用這個類的方法不就可以了嗎,調用時用Class.方法,如果名字空間同那你還要先引用名字空間 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我编的一个login control 供您参考。using System;using System.Collections;using System.ComponentModel;using System.Drawing;using System.Data;using System.Windows.Forms;namespace LoginControlApp{ /// <summary> /// Summary description for LoginControl. /// </summary> public class LoginControl : System.Windows.Forms.UserControl { private System.Windows.Forms.Button login; private System.Windows.Forms.Button cancel; private System.Windows.Forms.Label nameLabel; private System.Windows.Forms.TextBox password; private System.Windows.Forms.Label passwordLabel; private System.Windows.Forms.TextBox name; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public LoginControl() { // This call is required by the Windows.Forms Form Designer. InitializeComponent(); // TODO: Add any initialization after the InitializeComponent call } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Component Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.login = new System.Windows.Forms.Button(); this.cancel = new System.Windows.Forms.Button(); this.nameLabel = new System.Windows.Forms.Label(); this.name = new System.Windows.Forms.TextBox(); this.password = new System.Windows.Forms.TextBox(); this.passwordLabel = new System.Windows.Forms.Label(); this.SuspendLayout(); // // login // this.login.Location = new System.Drawing.Point(168, 112); this.login.Name = "login"; this.login.Size = new System.Drawing.Size(96, 24); this.login.TabIndex = 0; this.login.Text = "Login"; this.login.Click += new System.EventHandler(this.login_Click); // // cancel // this.cancel.Location = new System.Drawing.Point(40, 112); this.cancel.Name = "cancel"; this.cancel.Size = new System.Drawing.Size(96, 24); this.cancel.TabIndex = 1; this.cancel.Text = "Cancel"; // // nameLabel // this.nameLabel.Location = new System.Drawing.Point(56, 24); this.nameLabel.Name = "nameLabel"; this.nameLabel.Size = new System.Drawing.Size(40, 16); this.nameLabel.TabIndex = 2; this.nameLabel.Text = "Name:"; // // name // this.name.Location = new System.Drawing.Point(120, 24); this.name.Name = "name"; this.name.Size = new System.Drawing.Size(120, 20); this.name.TabIndex = 3; this.name.Text = ""; // // password // this.password.Location = new System.Drawing.Point(120, 64); this.password.Name = "password"; this.password.PasswordChar = '*'; this.password.Size = new System.Drawing.Size(120, 20); this.password.TabIndex = 4; this.password.Text = ""; // // passwordLabel // this.passwordLabel.Location = new System.Drawing.Point(40, 72); this.passwordLabel.Name = "passwordLabel"; this.passwordLabel.Size = new System.Drawing.Size(64, 16); this.passwordLabel.TabIndex = 5; this.passwordLabel.Text = "Password:"; // // LoginControl // this.Controls.Add(this.passwordLabel); this.Controls.Add(this.password); this.Controls.Add(this.name); this.Controls.Add(this.nameLabel); this.Controls.Add(this.cancel); this.Controls.Add(this.login); this.Name = "LoginControl"; this.Size = new System.Drawing.Size(312, 150); this.ResumeLayout(false); } #endregion private void login_Click(object sender, System.EventArgs e) { this.OnValidityCheck(); } public delegate void ValidityCheckEventHandler(object sender,ValidityCheckEventArgs e); public class ValidityCheckEventArgs { public ValidityCheckEventArgs(string name,string password) { this._name=name; this._password=password; } private string _name=null; private string _password=null; private bool _result=false; private string _message=null; public string Name { get{return this._name;} } public string Password { get{return this._password;} } public bool Result { get{return this._result;} set{this._result=value;} } public string Message { get{return this._message;} set{this._message=value;} } } public event ValidityCheckEventHandler ValidityCheck=null; protected virtual void OnValidityCheck() { if(this.ValidityCheck!=null) { ValidityCheckEventArgs e=new ValidityCheckEventArgs(this.name.Text,this.password.Text); this.ValidityCheck(this,e); if(e.Result==true) { if(this.Parent is Form) (this.Parent as Form).Close(); } else { MessageBox.Show(e.Message); } } } }} 很简单,不用那么负载,吓人啊.先写个类,做成DLL,然后在你的WINFORM中USING NAMESPACE,就可以使用了啊. 楼上几位说的都没错,方法确实挺多的,比较常用的应该算dll吧 Update 无法找到 TableMapping['a'] 或 DataTable“a” C#操作IIS设置了站点的应用程序名但后面的按钮依然是创建 线程问题。 求一个绘图算法 求救:GDI+ 中发生一般性错误。 [DllImport("User32.dll", CharSet = CharSet.Auto)]啥意思 关闭窗体 c#简单问题,老鸟一眼搞定,进来看看吧! IIS网站URL如何重定向? 散分,终于看完了Thinking in C++(E2V1) 一个精华贴!服务器端控件与客户端脚本之间的数据传递问题。 水晶报表的问题(不好意思!)
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;namespace LoginControlApp
{
/// <summary>
/// Summary description for LoginControl.
/// </summary>
public class LoginControl : System.Windows.Forms.UserControl
{
private System.Windows.Forms.Button login;
private System.Windows.Forms.Button cancel;
private System.Windows.Forms.Label nameLabel;
private System.Windows.Forms.TextBox password;
private System.Windows.Forms.Label passwordLabel;
private System.Windows.Forms.TextBox name;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null; public LoginControl()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent(); // TODO: Add any initialization after the InitializeComponent call } /// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.login = new System.Windows.Forms.Button();
this.cancel = new System.Windows.Forms.Button();
this.nameLabel = new System.Windows.Forms.Label();
this.name = new System.Windows.Forms.TextBox();
this.password = new System.Windows.Forms.TextBox();
this.passwordLabel = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// login
//
this.login.Location = new System.Drawing.Point(168, 112);
this.login.Name = "login";
this.login.Size = new System.Drawing.Size(96, 24);
this.login.TabIndex = 0;
this.login.Text = "Login";
this.login.Click += new System.EventHandler(this.login_Click);
//
// cancel
//
this.cancel.Location = new System.Drawing.Point(40, 112);
this.cancel.Name = "cancel";
this.cancel.Size = new System.Drawing.Size(96, 24);
this.cancel.TabIndex = 1;
this.cancel.Text = "Cancel";
//
// nameLabel
//
this.nameLabel.Location = new System.Drawing.Point(56, 24);
this.nameLabel.Name = "nameLabel";
this.nameLabel.Size = new System.Drawing.Size(40, 16);
this.nameLabel.TabIndex = 2;
this.nameLabel.Text = "Name:";
//
// name
//
this.name.Location = new System.Drawing.Point(120, 24);
this.name.Name = "name";
this.name.Size = new System.Drawing.Size(120, 20);
this.name.TabIndex = 3;
this.name.Text = "";
//
// password
//
this.password.Location = new System.Drawing.Point(120, 64);
this.password.Name = "password";
this.password.PasswordChar = '*';
this.password.Size = new System.Drawing.Size(120, 20);
this.password.TabIndex = 4;
this.password.Text = "";
//
// passwordLabel
//
this.passwordLabel.Location = new System.Drawing.Point(40, 72);
this.passwordLabel.Name = "passwordLabel";
this.passwordLabel.Size = new System.Drawing.Size(64, 16);
this.passwordLabel.TabIndex = 5;
this.passwordLabel.Text = "Password:";
//
// LoginControl
//
this.Controls.Add(this.passwordLabel);
this.Controls.Add(this.password);
this.Controls.Add(this.name);
this.Controls.Add(this.nameLabel);
this.Controls.Add(this.cancel);
this.Controls.Add(this.login);
this.Name = "LoginControl";
this.Size = new System.Drawing.Size(312, 150);
this.ResumeLayout(false); }
#endregion private void login_Click(object sender, System.EventArgs e)
{
this.OnValidityCheck();
} public delegate void ValidityCheckEventHandler(object sender,ValidityCheckEventArgs e);
public class ValidityCheckEventArgs
{
public ValidityCheckEventArgs(string name,string password)
{
this._name=name;
this._password=password;
} private string _name=null;
private string _password=null;
private bool _result=false;
private string _message=null; public string Name
{
get{return this._name;}
}
public string Password
{
get{return this._password;}
}
public bool Result
{
get{return this._result;}
set{this._result=value;}
}
public string Message
{
get{return this._message;}
set{this._message=value;}
}
} public event ValidityCheckEventHandler ValidityCheck=null; protected virtual void OnValidityCheck()
{
if(this.ValidityCheck!=null)
{
ValidityCheckEventArgs e=new ValidityCheckEventArgs(this.name.Text,this.password.Text);
this.ValidityCheck(this,e);
if(e.Result==true)
{
if(this.Parent is Form)
(this.Parent as Form).Close();
}
else
{
MessageBox.Show(e.Message);
}
}
}
}
}
先写个类,做成DLL,
然后在你的WINFORM中USING NAMESPACE,
就可以使用了啊.