' 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
新聞熱點
疑難解答
圖片精選