Converting seconds into days, hours, minutes and seconds
int days = seconds / 60 / 60 / 24;
int hours = (seconds / 60 / 60) % 24;
int minutes = (seconds / 60) % 60;
int seconds = seconds % 60;
Converting seconds into days, hours, minutes and seconds
int days = seconds / 60 / 60 / 24;
int hours = (seconds / 60 / 60) % 24;
int minutes = (seconds / 60) % 60;
int seconds = seconds % 60;
Add an event handler for Form Closing and use this code. This is a really simple solution.
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Are you sure?", "Close application", MessageBoxButtons.YesNo) == DialogResult.Yes)
e.Cancel = false;
else
e.Cancel = true;
}
Sometimes you need to check if a number is even or odd. A simple way to do this is using the modulus operator (the %-sign). It will calculate and return the remainder when dividing a value with another value.
So how do you use that to find out if a number is even or odd?
As usual you need to understand the logic of the operation to be able to program a solution. In this case it’s really easy… The definition an even number is of course that it’s dividable by two (leaving no remainder). This is simple second grade school math.
So all we have to do is divide the number by 2 and check that the remainder is 0. Using the modulus operator this could look like this: 4 % 2 == 0. That operation will return TRUE, since dividing 4 by 2 will give the remainder of 0.
Sometimes you need to check a lot of numbers through out your application, and it might be convenient to have a function you can simply call whenever needed.
A very simple function, that takes one argument (number) could look like this:
<?php
function isEven($number)
{
if ($number % 2 == 0)
return true;
else
return false;
}
?>
The function will return TRUE if the number is even and FALSE if it is odd. Also, since the only values the function will return is either TRUE or FALSE (depending on the outcome of the if-condition). You could put the condition directly in the return statement. Looks better and saves some code. Like this:
function isEven($number)
{
return ($number % 2 == 0);
}
You just call the function with the variable or number you need to check. Like this:
<?php
function isEven($number)
{
return ($number % 2 == 0);
}
$dude = 3;
if (isEven($dude))
echo $dude." is an even number";
else
echo $dude." is an odd number";
?>
You could of course just use the modulus directly in your code, without a function since it holds very little code. But there might be other operations you want to do in there – like checking if the number is an integer and so on.
Also, putting often used code inside a good function with a self explanatory name – will increase the readability of your code.
Short snippet:
textBox.SelectionStart = textBox.TextLength; textBox.ScrollToCaret();
Principle 1.
Always have a clear image of what you want to do BEFORE you start writing your code. Then break it down in logical instructions – making an algorithm. An algorithm is a step by step instruction on how to solve a specific problem. You might think of it as a recipe. If you want to bake bread – you don’t just start shoving any eatable stuff you find into the oven without a plan. First you decide what kind of bread you want to make, then you find out what ingredients is needed, how much and in what order they are added.
So basically – if you can’t solve the problem with logic you can’t solve it with code.
For example:
You want to write a piece of code that finds the mean (average) of three numbers supplied by a user. Before you start writing the code, you must of know how to calculate the mean of three numbers. The solution to this mathematical problem is of course (n1+n2+n3) / 3. Now I just have to break it down into step by step instructions, like this:
1. Gather three numbers from the user
2. Add them together.
3. Divide the sum by three
4. Output the sum on the screen
Now I can easily translate this simple algorithm to code in any language, for example PHP. Review your steps and think about how to solve this in code;
Step 1. Make a form with three textfields.
Step 2 & 3. Take the three values from the POST array, add them together and divide by three.
Step 4. Echo the result.
<html>
<body>
<?php
if (isset($_POST['submit'])) {
$n1 =$_POST['n1'];
$n2 =$_POST['n2'];
$n3 =$_POST['n3'];
$sum = ($n1 + $n2 + $n3) / 3;
echo $sum;
}
?>
<form action="" method="post">
<input type="text" name="n1" />
<input type="text" name="n2" />
<input type="text" name="n3" />
<input type="submit" name="submit" />
</form>
</body>
</html>
The calculation could even fit in just one single line:
echo ($_POST['n1'] + $_POST['n2'] + $_POST['n3']) / 3;
Principle 2.
If you use code that you find online (like on a blog or a forum), make sure you learn what every single line of code does. It will take a bit of time, and might demand some hard work – but having code in your application that you don’t really understand is a bit like playing the lottery.
Getting code to do exactly what you want will be very hard if you don’t know exactly what is does.
Also, this is maybe the best way to learn how to code.