Does .Net support ‘deprecation’ as in Java?
Yes, use the Obsolete attribute on the property, method, etc. You can also provide some deprecation message when you specify this attribute.
How can I use events to restrict key input to grid cells
If you make sure your DataGrid is using a DataGridTableStyle, then you can access the TextBox through the GridColumnStyles collection and hook the event there. Here is some code…. [C#] //in formload this.dataGrid2.DataSource = this.dataSet11.Customers; // set the data source //make sure grid has a tablestyle DataGridTableStyle ts = new DataGridTableStyle(); ts.MappingName = this.dataSet11.Customers.TableName; this.dataGrid2.TableStyles.Add(ts); //now we can wire up wire up events for columns 1 and 4 …. DataGridTextBoxColumn tbc = (DataGridTextBoxColumn)ts.GridColumnStyles[0]; tbc.TextBox.KeyPress += new KeyPressEventHandler(CellKeyPress); tbc = (DataGridTextBoxColumn)ts.GridColumnStyles[3]; tbc.TextBox.KeyPress += new KeyPressEventHandler(CellKeyPress);….. //the handler private void CellKeyPress(object sender, KeyPressEventArgs e) { //don’t allow 1’s if(e.KeyChar == ’1’) e.Handled = true; } [VB.NET] ’in formload Me.dataGrid2.DataSource = Me.dataSet11.Customers ’ set the data source ’make sure grid has a tablestyle Dim ts As New DataGridTableStyle() ts.MappingName = Me.dataSet11.Customers.TableName Me.dataGrid2.TableStyles.Add(ts) ’now we can wire up wire up events for columns 1 and 4 …. Dim tbc as DataGridTextBoxColumn = CType(ts.GridColumnStyles(0), DataGridTextBoxColumn) AddHandler tbc.TextBox.KeyPress, AddressOf CellKeyPress tbc = CType(ts.GridColumnStyles(3), DataGridTextBoxColumn) AddHandler tbc.TextBox.KeyPress, AddressOf CellKeyPress ….. ’the handler Private Sub CellKeyPress(sender As Object, e As KeyPressEventArgs) ’don’t allow 1’s If e.KeyChar = ‘1’c Then e.Handled = True End If End Sub ’CellKeyPress
How can I programmatically manipulate Anchor styles
You can do this using the bitwise operators &, | and ^ ( And, Or and Xor (or &, Or, ^) in VB.Net). Here is code that will toggle label1 being anchored on the left. [C#] private void button1_Click(object sender, System.EventArgs e) { if ((label1.Anchor & AnchorStyles.Left) == 0) { //add it label1.Anchor = label1.Anchor | AnchorStyles.Left; } else if ((label1.Anchor & AnchorStyles.Left) != 0) { //remove label1.Anchor = label1.Anchor ^ AnchorStyles.Left; } } [VB.NET] Private Sub button1_Click(sender As Object, e As System.EventArgs) If(label1.Anchor And AnchorStyles.Left) = 0 Then ’add it label1.Anchor = label1.Anchor Or AnchorStyles.Left ElseIf(label1.Anchor And AnchorStyles.Left) <> 0 Then ’remove label1.Anchor = label1.Anchor Xor AnchorStyles.Left End If End Sub ’button1_Click
How can I get a CheckBox column in a DataGrid to react to the first click
When you first click into a checkbox column, the checked state of the cell does not change. One way you can make the checked state change on the first click is to handle the grid’s MouseUp event, and change the check value there. [VB.Net} Private myCheckBoxCol As Integer = 9 ’my checkbox column Private Sub DataGrid2_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles DataGrid2.MouseUp Dim hti As DataGrid.HitTestInfo = Me.dataGrid2.HitTest(e.X, e.Y) Try If hti.Type = DataGrid.HitTestType.Cell AndAlso hti.Column = myCheckBoxCol Then Me.dataGrid2(hti.Row, hti.Column) = Not CBool(Me.dataGrid2(hti.Row, hti.Column)) End If Catch ex As Exception MessageBox.Show(ex.ToString()) End Try End Sub ’dataGrid2_MouseUp [C#] private int myCheckBoxCol = 9; //my checkbox column private void dataGrid2_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { DataGrid.HitTestInfo hti = this.dataGrid2.HitTest(e.X, e.Y); try { if( hti.Type == DataGrid.HitTestType.Cell && hti.Column == myCheckBoxCol) { this.dataGrid2[hti.Row, hti.Column] = ! (bool) this.dataGrid2[hti.Row, hti.Column]; } } catch(Exception ex) { MessageBox.Show(ex.ToString()); } }
How do I prevent a control from receiving focus when it receives a mouseclick
You can set the control’s Enabled property to false. This will prevent clicking but also gives the disabled look. There is a ControlStyles.Selectable flag that determines whether the control can get focus or not. But it does not appear to affect some controls such as TextBox. But you can prevent the TextBox from getting focus by overriding its WndProc method and ignoring the mouse down. public class MyTextBox : TextBox { const int WM_LBUTTONDOWN = 0x0201; protected override void WndProc(ref System.Windows.Forms.Message m) { if(m.Msg == WM_LBUTTONDOWN) return; base.WndProc(ref m); } }