drupal stats

Windows Forms Controls

Reset all Controls in a Windows Form

Reset all Controls (Textbox, ComboBox, CheckBox, ListBox) in a Windows Form using C#

using System;

using System.Collections.Generic;

using System.Linq;

using System.Windows.Forms;

 

namespace Common

{

    public class Utilities

    {

        public static void ResetAllControls(Control form)

        {

            foreach (Control control in form.Controls)

            {

                if (control is TextBox)

                {

                    TextBox textBox = (TextBox)control;

                    textBox.Text = null;

                }

 

                if (control is ComboBox)

                {

                    ComboBox comboBox = (ComboBox)control;

                    if (comboBox.Items.Count > 0)

                        comboBox.SelectedIndex = 0;

                }

 

                if (control is CheckBox)

                {

                    CheckBox checkBox = (CheckBox)control;

                    checkBox.Checked = false;

                }

 

                if (control is ListBox)

                {

                    ListBox listBox = (ListBox)control;

                    listBox.ClearSelected();

                }

            }

        }      

    }

}

 

Calling method from code behind form:

private void button1_Click(object sender, EventArgs e)

{

    Common.Utilities.ResetAllControls(this);

}

Clear all TextBoxes in a Windows Form

Clear all Text Boxes in a Windows Form using C#

using System;

using System.Collections.Generic;

using System.Linq;

using System.Windows.Forms;

 

namespace Common

{

    public class Utilities

    {

        public static void ClearAllTextBoxes(Control form)

        {

            foreach (Control control in form.Controls)

            {

                if (control is TextBox)

                {

                    TextBox textBox = (TextBox)control;

                    textBox.Text = null;

                }

            }

        }      

    }

}

 

Calling method from code behind form:

private void button1_Click(object sender, EventArgs e)

{

    Common.Utilities.ClearAllTextBoxes(this);

}

Changing Button Background Image on Mouse Hover

Change button background on hover in windows forms

public Form1()

{

   InitializeComponent();

   this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.background_1));

}

 

private void button1_MouseHover(object sender, EventArgs e)

{

   this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.background_2));

}

 

private void button1_MouseLeave(object sender, EventArgs e)

{

   this.button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.background_1));

}

Import Excel file into DataGridView

Import Excel file into DataGridView thumb Import Excel file into DataGridView

 

Import Excel file into DataGridView using C#

string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", txtPath.Text);

string query = String.Format("select * from [{0}$]", "Sheet1");

OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);

DataSet dataSet = new DataSet();

dataAdapter.Fill(dataSet);

dataGridView1.DataSource = dataSet.Tables[0];

 

Download Source

Import CSV file into DataGridView

This article will guide you how to import a CSV file into DataGridView

Import CSV file into DataGridView thumb Import CSV file into DataGridView

See the contents of a csv file

CSV Content thumb Import CSV file into DataGridView

 

The first line is column names:

CustomerID,CompanyName,ContactName

 

The remaining lines are the content, each line corresponds to a record,  the columns are separated by commas “,”

 

Step 1: Creating StreamReader

System.IO.StreamReader streamReader = new StreamReader(txtPath.Text);

 

Step 2: Reading CSV file header

rowValue = streamReader.ReadLine();

cellValue = rowValue.Split(',');                

for (int i = 0; i <= cellValue.Count() - 1; i++)

{

    DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();

    column.Name = cellValue[i];

    column.HeaderText = cellValue[i];

    dataGridView1.Columns.Add(column);

}

 

Step 3: Reading CSV file content

while (streamReader.Peek() != -1)

{

    rowValue = streamReader.ReadLine();

    cellValue = rowValue.Split(',');

    dataGridView1.Rows.Add(cellValue);

}

 

Complete Source Code

private void btnLoadData_Click(object sender, EventArgs e)

{

    string rowValue;

    string[] cellValue;

    

    if (System.IO.File.Exists(txtPath.Text))

    {

        System.IO.StreamReader streamReader = new StreamReader(txtPath.Text);

 

        // Reading header

        rowValue = streamReader.ReadLine();

        cellValue = rowValue.Split(',');                

        for (int i = 0; i <= cellValue.Count() - 1; i++)

        {

            DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();

            column.Name = cellValue[i];

            column.HeaderText = cellValue[i];

            dataGridView1.Columns.Add(column);

        }

 

        // Reading content

        while (streamReader.Peek() != -1)

        {

            rowValue = streamReader.ReadLine();

            cellValue = rowValue.Split(',');

            dataGridView1.Rows.Add(cellValue);

        }

 

        streamReader.Close();

    }

    else

    {

        MessageBox.Show("No File is Selected");

    }

}

 

Download Source

Page 1 of 212