Friday, 24 May 2013

textbox - wait for user to finish typing in a Text Box

private void txtBusinessName_TextChanged(object sender, EventArgs e)
{
            TimerKey = 3000;
            timer1.Interval = TimerKey;
            timer1.Enabled = true;
}

private void timer1_Tick(object sender, EventArgs e)
{
          SearchFirstTextbox();
            timer1.Enabled = false;
}

public void SearchFirstTextbox()
{
           string searchList = "<sreacn Key Word>";
            int col = 1;
            int colCount = col + 1;

            for (int lvelist = 0; lvelist < listview1.Items.Count; lvelist++)
            {
                if (listview1.Items[lvelist].Text.ToLower().Contains(searchList.ToLower()))
                {
                    listview1.TopItem = lvwClientInformation.Items[lvelist];
                    listview1.Items[lvelist].Selected = true;
                    listview1.Select();
                    lvelist = listview1.Items.Count;
                }
}

Thursday, 23 May 2013

Display Time with the DateTimePicker Control

Display Time with the DateTimePicker Control

datetimePicker.Format = DateTimePickerFormat.Time;
datetimePicker.ShowUpDown = true; 

Comobo Box Hide / Not Type (Key press)

Solutions :  
private void ComoboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
            e.Handled = true;
}

shortcuts in C# Windows Form

Solutions :   

private void frmPartsFormWizard_KeyDown(object sender, KeyEventArgs e)
{
            if (Control.ModifierKeys == Keys.Alt && e.KeyCode == Keys.S)
            {
                txtbox1.Focus();
            }
            else if (Control.ModifierKeys == Keys.Alt && e.KeyCode == Keys.R)
            {
                txtbox2.Focus();
            }
}

Wednesday, 22 May 2013

string[] remove null (string array remove Null)

string[] items = new string[]
{ "", "Name1", "Name2", null, "Name3", "Name4", null, "Name5" };
 
Solutions :   
items = items.Where(s => !String.IsNullOrEmpty(s)).ToArray(); 

Friday, 17 May 2013

How check if the form is already open

using System.Windows.Forms;

FormCollection fc = Application.OpenForms;
For (int FrmCount = 0; FrmCount < fc.Count; FrmCount++)
{
    Form frm = fc[FrmCount];
    if (frm.Name == "<Form Name>")
    {
       MessageBox.Show(" Child Forms Open");
    }
}

Tuesday, 14 May 2013

ListView Column wise Sort

private void Listview1_ColumnClick(object sender, ColumnClickEventArgs e)
{
     this.Listview1.ListViewItemSorter = new ListViewItemComparer(e.Column);
}
class ListViewItemComparer : IComparer
        {
            private int col;
            public ListViewItemComparer()
            {
                col = 0;
            }
            public ListViewItemComparer(int column)
            {
                col = column;
            }
            public int Compare(object x, object y)
            {
                return String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
            }
        }

Copy data copy/paste to DataGridView use C#

private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Modifiers == Keys.Control)
                {
                    switch (e.KeyCode)
                    {
                        case Keys.C:
                            CopyToClipboard();
                            break;

                        case Keys.V:
                            PasteClipboard();
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Copy/paste operation failed. " + ex.Message, "Copy/Paste", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

private void CopyToClipboard()
        {
            //Copy to clipboard
            DataObject dataObj = dataGridView1.GetClipboardContent();
            if (dataObj != null)
                Clipboard.SetDataObject(dataObj);
        }

private void PasteClipboard()
        {
            try
            {
                string s = Clipboard.GetText();
                string[] lines = s.Split('\n');

                int iRow = dataGridView1.CurrentCell.RowIndex;
                int iCol = dataGridView1.CurrentCell.ColumnIndex;
                DataGridViewCell oCell;
                if (iRow + lines.Length > dataGridView1.Rows.Count - 1)
                {
                    bool bFlag = false;
                    foreach (string sEmpty in lines)
                    {
                        if (sEmpty == "")
                        {
                            bFlag = true;
                        }
                    }

                    int iNewRows = iRow + lines.Length - dataGridView1.Rows.Count;
                    if (iNewRows > 0)
                    {
                        if (bFlag)
                            dataGridView1.Rows.Add(iNewRows);
                        else
                            dataGridView1.Rows.Add(iNewRows + 1);
                    }
                    else
                        dataGridView1.Rows.Add(iNewRows + 1);
                }
                foreach (string line in lines)
                {
                    if (iRow < dataGridView1.RowCount && line.Length > 0)
                    {
                        string[] sCells = line.Split('\t');
                        for (int i = 0; i < sCells.GetLength(0); ++i)
                        {
                            if (iCol + i < this.dataGridView1.ColumnCount)
                            {
                                oCell = dataGridView1[iCol + i, iRow];
                                oCell.Value = Convert.ChangeType(sCells[i].Replace("\r", ""), oCell.ValueType);
                            }
                            else
                            {
                                break;
                            }
                        }
                        iRow++;
                    }
                    else
                    {
                        break;
                    }
                }
                Clipboard.Clear();
            }
            catch (FormatException)
            {
                MessageBox.Show("The data you pasted is in the wrong format for the cell");
                return;
            }
        }