简体   繁体   English

C#Windows表单计算器-每次单击选择文本框

[英]C# Windows forms Calculator - Selecting textbox per click

First of all, I'm german, 2. here is a screenshot of my calculator.. kinda weird but well i know xD 首先,我是德国人,2。这是我的计算器的屏幕截图..有点奇怪,但我知道xD

我的UI

So, I'm looking for a Command: 因此,我正在寻找一个命令:

I got 4 Textboxes, the 1st one(textBox1) for the 1. Number, the 2. one(textBox2) for the Operator (+-*/), the 3. one for the 2. Number and the 4. one for the Result 我有4个文本框,第一个是1.数字(textBox1),数字是1. 2.(one-textBox2)是运算符(+-* /),3.是2.数字,而4.是。结果

if i do: 如果我做:

private void button3_Click(object sender, EventArgs e)
 {
  textBox1.SelectedText += "1";
  textBox3.SelectedText += "1";

 }

or 要么

private void button3_Click(object sender, EventArgs e)
 {
  textBox1.Text = textBox1.Text + "1";
  textBox3.Text = textBox3.Text + "1";
 }

the Number from the button I click on is shown in both textboxes now (1. and 3. one). 现在,两个文本框中都显示了我单击的按钮中的数字(1.和3.一个)。 But I want that the Number of the button i clicked on is going to be in the Textbox i selected first. 但我希望我单击的按钮的编号将在我首先选择的文本框中。 So i first do select the textbox1 or textbox3 with simply clicking in it and then i click on a button(for example 1) and then the number 1 is going to be in the textbox i selected/clicked. 因此,我首先只需单击一下即可选择textbox1或textbox3,然后单击一个按钮(例如1),然后数字1将出现在我选择/单击的文本框中。

On the _Activate Event for both Textboxes, store which box was activated. 在两个文本框的_Activate事件上,存储激活了哪个框。 Then use that in the _Clicked event: 然后在_Clicked事件中使用它:

private TextBox currentTextBox;

 // Both textboxes can use this function
private void textbox_Activate(object sender, EventArgs e)
{
        this.currentTextBox = (TextBox) sender;
}

private void button3_Click(object sender, EventArgs e)
{
         currentTextBox.Text = textBox1.Text + "1";
}

You can have a global variable TextBox textBoxSelected then in a event textBox1_Click and textBox3_Click set the variable. 您可以具有一个全局变量TextBox textBoxSelected,然后在事件textBox1_ClicktextBox3_Click中设置该变量。 Later in button3_Click choose the textboxselected and add your logic. 稍后在button3_Click中,选择选中的文本框并添加逻辑。 Try this: 尝试这个:

TextBox textBoxSelected;
private void txtBox1_Click(object sender, EventArgs e)
{
    textBoxSelected = textBox1;
}
private void txtBox3_Click(object sender, EventArgs e)
{
    textBoxSelected = textBox3;
}
private void button3_Click(object sender, EventArgs e)
{
    textBoxSelected.Text += "1";
}

I can't make much sense of your question, but I have noticed an issue in your logic. 我不太理解您的问题,但是我注意到您的逻辑上有一个问题。

C# will be adding them as strings, which results in concatenation. C#将它们添加为字符串,从而导致串联。

Convert the values to integers first and it will add correctly. 首先将值转换为整数,它将正确添加。

textBox1.Text = int.Parse(textBox1.Text) + 1;

As for you actual question, if you want to have a way of "remembering" what text box you clicked, add an event to the Click event to store the text box control that you have selected in a variable, and then use that in your logic. 至于您的实际问题,如果您想“记住”您单击了哪个文本框,请将一个事件添加到Click事件中,以存储您在变量中选择的文本框控件,然后在您的变量中使用它逻辑。

So here are some recos: 1/ Naming convention: Use clear names that refer to the button function or the textbox content. 因此,这里是一些解决方案:1 /命名约定:使用明确的名称来引用按钮功能或文本框内容。 Say for example: TextboxLeftOperand, ButtonN1, TextboxOperator, ... 2/ Use a new variable called SelectedTextbox var SelectedTextbox = new Textbox(); 例如说:TextboxLeftOperand,ButtonN1,TextboxOperator,... 2 /使用一个名为SelectedTextbox的新变量var SelectedTextbox = new Textbox();。 3/ Add to the click event of both textboxes an assignment of the SelectedTextbox. 3 /将两个选定文本框的分配添加到两个文本框的click事件中。 For the left textbox: SelectedTextbox = TextboxLeftOperand // textbox1 in your case And for the right textbox SelectedTextbox = TextboxRightOperand // textbox1 in your case 4. Now all you have is work with your new variable. 对于左边的文本框:SelectedTextbox = TextboxLeftOperand //文本框1对于右边的文本框SelectedTextbox = TextboxRightOperand //文本框1。现在,您所拥有的就是使用新变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM