Thursday, 23 May 2013
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");
}
}
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);
}
}
{
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;
}
}
{
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;
}
}
Monday, 29 April 2013
Get list of network computer Names and IP Address using C#
using System.Net.NetworkInformation;
using System.Net;
NetworkBrowser nb1 = new NetworkBrowser();
string[] PCIP = new string[250];
string[] PCIP1 = new string[250];
int i = 0;
foreach (string pc in nb1.getNetworkComputers())
{
PCIP[i] = pc;
IPAddress[] addresslist = Dns.GetHostAddresses(pc);
foreach (IPAddress address in addresslist)
{
PCIP1[i] = address.ToString();
}
i++;
}
using System.Net;
NetworkBrowser nb1 = new NetworkBrowser();
string[] PCIP = new string[250];
string[] PCIP1 = new string[250];
int i = 0;
foreach (string pc in nb1.getNetworkComputers())
{
PCIP[i] = pc;
IPAddress[] addresslist = Dns.GetHostAddresses(pc);
foreach (IPAddress address in addresslist)
{
PCIP1[i] = address.ToString();
}
i++;
}
Get list of network computer names using C#
//get Network Computers Name
// Use using System.Net.NetworkInformation;
using System.Net.NetworkInformation;
NetworkBrowser nb1 = new NetworkBrowser();
string[] PCIP = new string[250];
int i = 0;
foreach (string pc in nb1.getNetworkComputers())
{
PCIP[i] = pc;
i++;
}
// Use using System.Net.NetworkInformation;
using System.Net.NetworkInformation;
NetworkBrowser nb1 = new NetworkBrowser();
string[] PCIP = new string[250];
int i = 0;
foreach (string pc in nb1.getNetworkComputers())
{
PCIP[i] = pc;
i++;
}
Subscribe to:
Posts (Atom)
