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
How can I get all IP addresses for my local machine
[C#] string s =”; System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList; for (int i = 0; i < addressList.Length; i ++) { s += addressList[i].ToString() + ‘\n’; } textBox1.Text = s; [VB.NET] Dim s As String = ” Dim addressList As System.Net.IPAddress() = Dns.GetHostByName(Dns.GetHostName()).AddressList Dim i As Integer For i = 0 To addressList.Length – 1 s += addressList(i).ToString() + ControlChars.Lf Next i textBox1.Text = s
How can I determine the node level of a node in my treeview
Here is a code snippet suggested by Mattias Sjögren on the microsoft.public.dotnet.languages.csharp newsgroup. [C#] public int NodeLevel(TreeNode node) { int level = 0; while ((node = node.Parent) != null) level++; return level; } [VB.NET] Public Sub NodateLevel(ByVal node as TreeNode) As Integer Dim level as Integer = 0 While Not node Is Nothing node = node.Parent level = level + 1 End While End Sub