如何连续加数字并获得C#的总数 [英] How do I add numbers continuesly and get a total in C#
问题描述
您好,我想在c#中创建一个日常程序(在Visual Studio 2017中),它应该像这样:
有一个文本框-标签-"BTN",其底部是Photoshop之类的名称,因此标签首先显示0(小时),然后当您向文本框添加数字时显示"2:30"分钟,然后点击"BTN",标签应显示2:30(小时).另一件事是我希望它与垂直进度条同步(我知道如何制作),但我不知道如何与进度条同步,并且我希望它将所有内容都保存在SQL数据库中我加一个数字的时间.我知道这很多(也许对我而言,因为我是初学者),但是如果您知道该怎么做,请帮助我.谢谢.
我尝试过的事情:
我在Google和YouTube上做了很多研究,这就是我所得到的(我首先做了一个计算器,但这不是我想要的):
hello, I want to create a daily program in c# ( in visual studio 2017 ), it should be like this :
there is a textbox - label - ''BTN'' and at the bottom of those is a name of something like Photoshop and so the label at first shows 0 ( hours ) and then when u add a number to textbox like "2:30" minutes and hit the ''BTN'' the label should show 2:30 (hours). and another thing is that I want it to sync with a vertical progress bar ( I know how to make this one ) but I don''t know how to sync it with progress bar and I want it to save everything in a SQL database every time that I add a number. I know this is a lot ( maybe for me cause I''m a beginner ) but please if u know how to make it, help me. thank you.
What I have tried:
I did a lot of research in google and YouTube and this Is what I got ( I made a calculator first but it wasn''t what I want):
public Form1()
{
InitializeComponent();
}
int a = 0;
private void button1_Click(object sender, EventArgs e)
{
a++;
label1.Text = a.ToString();
}
我想要这样的东西,但与其计算点击次数,不如添加我在文本框中输入的数字并将其与进度栏和标签同步.
I want something like this but instead of counting my clicks it should add the numbers that I type in textbox and sync it with the progress bar and the label.
推荐答案
首先要做的是将文本框中的数字转换为可以使用的数字格式.为此,请使用TryParse:
The first thing to do is start by converting the number in the textbox into a numeric format that you can work with. For that, use TryParse:
double val;
if (!double.TryParse(myTextBox.Text, out val))
{
// It's not a valid floting point number - report a problem to the user instead of continuing.
...
return;
}
现在,您可以使用val
中的值求和.
您可以通过设置Bars的Value属性来将其与进度条同步"-通过设置Maximum和Minimum属性来覆盖整个范围,因此您的值将介于其中.
保存到数据库更加复杂-如果您还不能处理ProgressBar,那么我将把它保留一会儿并考虑使用更简单的东西,例如设置文件: ^ ]
Now you can use the value in val
to add up your total.
You "sync" it with the progress bar by setting the bars Value property - by setting the Maximum and Minimum properties to cover the whole range, so your values fit between those.
Saving to a DB is more complex - if you can''t cope with a ProgressBar yet, then I''d leave that for a while and consider using something simpler, like a settings file: Using Settings in C#[^]
在此处查看示例: [进度栏]和此处: [ SQL客户端]
See examples here: [progressbar] and here: [SQL Client]
这篇关于如何连续加数字并获得C#的总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!