你如何設定按鈕的標簽?
這是我的代碼:
private void InitializeMyButton()
{
// Create and initialize a Button.
Button button1 = new Button();
// Set the button to return a value of OK when clicked.
button1.DialogResult = DialogResult.None;
// Add the button to the form.
Controls.Add(button1);
}
希望有人能找到解決辦法。
uj5u.com熱心網友回復:
繼承自的類Control具有您可以為此設定的Text 屬性,而 aButton就是其中之一。一些控制元件(例如 Button 和 Label)的 Text 不是由用戶設定的,而是由開發人員設定的。其他 (TextBox) 是用戶可編輯的,您可以從 Text 屬性中讀取以了解用戶輸入的內容
一些控制元件并沒有真正合理地使用 Text 屬性(例如 DataGridView),但它們繼承自 Control,因此它們有一個(未使用)
在 C# 中,屬性就像一個變數:您可以通過將它放在 的左側來為它設定一個值=:
label.Text = "Hello";
你也可以從他們那里讀到:
MessageBox.Show("You typed " textbox.Text);
方法和性質不同;方法“做某事”;.Show以上是一種方法;它顯示了一個 MessageBox,您將一個字串作為引數傳遞給它,但它與屬性不同。
您不會通過呼叫(運行)它來設定屬性 -button.Text("My button label")如果按鈕具有 Text方法,則您在注釋中的嘗試會起作用,但Text按鈕上是屬性,而不是方法。
當您在大多數情況下查看 C# 時,您可以通過查看(其后是否有一個左括號來判斷它是屬性還是方法。方法也有動詞名稱,而屬性是名詞:
textbox.Clear();
textbox.Text = "hello";
textbox.AppendText("world");
uj5u.com熱心網友回復:
Button.Text:
private void InitializeMyButton()
{
// Create and initialize a Button.
Button button1 = new Button();
// Set the button to return a value of OK when clicked.
button1.DialogResult = DialogResult.None;
button1.Text = "OK";
// Add the button to the form.
Controls.Add(button1);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/381916.html
下一篇:使用javascript禁用按鈕
