2016-07-03 52 views
-2
public partial class Form1 : Form 
{ 
    public static string a = "a"; public static string b = "b"; public static string c = "c"; 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     textBox1.Text = a; 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     textBox1.Text = b; 
    } 

    private void button3_Click(object sender, EventArgs e) 
    { 
     textBox1.Text = c; 
    } 

    private void button4_Click(object sender, EventArgs e) 
    { 
     a = null; 
     b = null; 
     c = null; 
    } 
} 

我想爲聊天做一個簡單的鍵盤。
我用一個只有3個按鈕的小樣本程序啓動它;按鈕a,按鈕b,按鈕c分別用於a,b,c。
當我運行程序時,我按了按鈕a &,然後按b按鈕b(現在我想輸出ab形式),但它首先顯示一個然後按按鈕b它刪除a並顯示b。
我想製作更多按鈕來製作鍵盤。 基本上,我想打印存儲在順序按鍵的字母,但刪除了第一個,然後打印下一個..如何使按鈕輸入特定值?

+0

向我們顯示代碼。你還在開發什麼類型的應用程序? WPF,UWP,Windows Phone?我的猜測是你沒有將字符'b'連接到輸出字符串 – dimlucas

+0

什麼?也就是說,如果你設置了文字。你至少需要管理文本(追加等)... – icbytes

+0

dimlucas先生!我正在使用Visual Studio C#(Windows窗體應用程序) 我試圖上傳圖像或代碼 –

回答

2

創建一個屏幕上的鍵盤最簡單的方法是在特殊鍵來使用按鈕文本,除了像退格,回車,明確etc`。

private void KeyButton_Click(object sender, EventArgs e) 
{ 
    textBox1.Text += ((Button)sender).Text; 
} 

private void ClearButton_Click(object sender, EventArgs e) 
{ 
    textBox1.Text = string.Empty; 
} 

private void BackspaceButton_Click(object sender, EventArgs e) 
{ 
    textBox1.Text = textBox1.Text.SubString(0, textBox1.Text.Length-1); 
} 
+0

這是行不通的,如果這個人創建了按鈕修飾符 –

+0

@CallumLinington不知道你的意思是按鈕修改,但我用這個確切的方法來創建一個屏幕上的鍵盤,每個按鈕有兩個不同的文本(一個用於大寫鎖定關閉和一個大寫鎖定)。當然,我使用的按鈕是專門用於該鍵盤的用戶控件。 –

1

這是因爲您使用=操作擦除值。嘗試使用+=

textBox1.Text += c; 
textBox1.Text = textBox1.Text + c; 

你也可以從按鈕的Text屬性的文本值。 並且每個按鈕只有一個Button.Click事件處理程序。

private void button_Click(object sender, EventArgs e) 
{ 
    var button = sender as Button; 
    textBox1.Text = textBox1 + button.Text; 
} 
0

正如我告訴你的意見,你發佈你需要連接的代碼之前(又名追加)的字符: 這樣,所有的文字按鈕單擊事件可以用單一的方法來處理文本框。

如果你有一個名爲textBox1一個文本框這樣做:

textBox1.Text = 'a'

取代任何文字已經寫在與字符的文本框「A」

你需要什麼do是使用+=運營商:

textBox1.Text += a; 
textBox1.Text = b; 
textBox1.Text = c; 
+0

先生!非常感謝....... –

相關問題