Wednesday, July 30, 2008

Setting the Form Icon to an Application Resource in VB.NET

I have an application with a main form and many dialogs. I want the main form and all of the dialogs to use the same icon in the forms' title bars. So, instead of defining the Icon property on each form, I have created an application resource and added a line of code to each Form_Load method.

First, add an Icon to your Project Resources (Project/Properties then on the Resource tab). Remember the name of the icon resource you create. Mine is AppIcon in this example.

Then for each Form_Load method in the VB.NET app (for the main form and each dialog's form), I add the following:

Me.Icon = My.Resources.AppIcon

There may be even better ways to do this, but this is the simplest I have found so far.

For more information on using resources, see:

http://visualbasic.about.com/od/usingvbnet/a/ResVBNET.htm

Tuesday, July 29, 2008

Keeping a Nonmodal form on top of App, but not on top of Everything

It took me a while to figure this one out, but the solution is pretty simple.

I want to keep a nonmodal form on top of my main app form. I have tried using:

Dlg.TopMost = True

in the nonmodal form (Dlg), but this approach keeps the form on top of everything, not just my main app form.

The solution is:

Dlg.Owner = Me ' Me is the main app form
Dlg.TopMost = False



More info can be found at:

http://www.syncfusion.com/faq/windowsforms/faq_c95c.aspx#q975q

Thursday, July 24, 2008

Changing a Control's Font Size at Runtime in VB.NET

If you have a control that is using a font with size 8.25 (which was defined at design-time), then at runtime you want to change it to a different size (such as 12), you cannot simply say:

ctrl.Font.Size = 12 (this will not work - Property Size is Read-Only)

However, you can replace the whole font:

ctrl.Font = new Font(ctrl.Font.FontFamily, 12, ctrl.Font.Style)

For more info and examples in C#, see: http://bytes.com/forum/thread261450.html