Showing posts with label Button. Show all posts
Showing posts with label Button. Show all posts

Sunday, 27 September 2009

Get current date and time using C#

In this example, the date and time will be written to 'label1' when 'button3' is pressed.
private void button3_Click(object sender, EventArgs e)

{

label1.Text = DateTime.Now.ToString();

}

C# application that counts the number of characters being entered

This application consists of a textbox and a label. As the user types in the text box the label displays the number of characters that have been entered. The code is as follows :

private void textBox1_TextChanged (object sender, EventArgs e)

{
int charactercount;
string labeloutput;

charactercount = textBox1.TextLength;
labeloutput = charactercount.ToString();

lblcount.Text = "Character count : " + labeloutput;

{

For Loop to display user input in C#

In this example, when 'button3' is pressed the application will read what has been added to 'textBox1' and print it the console each time it loops through.
private void button3_Click(object sender, EventArgs e)

{

for (int i = 1; i <=10; ++i)

{

System.Console.WriteLine (textBox1.Text);

}

}

Trim spaces from the start and end of string using C#

In this example I have a text box on my windows form to collect the user input and a label to display the outcome once a button is pressed. The code is as follows to trim spaces from the beginning and end of a user input.

private void button3_Click(object sender, EventArgs e)

{

string userinput;
char [] trimchars = {' '};

userinput = textBox1.Text.Trim(trimchars);

lable1.Text = userinput;

}

Collect user input from form and change it to upper or lower case in C#

In this example I have a text box on my windows form to collect the user input and a label to display the outcome once a button is pressed. The code is as follows to change the input to upper and lower case.

private void button3_Click(object sender, EventArgs e)

{

label1.Text = textBox.Text.ToUpper();

}

or

{

label1.Text = textBox.Text.ToLower();

}

Check if folder exists and return a message in C#

The form I have used for this example contains one label and one button. The code checks the location you specify and then prints a message in the label to tell you if the directory exists.

using System.IO;

private void button1_Click(object sender, EventArgs e)
{
string foldername;
string path;
string combinedpath;
{

foldername = "testfolder";
path = @"C:\blogexample";
combinedpath = path + @"\" + foldername;

if
(Directory.Exists(combinedpath))
label1.Text = "Directory exists";
else
label1.Text = "Directory doesn't exist";

}

How do you add a value from a webform to a variable in C#?

In Visual Studio 2005 :

Create a webform with a text field and change it's name to 'txtID'
Create another text field to show the result of the variable and change its name to 'txtResult'
Add a button and then add this code to it's click event.

String info = txtID.Text;

txtResult.Text = info;

When the button is clicked it will take the text that is in the field and add it to the variable called 'info. It will then display the value of the variable in the txtResult field.