How do I add shortcuts that are not in the Shortcuts enum to my menu item?
To customize the shortcut (which are not present in the System.Windows.Forms.Shortcut Enum), you need to override the ProcessCmdKey event of the form and handle the needed keys. The below example has a menu item with name “item” and the shortcut Ctrl + Alt + 0 has been set by handling the ProcessCmdKey event. The Click event of the menu item is simulated. //Override this method in the form which have the menu. This will only trigger when the form is on focus. protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (keyData == (Keys.Control | Keys.Alt | Keys.D0)) // Ctrl + Alt + 0 { if (this.Menu != null && this.Menu.MenuItems.Count > 0) this.Menu.MenuItems[‘item’].PerformClick(); // triggers click event. } return base.ProcessCmdKey(ref msg, keyData); } Instead of MenuItem, you can utilize ToolStripMenuItem which has plenty of features and all kind of shortcuts are available.
I don’t like the resource Editor that comes with Visual Studio.NET. Are there any alternatives out there?
There is one by Lutz Roeder. You can find it here along with other useful tools. Resourcer for .NET
How do I autosize a button to fit its text
Get a Graphics object for the button and use its MeasureString method to compute the width you need. Graphics g = button1.CreateGraphics(); float w = g.MeasureString(button1.Text, button1.Font).Width; g.Dispose(); button1.Width = (int)w + 12; // 12 is for the margins
Is there any easy way to convert VB.NET code to C#?
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 to draw a faded image?
The trick is to use an alpha component while drawing the image. // transparency should be in the range 0 to 1 protected void DrawScrollImage(Graphics g, Rectangle rect, Image image, float transparency) { ImageAttributes ia = new ImageAttributes(); ColorMatrix cm = new ColorMatrix(); cm.Matrix00 = 1; cm.Matrix11 = 1; cm.Matrix22 = 1; cm.Matrix33 = transparency; ia.SetColorMatrix(cm); g.DrawImage(image, rect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, ia); }