Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Using app.config file in C# Visual Studio

If  would like to read some value from app.config file follow these steps.

1,Add System Configuration namespace to reference



















Otherwise you will get 'ConfigurationManager' does not exist error

2,Import name space

using System.Configuration

3,Read String from config file

ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;



4,Sample App.config file


<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <connectionStrings>
    <add name="SqlConnectionString" 
connectionString="Data Source=localhost\SQLEXPRESS;Initial 
Catalog=library;Integrated Security=True;" />

  </connectionStrings>
</configuration>






Using Timer in C#

1,Drag & drop timer from Visual Studio tool box

2,To start timer
timer1.Start();
3,To Stop Timer
timer1.Stop();
4,Timer Tick event (Executes every second if timer started) 

private void timer1_Tick(object sender, EventArgs e)
{
commands
}

5,To count down using Timer

private void timer1_Tick(object sender, EventArgs e)
{
label3.Text = (Convert.ToInt32(label3.Text) - 1).ToString();
}




Easy way to handle combo box in C#

1,Drag & drop Combo box in form in visual studio
2,Assign list like below picture






















3,Assign default value in Combo box when form load

private void Form1_Load(object sender, EventArgs e)
        {
            
            comboBox1.SelectedIndex = 0;
        }
 

4,Double click on combo box will show SelectedIndexChanged event,Use Switch command to write your different event code.

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (comboBox1.SelectedIndex){

                case 0: shutdown = "-s"; break;
                case 1: shutdown = "-r"; break;
                case 2: shutdown = "-l"; break;

            
            
            }




^ Top