How do I intercept event subscription of a event in my base class?
In C# you could intercept what gets subscribed as follows: // In a Control derived class, for example: // Use override if the base class property was marked as virtual public new event EventHandler Click { add { // Do not let derived classes subscribe to this event, they should instead override OnClick. if (value.Target != this) base.Click += value; } remove { base.Click -= value; } }
How can I display a context menu when the user right-clicks on a node in the TreeView control?
You can display a context menu when a user right-clicks on a node by listening to the TreeView’s MouseUp event as shown below: [C#] private void treeView1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { if(e.Button == MouseButtons.Right) { Point ClickPoint = new Point(e.X,e.Y); TreeNode ClickNode = treeView1.GetNodeAt(ClickPoint); if(ClickNode == null) return; // Convert from Tree coordinates to Screen coordinates Point ScreenPoint = treeView1.PointToScreen(ClickPoint); // Convert from Screen coordinates to Form coordinates Point FormPoint = this.PointToClient(ScreenPoint); // Show context menu contextmenu.MenuItems.Clear(); contextmenu.MenuItems.Add(‘Item1’); contextmenu.MenuItems.Add(‘Item2’); contextmenu.Show(this,FormPoint); } } [VB.NET] Private Sub treeView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) If e.Button = MouseButtons.Right Then Dim ClickPoint As Point = New Point(e.X,e.Y) Dim ClickNode As TreeNode = treeView1.GetNodeAt(ClickPoint) If ClickNode Is Nothing Then Return End If ’ Convert from Tree coordinates to Screen coordinates Dim ScreenPoint As Point = treeView1.PointToScreen(ClickPoint) ’ Convert from Screen coordinates to Form coordinates Dim FormPoint As Point = Me.PointToClient(ScreenPoint) ’ Show context menu contextmenu.MenuItems.Clear() contextmenu.MenuItems.Add(‘Item1’) contextmenu.MenuItems.Add(‘Item2’) contextmenu.Show(this,FormPoint) End If End Sub
I set the wait cursor using Cursor.Current = Cursors.WaitCursor;. Why does does it disappear before I want it to
Setting the Current property changes the cursor and stops the processing of mouse events. Setting the cursor back to Cursors.Default restarts the mouse event processing and displays the proper cursor for each control. If a DoEvents is called before you reset the cursor back to the default, this will also start up the mouse event processing and you will lose the particular cursor you set. So, if your WaitCursor is disappearing, one explanation might be that DoEvents is getting called. Here is some code that sets a WaitCursor. Cursor oldCursor = Cursor.Current; Cursor.Current = Cursors.WaitCursor; try { // Do your processing that takes time… // eg. let’s wait for 2 seconds… DateTime dt = DateTime.Now.AddSeconds(2); while(dt > DateTime.Now) { //do nothing } } finally { Cursor.Current = oldCursor; }
How do I load a BMP file that has been added to my solution as an embedded resource
If you add a BMP file to your solution using the File|Add Existing Item… Menu Item, then change the Build Action property of this BMP file to Embedded Resource, you can then access this resource with code similar to: // WindowsApplication6 corresponds to Default Namespace in your project settings. // subfolders should be the folder names if any, inside which the bmp is added. If the bmp was added to the top level, you don’t have to specify anything here. string bmpName = ‘WindowsApplication6.subfolders.sync.bmp’; System.IO.Stream strm = null; try { strm = this.GetType().Assembly.GetManifestResourceStream(bmpName); pictureBox1.Image = new Bitmap(strm); // Do the same for Icons // Icon icon1 = new Icon(strm); } catch(Exception e) { MessageBox.Show(e.Message); } finally { if(strm != null) strm.Close(); }
How do I create a new bitmap with a new size based on an existing bitmap?
You can simply specify the new size in the Bitmap constructor as follows: [C#] Bitmap bmp = new Bitmap(‘exisiting.bmp’); // Create a new bitmap half the size: Bitmap bmp2 = new Bitmap(bmp, bmp.Width*0.5, bmp.Height*0.5); this.BackgroundImage = bmp2; [VB.Net] Dim bmp As New Bitmap( ‘exisiting.bmp’) ’ Create a new bitmap half the size: Dim bmp2 As New Bitmap( bmp, bmp.Width * 0.5, bmp.Height * 0.5) Me.BackgroundImage = bmp2 If you have to specify a particular Interpolation mode while resizing use the following code: [C#] Bitmap bmp = new Bitmap(‘exisiting.bmp’); // Create a new bitmap half the size: Bitmap bmp2 = new Bitmap( bmp.Width*0.5, bmp.Height*0.5, Imaging.PixelFormat.Format24bppRgb); Graphics g = Graphics.FromImage(bmp2); // Set interpolation mode g.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; // Draw image using specified interpolation mode. g.DrawImage(bmp, 0, 0, bmp2.Width, bmp2.Height); this.BackgroundImage = bmp2 [VB.Net] Dim bmp As New Bitmap( ‘existing.bmp’) Dim bmp2 As New Bitmap( bmp.Width * 0.5, bmp.Height * 0.5, Imaging.PixelFormat.Format24bppRgb) Dim g As Graphics = Graphics.FromImage(bmp2) ’ Set interpolation mode g.InterpolationMode = Drawing.Drawing2D.InterpolationMode.HighQualityBicubic ’ Draw image using specified interpolation mode. g.DrawImage(bmp, 0, 0, bmp2.Width, bmp2.Height) Me.BackgroundImage = bmp2