Click here to Skip to main content
15,916,462 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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.
Posted
Updated 29-Jul-17 1:44am
v2
Comments
BillWoodruff 29-Jul-17 12:30pm    
I do not mean to discourage you, but I think you need to first spend more time studying C# fundamentals, and taking on simpler problems ... so that you build a good foundation for the future.

Need a good book : ask me.
Johny2018 29-Jul-17 13:30pm    
yes I have to spend more time on fundementals but u know cause i'm just starting I have a lot of good ideas ( but I can't make any of them ). I just needed this one its critical for me to create this program after this i'm gonna master the fundementals :)
BillWoodruff 29-Jul-17 19:37pm    
It's your choice.

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:
C#
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;
   }
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#[^]
 
Share this answer
 
Comments
Johny2018 29-Jul-17 8:58am    
I read your answer about 10 times and I searched how to add value in total there were some codes but non of them worked. so eventually this is what I have but it's not working :
//int val = 0;

//total = int.Parse(textBox1.Text) + int.Parse(label.Text);


double val;
if (!double.TryParse(textBox1.Text, out val))
{

val = Convert.ToInt32(this.textBox1.Text);
int total = val + label1.Text; // I get error here it says cant implicitly convert type string to int !!!!
}

OriginalGriff 29-Jul-17 10:04am    
Look at the code I wrote...not what you imagine I wrote. Pay attention to comments.
If you don't understand the code, look at eth documentation of TryParse - it's all pretty clear.
If you want to enter integers, use int.TryParse instead of double.TryParse.
Johny2018 29-Jul-17 10:45am    
ok, so I understand that tryparse is for converting string to int, but i can't understand why u used double ?! and another thing is that i can't find a way to add a number into label. all i have is a form with lbl, btn, txtbox, and in the code area right now its just the code that u gave me (thx). if the first number in label is 0 how can i add another number to it ?! i mean if i add 1 lbl should show me 1 then if i add 2 it should show me 3 and if i add 2 again it should show 5, but all my program does is that show me whatever it's in the txtbox :(
OriginalGriff 29-Jul-17 11:09am    
Because I assumed you would be using hours.minutes
If you use an int, then all you can get the user to type is a number of minutes, which may not be convenient. Make sense?

As far as the label goes, think about it. Do you personally add "Water" to "Teabag" to get a drink? Or you you put a teabag in the cup, and add water to it there?
The difference is that strings are "names", "labels" - they aren't numbers. So you can't add two strings together "12" + "42" and get a string that has the sum in: "54" because adding strings together adds the "names" together: "12" + "42" gives you a longer string "1242".
So if you keep your total in a label, you have to parse the label to a number, add the new value to that number, then convert the result back to a string to display it in the label.
It makes sense, really: your home telephone number has two parts: an area code such as "0777" and a local code such as "123456". When you compbine the two to get a whole telephone number you want "0777" + "123456" to give you "0777123456", not "124233". But if you want to add my monthly wages of 777 dollars to your monthly wages of 123456 dollars then you want a number: 124233 dollars.
The type of data you are using affects what results you get, so you have to convert it as necessary to the right data types or you will not get the results you want!
Johny2018 29-Jul-17 11:59am    
oh, thank u so much, I understood after reading the first two parts but thx for further examples :) but still I don't know what code should I use :( (stupid me)
my mind is literally blowing up ( English is my second language maybe this is the cause ). It shouldn't be that hard I have a lbl it has nothing in it when u type a number to textbox and hit the btn it should show that num, and when u type another num it should combine those two and just show it in the lbl, like ur example of 12+42 = 54
int val;
if (int.TryParse(textBox1.Text, out val))
{
val++;
label.Text = textBox1.Text;
}
i used int but it keeps showing me whats inside the txtbox.
everything u said makes sense but i can't find out the codes :(
maybe i'll find out tomorrow. anyway thank u so much again for everything u helped a lot.
See examples here: [progressbar] and here: [SQL Client]
 
Share this answer
 
Comments
Johny2018 29-Jul-17 9:08am    
thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900