How to convert a Cursor class to a .cur file
protected void WriteCursorToFile(Cursor cursor, string fileName) { byte[] blob = TypeDescriptor.GetConverter(typeof(System.Windows.Forms.Cursor)).ConvertTo(cursor, typeof(byte[])) as byte[]; if(blob != null) { FileStream fileStream = new FileStream(fileName, FileMode.Create); fileStream.Write(blob, 0, blob.Length); fileStream.Flush(); fileStream.Close(); } else MessageBox.Show(‘Unable to convert Cursor to byte[]’); }
I set a Control’s Visible property to true and in the immediate next statement, it returns false. Why doesn’t setting the Visible property ’take’
A control’s Visible property will also depend on it’s parent control’s (if any) visible property. If the parent control is not visible, the control will also return false.
In a Hashtable, why doesn’t setting the value for an existing key to be null remove the key from the Hashtable
This is by design. You have to use Remove to actually remove the Key/Value pair from the Hashtable.
A control’s Validating event is hit even when the user clicks on the Close box. How can I avoid this behavior
One way is to add code to your Validating handler and only execute the validation routine if the mouse is in the client area. This will avoid the click on the title bar and its system menu. You might also want to add special handling for the tab key so your validation is hit independent of the mouse location when you tab off the control. private bool tabKeyPressed = false; private void textBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e) { if(tabKeyPressed || this.ClientRectangle.Contains(this.PointToClient(Cursor.Position))) { if(boolNotOKValues) //do your validating e.Cancel = true; //failed } tabKeyPressed = false; } protected override bool ProcessDialogKey(Keys keyData) { tabKeyPressed = keyData == Keys.Tab; return base.ProcessDialogKey(keyData); }
How can I swap colors in a bitmap or icon
You use ImageAttributes, adding a ColorMap that swaps the colors. Here is a code snippet. Bitmap originalBMP = (Bitmap) Image.FromFile(@’c:\circle.bmp’); //make a copy so original will still be available Bitmap swappedBMP = new Bitmap(originalBMP); Graphics g = Graphics.FromImage(swappedBMP); // Create a color map. ColorMap[] colorSwapper= new ColorMap[2]; colorSwapper[0] = new ColorMap(); colorSwapper[1] = new ColorMap(); colorSwapper[0].OldColor = Color.Red; //red changes to yellow colorSwapper[0].NewColor = Color.Yellow; colorSwapper[1].OldColor = Color.Blue;//blue changes to green colorSwapper[1].NewColor = Color.Green; // Create an ImageAttributes object, and call SetRemapTable ImageAttributes imageAttr = new ImageAttributes(); imageAttr.SetRemapTable(colorSwapper); //overdraw the bitmap with swapped colors g.DrawImage(swappedBMP, new Rectangle(0, 0, swappedBMP.Width, swappedBMP.Height),0, 0, swappedBMP.Width, swappedBMP.Height, GraphicsUnit.Pixel, imageAttr); pictureBox1.Image = swappedBMP; Here is similar code that wraps this technique in a method that swaps a single color. protected void DrawMyBitmap(Graphics gph, Color oldColor, Color newColor, Bitmap baseImage, Rectangle rect) { ImageAttributes imgattr = new ImageAttributes(); ColorMap[] clrmap = new ColorMap[1]{ new ColorMap() }; clrmap[0].OldColor = oldColor; clrmap[0].NewColor = newColor; imgattr.SetRemapTable(clrmap); gph.DrawImage(baseImage,rect,0,0,rect.Width, rect.Height,GraphicsUnit.Pixel,imgattr); }