[英]add 2 button when I'm clicking xamarin
I am trying to make a button that adds 2 to a number in a textbox every time I touch it. 我试图制作一个按钮,每次我触摸它时,会将2加到文本框中的数字。 I know how to add 1 like :
我知道如何添加1像:
int l;
t1.Text = l++.Tostring();
How can I add 2 when the button is touched? 触摸按钮时如何加2?
By "add 2 to textbox" i assume you mean: 通过“在文本框中添加2”,我假设您的意思是:
int l = 0;
t1.Text = (l + 2).ToString();
Edit: 编辑:
To make things clear - this will always set your TextBox
to 2. If you want to increment l
each time the button is clicked, you have to make l
a class level variable: 为了清楚
TextBox
,这总是将您的TextBox
设置为2。如果您想在每次单击按钮时增加l
,则必须使l
为类级别的变量:
public class SomeActivity()
{
private int l = 0;
private void OnButtonClicked()
{
l += 2
t1.Text = l.ToString();
}
}
What you're trying to do is the following: 您要执行的操作如下:
int l;
if (Int32.TryParse(t1.Text, out l))
{
t1.Text = (l+2).ToString();
}
This is a very basic question and it might cause some anger to post such questions. 这是一个非常基本的问题,可能会引起某些人发怒。 Therefore I would encourage you to read a book or tutorial on C# before posting similar questions.
因此,我建议您在发布类似问题之前阅读有关C#的书籍或教程。 Unfortunately I only know german books, but a search on Google should help you out.
不幸的是,我只知道德语书籍,但是在Google上进行搜索应该会对您有所帮助。
int count = 0; int count = 0;
buttonObject.Click += (sender, args) => { buttonObject.Click + =(发送方,参数)=> {
count += 2;
buttonObject.Text = count.ToString();
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.