How can I get a list of all processes running on my system
Use the static Process.GetProcesses() found in the System.Diagnostics namespace. [C#] Using System.Diagnostics; … foreach ( Process p in Process.GetProcesses() ) Console.WriteLine( p ); // string s = p.ToString(); [VB.NET] Imports System.Diagnostics … Dim p As Process For Each p In Process.GetProcesses() Console.WriteLine(p) ’ string s = p.ToString() Next p
How can I run an EXE from within my application
Use the Process class found in the System.Diagnostics namespace. [C#] Process proc = new Process(); proc.StartInfo.FileName = @’Notepad.exe’; proc.StartInfo.Arguments = ”; proc.Start(); [VB.NET] Dim proc As New Process() proc.StartInfo.FileName = ‘Notepad.exe’ proc.StartInfo.Arguments = ” proc.Start()
How can I catch when the user clicks off the grid, say to close the form
You can catch clicking off a DataGrid in the DataGrid.Validated event. But this event is also hit when you use the Tab key to move around the grid. So, if you want to catch it exclusively for clicking off the grid, you have to ignore the event due to the tab. One way to do this is to derive DataGrid and override ProcessDialogKey, noting whether the key is a tab before processing it. Then in your Validated event handler, you can check for this tab. Here are some snippets. [C#] //the handler that checks the derived grid’s field inTabKey private void dataGrid1_Validated(object sender, System.EventArgs e) { if(!this.dataGrid1.inTabKey) { Console.WriteLine( ‘Clicked off grid’); } else this.dataGrid1.inTabKey = false; } //derived class public class MyDataGrid: DataGrid { public bool inTabKey = false; protected override bool ProcessDialogKey(System.Windows.Forms.Keys keyData) { inTabKey = keyData == Keys.Tab; return base.ProcessDialogKey( keyData); } } [VB.NET] ’the handler that checks the derived grid’s field inTabKey Private Sub dataGrid1_Validated(sender As Object, e As System.EventArgs) If Not Me.dataGrid1.inTabKey Then Console.WriteLine(‘Clicked off grid’) Else Me.dataGrid1.inTabKey = False End If End Sub ’dataGrid1_Validated ’derived class Public Class MyDataGrid Inherits DataGrid Public inTabKey As Boolean = False Protected Overrides Function ProcessDialogKey(keyData As System.Windows.Forms.Keys) As Boolean nTabKey = keyData = Keys.Tab Return MyBase.ProcessDialogKey(keyData) End Function ’ProcessDialogKey End Class ’MyDataGrid
How can I change the Border color of my control
Override the OnPaint. Here is some code for a derived Button. [C#] public class MyButton : Button { protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); int borderWidth = 1; Color borderColor = Color.Blue; ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, borderColor, borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid); } } [VB.NET] Public Class MyButton Inherits Button Protected Overrides Sub OnPaint(e As PaintEventArgs) MyBase.OnPaint(e) Dim borderWidth As Integer = 1 Dim borderColor As Color = Color.Blue ControlPaint.DrawBorder(e.Graphics, e.ClipRectangle, borderColor, borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid) End Sub ’OnPaint End Class ’MyButton
How do I get the path to my running EXE
The Application class has a static member ExecutablePath that has this information. [C#] textBox1.Text = Application.ExecutablePath; [VB.NET] TextBox1.Text = Application.ExecutablePath