' visual basic 伪代码mycontrol(myindex).myproperty = myvaluemycontrol(myindex + 1).mymethod// c# 伪代码mycontrol[myindex].myproperty = myvalue;mycontrol[myindex + 1].mymethod
' visual basic 伪代码private sub mycontrol_click(sender as object, e as eventargs)   messagebox.show("您已单击 mycontrol 编号" & _      mycontrol.index)end sub// c# 伪代码private void mycontrol_click(system.object sender, system.eventargs e)   {      messagebox.show("您已单击 mycontrol 编号" +         mycontrol.index);   }' visual basic 伪代码dim i as integerfor i = 1 to 5   ' 插入代码以创建控件并为属性分配值。next i// c# 伪代码for (int i = 1; i < 6; i++){   // 插入代码以创建控件并为属性分配值。 }' visual basicpublic class buttonarray   inherits system.collections.collectionbaseend class// c#public class buttonarray : system.collections.collectionbase{   // 省略了由设计器添加的代码。}' visual basicprivate readonly hostform as system.windows.forms.form// c#private readonly system.windows.forms.form hostform;
public sub addnewbutton()   ' 创建 button 类的新实例。   dim abutton as new system.windows.forms.button()   ' 将按钮添加到集合的内部列表。   me.list.add(abutton)   ' 将按钮添加到由 hostform 字段    ' 引用的窗体的控件集合中。   hostform.controls.add(abutton)   ' 设置按钮对象的初始属性。   abutton.top = count * 25   abutton.left = 100   abutton.tag = me.count   abutton.text = "按钮 " & me.count.tostringend sub// c# public void addnewbutton(){   // 创建 button 类的新实例。   system.windows.forms.button abutton = new       system.windows.forms.button();   // 将按钮添加到集合的内部列表。   this.list.add(abutton);   // 将按钮添加到由 hostform 字段    // 引用的窗体的控件集合中。   hostform.controls.add(abutton);   // 设置按钮对象的初始属性。   abutton.top = count * 25;   abutton.left = 100;   abutton.tag = this.count;   abutton.text = "按钮 " + this.count.tostring();}' visual basicpublic sub new(byval host as system.windows.forms.form)   hostform = host   me.addnewbutton()end sub// c# // 使用此构造函数替换默认的构造函数。public buttonarray(system.windows.forms.form host){   hostform = host;   this.addnewbutton();}' visual basicdefault public readonly property item(byval index as integer) as _   system.windows.forms.button   get      return ctype(me.list.item(index), system.windows.forms.button)   end getend property// c#public system.windows.forms.button this [int index]{get   {      return (system.windows.forms.button) this.list[index];   }}' visual basicpublic sub remove()   ' 检查以确保存在要删除的按钮。   if me.count > 0 then      ' 从宿主窗体控件集合中删除添加到数组      ' 的最后一个按钮。请注意在访问数组时      ' 默认属性的使用。      hostform.controls.remove(me(me.count -1))      me.list.removeat(me.count -1)   end ifend sub// c#public void remove()}   // 检查以确保存在要删除的按钮。   if (this.count > 0)   {      // 从宿主窗体控件集合中删除添加到数组      // 的最后一个按钮。请注意在访问数组时      // 索引的使用。      hostform.controls.remove(this[this.count -1]);      this.list.removeat(this.count -1);   }}' visual basicpublic sub clickhandler(byval sender as object, byval e as _   system.eventargs)   messagebox.show("您已单击按钮 " & ctype(ctype(sender, _      button).tag, string))end sub// c#public void clickhandler(object sender, system.eventargs e){   system.windows.forms.messagebox.show("您已单击按钮 " +       (string)((system.windows.forms.button) sender).tag);}' visual basicaddhandler abutton.click, addressof clickhandler// c#abutton.click += new system.eventhandler(clickhandler);
' visual basic' 声明新的 buttonarray 对象。dim mycontrolarray as buttonarray// c#// 声明新的 buttonarray 对象。buttonarray mycontrolarray;
' visual basicmycontrolarray = new buttonarray(me)// c#mycontrolarray = new buttonarray(this);
' visual basic' 调用 mycontrolarray 的 addnewbutton 方法。mycontrolarray.addnewbutton()' 更改按钮 0 的 backcolor 属性。mycontrolarray(0).backcolor = system.drawing.color.red// c#// 调用 mycontrolarray 的 addnewbutton 方法。mycontrolarray.addnewbutton();// 更改按钮 0 的 backcolor 属性。mycontrolarray[0].backcolor = system.drawing.color.red;
' visual basic' 调用 mycontrolarray 的 remove 方法。mycontrolarray.remove()// c#// 调用 mycontrolarray 的 remove 方法。mycontrolarray.remove();
mycontrolarray(0).backcolor = system.drawing.color.red
新闻热点
疑难解答
图片精选