How do I add an application to the Window’s Tray
In design mode, you drop a NotifyIcon object on your form. You can then drop a ContextMenu on your form and add this menu to the NotifyIcon’s ContextMenu property. This menu will be seen when the user rightclicks the try icon. You can add a handler for the NotifyIcon’s Click event to catch the action of a user clicking the icon. From code, you create an instance of NotifyIcon, set properties, hook any handlers you want, and then make it visible. Here are some VB snippets. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim trayItem As NotifyIcon = New NotifyIcon() trayItem.Icon = SystemIcons.Question trayItem.Visible = True AddHandler trayItem.Click, New EventHandler(AddressOf HandleTrayClick) End Sub Private Sub HandleTrayClick(ByVal sender As Object, ByVal e As EventArgs) MessageBox.Show(‘Tray item clicked’) End Sub
How do I set the color and font in a RichEditBox
You use the SelectionFont and SelectionColor properties. Make sure the control had focus. Then the following code will set the currently selected text to a red-bold-courier font. If no text is currently selected, then any new text typed (or inserted) will be red-bold-courier. [C#] richTextBox1.Focus(); richTextBox1.SelectionColor = Color.Red; richTextBox1.SelectionFont = new Font (‘Courier’, 10, FontStyle.Bold); [VB.NET] richTextBox1.Focus() richTextBox1.SelectionColor = Color.Red richTextBox1.SelectionFont = new Font (‘Courier’, 10, FontStyle.Bold)
How can I draw without handling a paint message
To draw on any hwnd, you need to get a Graphics object from that hwnd. Once you have the Graphics object, you can then use its methods to draw. Of course, since this drawing is not done in the Paint handler, it will not be automatically refreshed when the control is redrawn for any reason. Graphics g = Graphics.FromHwnd(this.Handle); SolidBrush brush = new SolidBrush(Color.Red); Rectangle rectangle = new Rectangle(25, 25, 100, 100); g.FillRectangle(brush, rectangle);
How can I implement an owner drawn listview
Carlos Perez explains how to do this in an article and a sample on codeproject.com. In this sample, he does an owner-drawn listview so he can implement custom sorting with a column header that contains a up/down icon to indicate the sorted column and order.
From within a PropertyGrid, is there any way to display property names that differ from the actual class property names
You can do this but it’s not as simple as using an attribute. If you want to modify the property names for your component, you have to interact with the reflection mechanism that the grid is using by implementing ICustomTypeDescriptor. The grid operates on objects called PropertyDescriptors with are the Framework’s generic wrapper for a property, where reflection is a specific implementation. So what ICustomTypeDescriptor allows you to do is to have *your object* asked for it’s properties rather than than the default system, which is reflection. You then write your own derived version of PropertyDescriptor (an abstract class in System.ComponentModel) and return your property descriptors instead of the ones that come back from the TypeDescriptor… so some of the impl would look something like this. public class MyFriendlyNamePropertyDescriptor : PropertyDescriptor { private PropertyDescriptor baseProp; private string friendlyName; public MyFriendlyNamePropertyDescriptor(PropertyDescriptor baseProp, Attribute[] filter) : base(baseProp) { this.baseProp = baseProp; } public override string Name { get{return this.baseProp.Name;} } public override string DisplayName { get { return GetFriendlyname(baseProp.Name); //replace with code to return a friendly name } } public override bool IsReadOnly { get {return baseProp.IsReadOnly;} } public override bool CanResetValue(object component) { return this.baseProp.CanResetValue(component); } public override Type ComponentType { get{return baseProp.ComponentType;} } public override object GetValue(object component) { return this.baseProp.GetValue(component); } public override Type PropertyType { get{return this.baseProp.PropertyType;} } public override void ResetValue(object component) { baseProp.ResetValue(component); } public override void SetValue(object component, object Value) { this.baseProp.SetValue(component, Value); } public override bool ShouldSerializeValue(object component) { return this.baseProp.ShouldSerializeValue(component); } } public class MyClass : ICustomTypeDescriptor { PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] filter) { PropertyDescriptorCollection baseProps = TypeDescriptor.GetProperties(GetType(), filter); // notice we use the type here so we don’t recurse PropertyDescriptor[] newProps = new PropertyDescriptor[baseProps.Count]; for (int i = 0; i < baseProps.Count; i++) { newProps[i] = new MyFriendlyNamePropertyDescriptor(baseProps[i], filter); string oldname = ((PropertyDescriptor)baseProps[i]).DisplayName ; string newname = ((MyFriendlyNamePropertyDescriptor)newProps[i]).DisplayName; } // probably wanna cache this… return new PropertyDescriptorCollection(newProps); } AttributeCollection ICustomTypeDescriptor.GetAttributes() { return TypeDescriptor.GetAttributes(this, true); } string ICustomTypeDescriptor.GetClassName() { return TypeDescriptor.GetClassName(this, true); } string ICustomTypeDescriptor.GetComponentName() { return TypeDescriptor.GetComponentName(this, true); } TypeConverter ICustomTypeDescriptor.GetConverter() { return TypeDescriptor.GetConverter(this, true); } EventDescriptor ICustomTypeDescriptor.GetDefaultEvent() { return TypeDescriptor.GetDefaultEvent(this, true); } EventDescriptorCollection ICustomTypeDescriptor.GetEvents(System.Attribute[] attributes) { return TypeDescriptor.GetEvents(this, attributes, true); } EventDescriptorCollection ICustomTypeDescriptor.GetEvents() { return TypeDescriptor.GetEvents(this, true); } PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty() { return TypeDescriptor.GetDefaultProperty(this, true); } PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties() { return TypeDescriptor.GetProperties(this, true); } object ICustomTypeDescriptor.GetEditor(System.Type editorBaseType) { return TypeDescriptor.GetEditor(this, editorBaseType, true); } object ICustomTypeDescriptor.GetPropertyOwner(System.ComponentModel.PropertyDescriptor pd) { return this; } // the code for MyClass…. including the properties to show in the propertygrid } (originally from [email protected] on microsoft.public.dotnet.framework.windowsforms. This code suggested by Rachid El Masoudi in an email to [email protected])