How do I prevent the context menu from showing up on certain keyboard keys (like Keys.Apps)?

Override WndProc in your Control and do the following. You should then listen to keyup and show the context menu yourself. [C#] protected override void WndProc(ref Message m) { if(m.Msg == 0x7b /*WM_CONTEXTMENU*/ ) { return; } if(m.Msg == 0x101 /*WM_KEYUP*/) { Keys keys = (Keys)m.WParam.ToInt32(); // Prevent this key from being processed. if(keys == Keys.Apps) return; } base.WndProc(ref m); } [VB.Net] Protected Overrides Sub WndProc(ByRef m As Message) If m.Msg = 0x7b Then ’WM_CONTEXTMENU Return End If If m.Msg = 0x101 Then ’WM_KEYUP Dim keys As Keys = CType(m.WParam.ToInt32(), Keys) ’ Prevent this key from being processed. If keys = Keys.Apps Then Return End If End If MyBase.WndProc( m) End Sub

How to debug your design time code (like designers, typeconverters, etc.)

You need to use a second instance of VS.NET to debug the one that’s running the code. Put your control on a from in VS.NET Start a 2nd Vs.Net Choose the Debug menu >> Processes … Double click ‘devenv.exe’ and choose ‘Common Language Runtime’ as the types of debugging Open your code file, set your breakpoint, and you’re debugging. Posted by Shawn Burke of MSFT in microsoft.public.dotnet.framework.windowsforms.

How do I programmatically determine the selected rows in a datagrid

The method DataGrid.IsSelected can tell you if a particular row is selected. So, you could use IsSelected in a loop through all your rows to finds if multiple rows have been selected. Depending upon the size of your datagrid, this may be a viable solution. If not, you could track the selections yourself by monitoring the key actions and the mouse actions. This would be more work. Thanks to John Hughes to the suggestion to use the dataview. [C#] public ArrayList GetSelectedRows(DataGrid dg) { ArrayList al = new ArrayList(); CurrencyManager cm = (CurrencyManager)this.BindingContext[dg.DataSource, dg.DataMember]; DataView dv = (DataView)cm.List; for(int i = 0; i < dv.Count; ++i) { if(dg.IsSelected(i)) al.Add(i); } return al; } private void button1_Click(object sender, System.EventArgs e) { string s = ‘Selected rows:’; foreach(object o in GetSelectedRows(dataGrid1)) { s+=”+o.ToString(); } MessageBox.Show(s); } [VB.NET] Public Function GetSelectedRows(ByVal dg As DataGrid) As System.Collections.ArrayList Dim al As New ArrayList() Dim cm As CurrencyManager = Me.BindingContext(dg.DataSource, dg.DataMember) Dim dv As DataView = CType(cm.List, DataView) Dim i As Integer For i = 0 to dv.Count – 1 If dg.IsSelected(i) Then al.Add(i) End If End Next Return al End Function ’GetSelectedRows Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim s As String = ‘Selected rows:’ Dim o As Object For Each o In GetSelectedRows(dataGrid1) s += ‘ ‘ + o.ToString() Next o MessageBox.Show(s) End Sub ’button1_Click

I get a ‘This would cause two bindings in the collection to bind to the same property’ error message. What might cause this

As the message suggests, the code is calling Control.DataBindings.Add twice with what amounts to the same parameters. One way this might happen is if you call the same code more than once in your program to reload your datasource for some reason, and in this code, you have lines such as: Me.TextBox1.DataBindings.Add(‘Text’, myDataTable, ‘Col1Name’) Me.TextBox2.DataBindings.Add(‘Text’, myDataTable, ‘Col2Name’) Me.TextBox3.DataBindings.Add(‘Text’, myDataTable, ‘Col3Name’) On the second call, this would attempt to add a duplicate binding to the DataBindings collection. One solution is to Clear the DataBindings collection before you add your new binding. Me.TextBox1.DataBindings.Clear(); Me.TextBox1.DataBindings.Add(‘Text’, myDataTable, ‘Col1Name’) Me.TextBox2.DataBindings.Clear(); Me.TextBox2.DataBindings.Add(‘Text’, myDataTable, ‘Col2Name’) Me.TextBox3.DataBindings.Clear(); Me.TextBox3.DataBindings.Add(‘Text’, myDataTable, ‘Col3Name’)

Why am I not being able to set a Color.Transparent color as a background to my control?

Sometimes the framework will throw an exception if you try to set the bg color to be transparent. This is because the Control doesn’t support transparent colors. To work around this you should call this method from within the Control: [C#] this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); [VB.Net] Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True) Depending on the Control, you might also have to perform some custom drawing, then.