EventCollection
2023.07.01 2 0
staticreadonlyobjectmouseDownEventKey=newobject();staticreadonlyobjectmouseUpEventKey=newobject();//Define the MouseDown event property.publiceventMouseEventHandler MouseDown {//Add the input delegate to the collection....
文章目录:

一、C#如何使用事件属性处理多个事件
要使用事件属性(Visual
Basic

2005
中的自定义事件),请在引发事件的类中定义事件属性,然后在处理事件的类中设置事件属性的委托。要在一个类中实现多个事件属性,该类必须在内部存储和维护为每个事件定义的委托。一种典型方法是实现通过事件键进行索引的委托集合。

要存储每个事件的委托,可以使用
EventHandlerList
类或实现您自己的集合。集合类必须提供用于基于事件键设置、访问和检索事件处理程序委托的方法。例如,可以使用Hashtable类或从DictionaryBase
类派生一个自定义类。不需要在类以外公开委托集合的实现详细信息。
类中的每个事件属性定义一个
add
访问器方法和一个
remove
访问器方法。事件属性的
add
访问器将输入委托实例添加到委托集合。事件属性的
remove
访问器从委托集合中移除输入委托实例。事件属性访问器使用事件属性的预定义键在委托集合中添加和从委托集合中移除实例。
使用事件属性处理多个事件
在引发事件的类中定义一个委托集合。
定义每个事件的键。
在引发事件的类中定义事件属性。
使用委托集合实现事件属性的
add
访问器方法和
remove
访问器方法。
使用公共事件属性可在处理事件的类中添加和移除事件处理程序委托。
示例下面的
C#
示例实现事件属性MouseDown和MouseUp
,并使用EventHandlerList
存储每个事件的委托。事件属性构造的关键字用粗体表示。
注意Visual
Basic
2005
中不支持事件属性。//The
class
SampleControl
defines
two
event
properties,
MouseUp
and
MouseDown.
classSampleControl:
Component
{//://Define
other
control
methods
and
properties.//://Define
the
delegate
collection.
protectedEventHandlerList
listEventDelegates=newEventHandlerList();//Define
a
unique
key
for
each
event.
staticreadonlyobjectmouseDownEventKey=newobject();staticreadonlyobjectmouseUpEventKey=newobject();//Define
the
MouseDown
event
property.
publiceventMouseEventHandler
MouseDown
{//Add
the
input
delegate
to
the
collection.
add
{
listEventDelegates.AddHandler(mouseDownEventKey,
value);
}//Remove
the
input
delegate
from
the
collection.
remove
{
listEventDelegates.RemoveHandler(mouseDownEventKey,
value);
}}//Define
the
MouseUp
event
property.
publiceventMouseEventHandler
MouseUp
{//Add
the
input
delegate
to
the
collection.
二、"SqlParameterCollection 仅接受非空的 SqlParameter 类型对象,不接受...
SqlParameter("@username",SqlDbType.VarChar,50).Value=user.UserName);
第一陵枝个迅羡参数不需要加"尺昌敏@"
SqlParameterCollection 对象组,只接受sqlParameter类型的对象,其他燃歼类型的全部不认识。所以高段轮不要想传其他的参数在里面了。sqlParameter的add方法,就是添加对象的。以内参数都是戚信sqlParameter类型的。
少定义了一个 UserId
就按你前面那样写就行了,加上去
cmd.Parameters.Add("@UserId ",SqlDbType.VarChar,50);
还有一个错误是:
引号的问题:
cmd.Parameters.Add("@UserEmail,SqlDbType.VarChar,50");
应是笑运:
cmd.Parameters.Add("@UserEmail",SqlDbType.VarChar,50);
最后你的数据库是不是就是这几个字段呢?
如果不完全符合,应该写成
UserId=@UserId ,----
我自己有一个,你可以参缺皮考一下:
string sql1 = "insert into TA1(xm,lv) values(@xm,@lv)" ;
System.Data.SqlClient.SqlParameter[] sqlparam = new System.Data.SqlClient.SqlParameter[2];
sqlparam[0] = new System.Data.SqlClient.SqlParameter("@xm",System.Data.SqlDbType.VarChar,50);
sqlparam[0].Value = "qimin1029";
sqlparam[1] = new System.Data.SqlClient.SqlParameter("@lv",System.Data.SqlDbType.Decimal);
sqlparam[1].Value = "0.25";
int dd = 0;
string erradssdf = this.ExecSql_EffectRowCount(sql1,sqlparam,System.Data.CommandType.Text,ref dd);
public string ExecSql_EffectRowCount(string sql, System.Data.SqlClient.SqlParameter[] sqlParams, System.Data.CommandType cmdType, ref int effectRowCount)
{
string res = "";
System.Data.SqlClient.SqlConnection cnn = new System.Data.SqlClient.SqlConnection(this.constring);
try
{
cnn.Open();
System.Data.SqlClient.SqlTransaction trans = cnn.BeginTransaction();
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sql, cnn);
cmd.Transaction = trans;
cmd.CommandType = cmdType;
try
{
if(sqlParams != null)
{
foreach(System.Data.SqlClient.SqlParameter p in sqlParams)
{
cmd.Parameters.Add(p);
}
}
effectRowCount = cmd.ExecuteNonQuery();
trans.Commit();
}
catch(Exception e1)
{
trans.Rollback();//回碰扮梁滚事务
res = e1.Message;
}
}
catch(Exception e)
{
res = e.Message;
}
finally
{
cnn.Close();
cnn.Dispose();
}
return res;
}
以上对于EventCollection的问题的2点解答,由涂画吧(tuhuaba.com)小编收集与互联网,希望对大家有用。
免责声明:本网信息来自于互联网,目的在于传递更多信息,并不代表本网赞同其观点。其内容真实性、完整性不作任何保证或承诺。如若本网有任何内容侵犯您的权益,请及时联系我们,本站将会在24小时内处理完毕。