Showing posts with label WinForms. Show all posts
Showing posts with label WinForms. Show all posts

Wednesday, May 14, 2008

WinForms - Using Mutex to force only one instance of Form

   1: static class Program
   2:  {
   3:      ///<summary>
   4:      /// The main entry point for the application.
   5:      ///</summary>
   6:      [STAThread]
   7:      static void Main()
   8:      {
   9:          bool instanceCountOne = false;
  10:  
  11:          using (Mutex mtex = new Mutex(true, "MyRunningApp", out instanceCountOne))
  12:          {
  13:              if (instanceCountOne)
  14:              {
  15:                  Application.EnableVisualStyles();
  16:                  Application.SetCompatibleTextRenderingDefault(false);
  17:                  Application.Run(new Form1());
  18:                  mtex.ReleaseMutex();
  19:              }
  20:              else
  21:              {
  22:                  MessageBox.Show("An application instance is already running");
  23:              }
  24:          }
  25:      }
  26:  }

Saturday, September 29, 2007

Default Form Button

Form properties ->Misc->AcceptButton

Friday, August 24, 2007

DataGridView - Copy multiple cells to clipboard

How to: Enable Users to Copy Multiple Cells to the Clipboard from the Windows Forms DataGridView Control

DataGridView Column Header Style

In order to use styles in your DataGridView control for your .NET 2.0 Windows application, you must specify dgvMyDataGridView.EnableHeadersVisualStyles = false.
Example of the code:

//Instantiate new DataGridViewCellStyle
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 =
    new System.Windows.Forms.DataGridViewCellStyle();

//Define Header Style
dataGridViewCellStyle2.Alignment =
    System.Windows.Forms.DataGridViewContentAlignment.BottomCenter;
dataGridViewCellStyle2.BackColor = System.Drawing.Color.Navy;
dataGridViewCellStyle2.Font = new System.Drawing.Font("Microsoft Sans Serif",
    8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
dataGridViewCellStyle2.ForeColor = System.Drawing.Color.White;
dataGridViewCellStyle2.SelectionBackColor =
    System.Drawing.SystemColors.Highlight;
dataGridViewCellStyle2.SelectionForeColor = System.Drawing.Color.Navy;
dataGridViewCellStyle2.WrapMode =
    System.Windows.Forms.DataGridViewTriState.True;

//Apply Header Style
this.dgvTransactions.ColumnHeadersDefaultCellStyle =
    dataGridViewCellStyle2;

//Disable Headers Visual Styles - apparently there is an "automatic"
// visual style that needs to be disabled
this.dgvTransactions.EnableHeadersVisualStyles = false;

Cell Styles in the Windows Forms DataGridView Control

Source

DataGridView - Conditional Color Change

If DataGridView.Rows(i).Cells("locuscount").Value = "0" Then
          DataGridView.Rows(i).Cells("Status").Style.BackColor() = Color.AliceBlue
End If

DataGridView - Click Location & Selected Col/Row

 

For selected column and row:

private void myDataGridView_MouseDown(object sender, MouseEventArgs e)
{
    int ColumnIndex =myDataGridView.SelectedColumns[0].Index;  // Selected Column Index
    int RowIndex = myDataGridView.SelectedRows[0].Index;  // Selected Row Index
}
 
 
For click location use:
 
DataGridView.HitTestInfo myHit = dataGridView1.HitTest(e.X, e.Y);
if (myHit.Type == DataGridViewHitTestType.Cell)
{
    // select row == myHit.RowIndex;
}