How can I add items to the System Menu of a form.
To do this, you can use use iterop to access the GetSystemMenu and AppendMenu Win32 APIs. You also need to override the form’s WndProc method to catch and act on the menu message. This idea was posted in the Microsoft newsgroups by Lion Shi. Here are some sample projects.
How can I control the cursor over my DataGrid
One way you can do this is to derive the DataGrid and override its WndProc method to handle the WM_SETCURSOR method yourself. In your override, you can do hit testing to decide when you want to set the cursor, or when you want to call the base class and let it set the cursor. Here are sample projects (VB and C#) showing the technique. Public Class MyDataGrid Inherits DataGrid Private Const WM_SETCURSOR As Integer = 32 Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) If m.Msg <> WM_SETCURSOR Then MyBase.WndProc(m) Else ’see if you want the cursor – in col 1, rows 2, 3, 4 Dim pt As Point = Me.PointToClient(Control.MousePosition) Dim hti As DataGrid.HitTestInfo = Me.HitTest(pt.X, pt.Y) If hti.Column = 1 AndAlso hti.Row > 1 AndAlso hti.Row < 5 Then Cursor.Current = Cursors.Hand ’if not, call the baseclass Else MyBase.WndProc(m) End If End If End Sub ’WndProc Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs) If Cursor.Current.Equals(Cursors.Hand) Then MessageBox.Show(”My MouseDown”) Else MyBase.OnClick(e) End If End Sub ’OnMouseDown End Class ’MyDataGrid
How do I bind a TextBox to a nested relation
Say you have a Parent table related to a Child table related to a GrandChildTable, and you want to bind a TextBox to a column in the GrandChild table. To do this you have to have nested relations defined, and you use this nesting to create the DataMember parameter for the the DataBinding that you add to the TextBox. Below are some code snippets. They show the DataMember as ”ParentToChild.ChildToGrandChild.Name” which creates a path from the parent table down to the field in the nested relation by stringing the relation names together separated with a period. You can also download both C# and VB projects. Dim dSet As New DataSet() ’get the tables Dim parentTable As DataTable = GetParentTable() Dim childTable As DataTable = GetChildTable() Dim grandChildTable As DataTable = GetGrandChildTable() dSet.Tables.AddRange(New DataTable() {parentTable, childTable, grandChildTable}) ’setup the relations Dim parentColumn As DataColumn = parentTable.Columns(”parentID”) Dim childColumn As DataColumn = childTable.Columns(”ParentID”) dSet.Relations.Add(”ParentToChild”, parentColumn, childColumn) parentColumn = childTable.Columns(”childID”) childColumn = grandChildTable.Columns(”ChildID”) dSet.Relations.Add(”ChildToGrandChild”, parentColumn, childColumn) Me.TextBox1.DataBindings.Add(”Text”, parentTable, ”ParentToChild.ChildToGrandChild.Name”)
How can I display master-details-details in three separate grids
Here is a sample (both VB and C#) that illustrates how to have a parent table which has a related child table, which also has a related grandchild table. Below are some code snippets. The trick is to always make the main parent table be the DataSource for all the grid, and then set the DataMember to be the relation name where teh relation starts at the parent table. This means the DisplayMember for the Child table is ”ParentToChild”, the name of that relation. And, the DisplayMember for the grandchild grid is ”ParentToChild.ChildToGrandChild” which defines the relation starting at the parent grid through the child grid. Dim dSet As New DataSet() ’get the tables Dim parentTable As DataTable = GetParentTable() Dim childTable As DataTable = GetChildTable() Dim grandChildTable As DataTable = GetGrandChildTable() dSet.Tables.AddRange(New DataTable() {parentTable, childTable, grandChildTable}) ’setup the relations Dim parentColumn As DataColumn = parentTable.Columns(”parentID”) Dim childColumn As DataColumn = childTable.Columns(”ParentID”) dSet.Relations.Add(”ParentToChild”, parentColumn, childColumn) parentColumn = childTable.Columns(”childID”) childColumn = grandChildTable.Columns(”ChildID”) dSet.Relations.Add(”ChildToGrandChild”, parentColumn, childColumn) ’set the grids Me.dataGrid1.DataSource = parentTable Me.dataGrid2.DataSource = parentTable Me.dataGrid2.DataMember = ”ParentToChild” Me.dataGrid3.DataSource = parentTable Me.dataGrid3.DataMember = ”ParentToChild.ChildToGrandChild” Me.dataGrid1.AllowNavigation = False Me.dataGrid2.AllowNavigation = False Me.dataGrid3.AllowNavigation = False
How can I tell which column (subitem) has been clicked on in my listview
Here is a solution offered by Bharat Patel of Microsoft in the microsoft.public.dotnet.languages.csharp newgroup. The attached solution contains both C# and VB.NET projects. The solution use Interop with the LVM_GETSUBITEMRECT window’s message to loop through each subitem’s bounds rectangle until it finds the one that contains the specified mouse click point.