2010-06-14 49 views
2

我有一個問題,我有一些動態button.and我想存儲整數值。如何在運行時在按鈕中存儲值?

,並獲得該按鈕的那個點擊事件值我怎麼能實現它提前 沙善

DataView dv = new DataView(dtCat, "PK_CATEGORY_ID IN(" + categoryIds.ToString() + "0)", "PK_CATEGORY_ID", DataViewRowState.CurrentRows); 

foreach (DataRowView rr in dv) 
{ 
    //MessageBox.Show(rr[0].ToString() + "------------" + rr[1].ToString()); 

    Button b2 = new Button(); 

    //b2.Name = rr[0].ToString(); 
    b2.Name = ""; 
    b2.Height = 200; 
    b2.Width = 200; 
    b2.Margin = new Thickness(0, -100, 0, 0); 
    b2.HorizontalAlignment = HorizontalAlignment.Left; 
    b2.Content = rr[1].ToString(); 
    b2.Background = System.Windows.Media.Brushes.Pink; 
    b2.Click += new RoutedEventHandler(b2_Click); 
    btncanvas.Children.Add(b2); 
    Canvas.SetLeft(b2, b2.Width * i); 
    i = i + 1; 
    MessageBox.Show(rr[0].ToString()); 
    b2.Tag = rr[0].ToString(); 

} 


void b2_Click(object sender, RoutedEventArgs e) 
{ 
    Button clicked = (Button)sender; 

    categoryname = clicked.Name; 
} 

回答

2

Tag財產

感謝可能是你想要的東西。

你已經在使用它在你的榜樣,但只是有:

b2.Tag = integerValue; 

然後在點擊處理程序使用Convert.ToInt32(object)方法獲取整數值回:

int retrievedValue = Convert.ToInt32(clicked.Tag); 
+0

但其給出錯誤that..Specified轉換是無效的。 – Shashank 2010-06-14 11:08:42

+0

thk它工作 – Shashank 2010-06-14 11:13:57

+0

@SHASHANK - 我已經糾正了代碼。我打字時沒有訪問Visual Studio;) – ChrisF 2010-06-14 11:14:06

相關問題