How do I use the CSV clipboard format supported by Excel
In this case, the dataobject is a memory stream. Here are code snippets that allow you to get/set CSV values from/to the clipboard in a manner compatible with Excel. [C#] private void GetCSVFromClipBoard_Click(object sender, System.EventArgs e) { IDataObject o = Clipboard.GetDataObject(); if(o.GetDataPresent(DataFormats.CommaSeparatedValue)) { StreamReader sr = new StreamReader((Stream) o.GetData(DataFormats.CommaSeparatedValue)); string s = sr.ReadToEnd(); sr.Close(); Console.WriteLine(s); } } private void CopyCSVToClipBoard_Click(object sender, System.EventArgs e) { String csv = ‘1,2,3’ + Environment.NewLine + ‘6,8,3’; byte[] blob = System.Text.Encoding.UTF8.GetBytes(csv); MemoryStream s = new MemoryStream(blob); DataObject data = new DataObject(); data.SetData(DataFormats.CommaSeparatedValue, s); Clipboard.SetDataObject(data, true); } [VB.NET] Private Sub GetCSVFromClipBoard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim o As IDataObject = Clipboard.GetDataObject() If Not o Is Nothing Then If (o.GetDataPresent(DataFormats.CommaSeparatedValue)) Then Dim sr As New StreamReader(CType(o.GetData(DataFormats.CommaSeparatedValue), Stream)) Dim s As String = sr.ReadToEnd() sr.Close() Console.WriteLine(s) End If End If End Sub ’GetCSVFromClipBoard_Click Private Sub CopyCSVToClipBoard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim csv As String = ‘1,2,3’ + Environment.NewLine + ‘6,8,3’ Dim blob As Byte() = System.Text.Encoding.UTF8.GetBytes(csv) Dim s As New MemoryStream(blob) Dim data As New DataObject() data.SetData(DataFormats.CommaSeparatedValue, s) Clipboard.SetDataObject(data, True) End Sub ’CopyCSVToClipBoard_Click
How can I create non-rectangular windows
Check out this Microsoft KB article, Shaped Windows Forms and Controls in Visual Studio .NET There are 2 ways to create non-rectangular windows: 1) The Control.Region property (Form inherits this of course): [CS] GraphicsPath gp = new GraphicsPath(); gp.AddEllipse(0,0,100,100); gp.AddRectangle(50,50,100,100); this.Region = new Region(gp); [VB.NET] Dim gp As New GraphicsPath() gp.AddEllipse(0, 0, 100, 100) gp.AddRectangle(50, 50, 100, 100) Me.Region = New [Region](gp) 2) Use the Form.TransparencyKey property. This tells the form not to paint any pixels that match the color of the TransparencyKey. So you can make your fancy skin bitmap, then set the TransparencyKey to be, say Color.Red, then all the pixels that are RGB(255,0,0) will be transparent. (from [email protected] on microsoft.public.dotnet.framework.windowsforms)
How do I check to see if a child form is already displayed so I don’t have two instances showing?
Here are two ways you can do this. i) Within the parent MDI form, use code such as this: // MyChildForm is the one I’m looking for MyChildForm childForm = null; foreach(Form f in this.MdiChildren) { if(f is MyChildForm) { // found it childForm = (MyChildForm) f; break; } } if( childForm != null) { childForm.Show(); childForm.Focus(); } else { childForm = new MyChildForm(); childForm.MdiParent = this; childForm.Show(); childForm.Focus(); } ii) Here is a second solution suggested by John Conwell that implements a singleton pattern on the child form. In the MDI Child form put this code in (where frmChildForm is the MDI child form you want to control) //Used for singleton pattern static frmChildForm childForm; public static ChildForm GetInstance { if (childForm == null) childForm = new frmChildForm; return childForm; } In the Parent MDI form use the following code to call the child MDI form frmChildForm childForm = frmChildForm.GetInstance(); childForm.MdiParent = this; childForm.Show(); childForm.BringToFront(); The first time this code is called, the static GetInstance method will create and return an instance of the child form. Every other time this code is called, the GetInstance method will return the existing instance of the child from, stored in the static field childForm. If the child form instance is ever destroyed, the next time you call GetInstance, a new instance will be created and then used for its lifetime. Also, if you need constructors or even overloaded constructors for your MDI Child Form, just add the needed parameters to the GetInstance function and pass them along to the class constructor.
How do I iterate through all the rows and columns in my datagrid
To iterate through all rows and columns in a WinForms DataGrid, you can access the underlying data using the grid’s view records and columns. The following code example demonstrates how to achieve this by looping through each record and column, retrieving its cell values and printing them to the console. [C#] [VB.NET]
How can I drag a window if it doesn’t have a title bar or border
You can drag such a window by using Win32 APIs to switch the mouse hit to WM_NCLBUTTONDOWN. The code below will allow you to drag by mousing down anywhere in the form’s clientarea as long as you don’t hit a child control on the form. using System.Runtime.InteropServices; ………… public const int WM_NCLBUTTONDOWN = 0xA1; public const int HTCAPTION = 0x2; [DllImportAttribute (‘user32.dll’)] public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); [DllImportAttribute (‘user32.dll’)] public static extern bool ReleaseCapture(); private void Form2_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { ReleaseCapture(); SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0); } Here is a solution posed by Jacob Grass (Abiliti Solutions) that does not rely on InteropServices. Private blnMoving As Boolean = False Private MouseDownX As Integer Private MouseDownY As Integer Private Sub Form1_MouseDown(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) Handles Form1.MouseDown If e.Button = MouseButtons.Left Then blnMoving = True MouseDownX = e.X MouseDownY = e.Y End If End Sub Private Sub Form1_MouseUp(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) Handles Form1.MouseUp If e.Button = MouseButtons.Left Then blnMoving = False End If End Sub Private Sub Form1_MouseMove(ByVal sender As Object, _ ByVal e As System.Windows.Forms.MouseEventArgs) Handles Form1.MouseMove If blnMoving Then Dim temp As Point = New Point() temp.X = Form1.Location.X + (e.X – MouseDownX) temp.Y = Form1.Location.Y + (e.Y – MouseDownY) Form1.Location = temp End If End Sub