2017-10-18 22 views
1
Label[] l1 = new Label[30];  
DataTable dt = ConsoleApp2.CitiesDB.getCities(this.region); 
       foreach(DataRow row in dt.Rows) 
       { 
        count++; 
        string city = row.Field<string>("city"); 
        l1[count] = new Label(); 
        l1[count].Location = new System.Drawing.Point(x,y); 
        l1[count].Size = new Size(140, 80); 
        l1[count].Font = new System.Drawing.Font("Century Gothic", 8.5F); 
        l1[count].Text = city; 
        x = x + 260; 
        this.Controls.Add(l1[count]); 
        this.Refresh(); 
        if(count == 4 || count %4 ==0) 
        { 
         y = y + 150; 
         x = 40; 
        } 
       //l1[count].Click += new EventHandler(l1_click); 
      } 

私は動的ラベルを作成しました(各ラベルは都市名です)。どのように各ラベルをクリック可能にすることができますか?私は登録フォームを持っています - ユーザーが "ニューヨーク"をクリックして "city"のテキストボックスに表示されたらそれが表示されます。 EventHandlerとのやり方は私にとってはうまくいかない);私に何ができる?C#動的クリックイベントを追加

protected void l1_click(object sender, EventArgs e) 
    { 
     RegisterForm register = new RegisterForm(username,email,password,address,phone); 
     register.state.Text = region; 
     register.city.Text = l1[count].Text; 
     register.Show(); 
    } 

おかげで(::各ラベルのための

答えて

1

割り当てワンクリックイベント: 私はコード内で何を意味するか仕事doesntのこと

l1[count].Click += l1_click; 

クリックイベントでは、見て、送信者の引数を使用していますラベルをクリックしました:

protected void l1_click(object sender, EventArgs e) 
{ 
    Label lbl = (Label)sender; 
    MessageBox.Show(lbl.Text); 
+0

ありがとうございました、それは働いています( – Daniel

+0

)いくつかの点についてnswer –

関連する問題