Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在整数上加一个数字,而不是把它放在它旁边_C#_Winforms_Integer_Counter - Fatal编程技术网

C# 如何在整数上加一个数字,而不是把它放在它旁边

C# 如何在整数上加一个数字,而不是把它放在它旁边,c#,winforms,integer,counter,C#,Winforms,Integer,Counter,我正试图为我正在从事的一个社会项目编写一个计数器。我正在使用windows窗体。我用一个文本框来获取我的起始数字和一个开始和停止按钮 代码方面,我设法让它发送消息和一切,但我在计数方面遇到了问题。我得到的错误是不能将int转换成字符串,否则它将在已经存在的数字上添加一个数字,例如它发送的内容:1、11、111、1111。而不是1、2、3、4等等 这就是我搞砸的部分 textBox1.Text = textBox1.Text + 1; SendKey

我正试图为我正在从事的一个社会项目编写一个计数器。我正在使用windows窗体。我用一个文本框来获取我的起始数字和一个开始和停止按钮

代码方面,我设法让它发送消息和一切,但我在计数方面遇到了问题。我得到的错误是不能将int转换成字符串,否则它将在已经存在的数字上添加一个数字,例如它发送的内容:1、11、111、1111。而不是1、2、3、4等等

这就是我搞砸的部分

            textBox1.Text = textBox1.Text + 1;
            SendKeys.Send("{Enter}");
            Thread.Sleep(1000);
这是全部代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace Counting_Bot
{
    public partial class Form1 : Form
    {

        
        public Form1()
        {
            InitializeComponent();
        }

        public void timer1_Tick(object sender, EventArgs e)
        {
           
            SendKeys.Send(textBox1.Text);
            textBox1.Text = textBox1.Text + 1;
            SendKeys.Send("{Enter}");
            Thread.Sleep(1000);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Stop();
        }

        public void textBox1_TextChanged(object sender, EventArgs e)
        {


            if (System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "[^0-9]"))
            {
                MessageBox.Show("Please enter only numbers.");
                textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);
            }


        }
    }
}

很抱歉措辞和格式不好,但我是新来的。文本框中的内容是
字符串,而不是
int
。字符串的
+
-运算符的实现方式是将字符串连接到第一个字符串的末尾。所以
“1”+“1”
返回
“11”
,正如
“Hello”+“World”
返回
“Hello World”

您必须首先将文本转换为数字:

if(int.TryParse(textBox1.Text, out var number)
{
    textBox1.Text = (number + 1).ToString();
}

textBox1.Text
类型为
string
,因此
+
textBox1.Text
1
(隐式转换为
string

您需要将
textBox1.Text
解析为整数:

textBox1.Text = (int.Parse(textBox1.Text) + 1).ToString();
现在,
+
执行数学加法

您可能还希望验证输入,以确保输入了有效的整数:

if (int.TryParse(textBox1.Text, out int i))
{
    textBox1.Text = (i + 1).ToString();
}

当操作员编辑文本框时,他应该能够犯错误,例如键入字母,甚至删除所有内容,只要您确定操作员在完成时键入了一个数字,并按下按钮通知您他已完成

通常通过禁用此按钮让操作员知道输入不正确。如果按钮已启用,则您知道在按钮单击事件处理程序中,按钮中的文本是数字的文本表示形式

  • 订阅event TextBox.TextChanged以启用和禁用按钮
  • 订阅事件按钮。单击以处理键入的文本
通常使用visual studio designer执行以下操作:

TextBox textBox1 = new TextBox();
Button button1 = new Button();

textBox1.TextChanged += OnTextBoxTextChanged;
button1.Clicked += OnButtonClicked;
以您的形式:

int parsedInput = 0;
private void OnTextBoxChanged(object sender, ...)
{
    // get the input text
    string input = textBox1.Text;

    // try to parse the input text. If this can be done, remember the parsedInput
    // enable the button if the input is parsed correctly
    button1.Enabled = (Int32.TryParse(input, out parsedInput);
}

private void OnButtonClicked(object sender, ...)
{
    // Because the button is enabled, you know that parsedInput contains the parsed text
    this.ProcessInput(this.parsedInput);
}
private int GetInput()
{
    Int32.TryParse(input, out int i);

如果您确定输入是一个数字,那么(textBox1.Text=(Convert.ToInt32(textBox1.Text)+1.ToString();)您可以使用
NumericUpDown
只允许输入数字。@Sinatr不会神奇地将文本转换为
int
@HimBromBeere,它有
decimal
属性。我确信我的输入将是一个数字,因为我是唯一使用它的人,我做了一个验证系统,如果你查看最后一个空格,但再次使用你的设备,它仍然会是1,22,222,2222。@Probler这应该在
timer1\u Tick
方法中完成。如果您确定
textBox1.Text
始终是一个整数,那么继续使用第一个示例:)`public void timer1_Tick(object sender,EventArgs e){textBox1.Text=(int.Parse(textBox1.Text)+1.ToString();SendKeys.Send(textBox1.Text);textBox1.Text=textBox1.Text+1;SendKeys.Send(“{Enter}”);Thread.Sleep(1000);}`我在计时器滴答声中这样做了,但它导致了相同的问题