简体   繁体   English

在C#中我想将变量从一个button1传递给另一个button2

[英]in c# i want to pass a variable from one button1 to other button2

I was trying to add 2 numbers by using a single text box (both the input and output must be specified in a single textbox).when I click on '+' button the data on the textbox1 should arise and and should enable the user to type a new number, those 2 numbers should be added and should be displayed when an '=' button is clicked 我试图使用单个文本框添加2个数字(输入和输出都必须在单个文本框中指定)。当我单击“ +”按钮时,textbox1上的数据应该出现并且应该使用户能够输入一个新数字,这两个数字应添加并在单击“ =”按钮时显示

so my problem is: 所以我的问题是:

if suppose if a button1 is clicked then a variable stored the value of that button1 and only displays that value when button2 is clicked 如果假设如果单击了button1,则变量将存储该button1的值,并且仅在单击button2时显示该值

please help me in finding out 请帮助我找出答案

If you have a value in a text field, when button 1 is clicked, the value from the text field must be extracted from the text field and saved somewhere. 如果文本字段中有值,则单击按钮1时,必须从文本字段中提取文本字段中的值并将其保存在某处。 This is done in the click event handler for the button. 这是在按钮的click事件处理程序中完成的。

Depending on what type of a program you are working in, the place you save the info may be different. 根据您正在使用的程序类型,保存信息的位置可能会有所不同。 You may save this in a temporary variable, a database, the session, hidden field, or somewhere else, it just needs to be saved. 您可以将其保存在临时变量,数据库,会话,隐藏字段或其他地方,只需保存即可。

When button 2 is clicked, extract the value in the same way and save it somewhere. 单击按钮2时,以相同的方式提取值并将其保存在某处。 If you have two values in the designated saved locations when you click the '=' button, use these values, add them together, and populate the text box with the result. 如果在单击“ =”按钮时在指定的保存位置中有两个值,请使用这些值,将它们加在一起,然后在结果中填充文本框。

You need just a variable to store first number.Define it in the class level: 您只需要一个变量来存储第一个数字,请在类级别对其进行定义:

int firstNumber;

Then when you get your number from textBox1 store it in the firstNumber,for example in button one (+) click: 然后,当您从textBox1中获取数字时,将其存储在firstNumber中,例如在按钮一(+)中,单击:

int temp;
if(int.TryParse(textBox1.Text, out temp)
{  
   firtNumber = temp;
   textBox1.Clear(); // or set visible or enabled to false
}

In (=) button: (=)按钮中:

int temp;
if(int.TryParse(textBox2.Text, out temp)
{  
   label1.Text = String.Format("Result of {0} + {1} is : {2}",firstNumber, temp, firstnumber+temp);
}

Ok, so one textbox that the user inputs a value into. 好的,因此用户可以在其中输入一个值的textbox One they use the + button the number disappears and they are able to put a second number in the textbox . 他们使用+ button ,数字消失,他们能够在textbox输入第二个数字。 The problem is you wish to store the value in the textbox prior to clearing it. 问题是您希望在清除值之前将其存储在textbox This is easy, and should be handled in the Event Handler for the + button_click . 这很容易,应该在Event Handler中使用+ button_click进行处理。

private int value1;
private int value2;
private int total;

private void addButton_Click(object sender, EventArgs e)
{
    int.TryParse(textbox1.Text, out value1);
}

private void equalButton_Click(object sender, EventArgs e)
{
    int.TryParse(textbox1.Text, out value2);
    total = value1 + value2;
    textbox1.Text = total.ToString();
}

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

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