【问题标题】:How to add a number to an int thats and not place it next to it [duplicate]如何将一个数字添加到一个整数而不是将它放在它旁边[重复]
【发布时间】:2020-12-20 11:03:53
【问题描述】:

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

代码方面,我设法让它发送消息和所有内容,但我在计数部分遇到了问题。我得到 cant convert int into a string 的错误,否则它会将一个数字添加到它发送的现有数字示例中: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);
            }


        }
    }
}

我很抱歉措辞和格式不好,但我是新来的 ¬.¬

【问题讨论】:

  • 如果您确定输入将是一个数字,那么 ( textBox1.Text = (Convert.ToInt32(textBox1.Text) + 1).ToString(); )
  • 您可以使用NumericUpDown 只允许输入数字。
  • @Sinatr 不会神奇地将文本转换为int
  • @HimBromBeere,它有 decimal Value 属性。

标签: c# winforms integer counter


【解决方案1】:

操作员在编辑文本框时,应该可以出错,比如输入字母,甚至删除所有内容,只要您确定操作员在完成时输入了一个数字并按下按钮进行通知你说他完蛋了。

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

  • 订阅事件 TextBox.TextChanged 以启用和禁用按钮
  • 订阅事件 Button.Clicked 以处理键入的文本

以下通常使用 Visual Studio 设计器完成:

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);

【讨论】:

    【解决方案2】:

    textBox1.Textstring 类型,因此+ 连接textBox1.Text1(隐式转换为string)。

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

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

    现在+ 执行数学加法。

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

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

    【讨论】:

    • 我确定我的输入将是一个数字,因为我是唯一使用这个的人并且我做了一个验证系统如果你查看最后一个 void 但是再次使用您的建议,它仍然只是 1、22、222、2222。
    • @Probler 这应该在您的timer1_Tick 方法中完成。如果您确定 textBox1.Text 将始终是整数,请继续使用第一个示例 :)
    • ` public void timer1_Tick(object sender, EventArgs e) { textBox1.Text = (int.Parse(textBox1.Text) + 1).ToString(); SendKeys.Send(textBox1.Text);文本框1.文本 = 文本框1.文本 + 1; SendKeys.Send("{Enter}");线程.睡眠(1000); }` 我在计时器滴答声中这样做了,但它导致了同样的问题
    【解决方案3】:

    文本框中的内容是string,而不是int。用于字符串的+-运算符的实现方式是将字符串连接到第一个字符串的末尾。所以"1" + "1" 返回"11",正如"Hello " + "World" 返回"Hello World"

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-05
      • 2021-10-03
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 2022-11-14
      相关资源
      最近更新 更多