How do I make the RichTextBox support drag and drop
1) Set the AllowDrop property to true 2) Add handlers for both the DragEnter and DragDrop event this.richTextBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.richTextBox1_DragEnter); this.richTextBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.richTextBox1_DragEnter); …. private void richTextBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) { if (((DragEventArgs)e).Data.GetDataPresent(DataFormats.Text)) ((DragEventArgs)e).Effect = DragDropEffects.Copy; else ((DragEventArgs)e).Effect = DragDropEffects.None; } private void richTextBox1_DragDrop(object sender, DragEventArgs e) { // Loads the file into the control. richTextBox1.LoadFile((String)e.Data.GetData(‘Text’), System.Windows.Forms.RichTextBoxStreamType.RichText); } Here are VB snippets. AddHandler Me.richTextBox1.DragEnter, New System.Windows.Forms.DragEventHandler(AddressOf Me.richTextBox1_DragEnter) AddHandler Me.richTextBox1.DragDrop, New System.Windows.Forms.DragEventHandler(AddressOf Me.richTextBox1_DragEnter) ….. Private Sub richTextBox1_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs) If CType(e, DragEventArgs).Data.GetDataPresent(DataFormats.Text) Then CType(e, DragEventArgs).Effect = DragDropEffects.Copy Else CType(e, DragEventArgs).Effect = DragDropEffects.None End If End Sub ’richTextBox1_DragEnter Private Sub richTextBox1_DragDrop(sender As Object, e As DragEventArgs) ’ Loads the file into the control. richTextBox1.LoadFile(CType(e.Data.GetData(‘Text’), [String]), System.Windows.Forms.RichTextBoxStreamType.RichText) End Sub ’richTextBox1_DragDrop
How do I find the top-left visible cell in a datagrid
In a Windows Forms DataGrid, there is no property exposed that gives you this information. But here is little trick that will allow you to get the top-left visible cell. 1) Add a private member Point pointInCell00 to the form containing the datagrid. 2) After the datagrid has been initialized, but before your user has been able to scroll it (say at the end of the Form_Load event), use code such as this to initialize pointInCell00 to be a point in cell 0,0. pointInCell00 = new Point(dataGrid1.GetCellBounds(0,0).X + 4, dataGrid1.GetCellBounds(0,0).Y + 4); 3) Then add this method to return DataGridCell that is the top-left cell. public DataGridCell TopLeftVisibleCell() { DataGrid.HitTestInfo hti = dataGrid1.HitTest(this.pointInCell00); return new DataGridCell(hti.Row, hti.Column); } //sample usage… private void button1_Click(object sender, System.EventArgs e) { DataGridCell dgc = TopLeftVisibleCell(); MessageBox.Show(dgc.RowNumber.ToString() + ‘ ‘ + dgc.ColumnNumber.ToString()); }
How do you provide ‘dynamic’ default values
Provide ShouldSerialize#PropertyName# and Reset#PropertyName# along with your property. Example: private bool ShouldSerializeFont() { return this.bFontSet; } /// /// Resets the property to its default value. /// private void ResetFont() { this.localFont = null; }
Where can I find a discussion of databinding and windows forms
Take a look at the Microsoft KB article, INFO: Roadmap for Windows Forms Data Binding (Q313482). This article gets you started. It also lists other Microsoft KB articles that have to do with using the Windows Forms DataGrid and databinding.
I want to do sort of a database join of two tables. How can I use data from two DataTables in a single DataGrid
Take a look at this Microsoft KB article, HOW TO: Extend the Windows Form DataGridTextBoxColumn to Display Data From Other Tables (C#,VB). It subclasses a DataGridColumnStyle and overrides GetColumnValueAtRow, Commit and ReadOnly to implement this behavior.