Answered by:
Windows Forms Consistent Look and Feel

Question
-
Hi,
I wold like to develop one application in Winforms using .NET 3.0 and C#. I work lot on Web Applications but not much on Windows Forms. In web applications, we have CSS to make sure all forms are same look and feel include Lablel, Drop down list, text boxes. But how can i maintain the same look and feel in Windows form across all forms. I dont want to go each and every control and set the properties. If i do so i need to spend more time on design than functionality. I hope there is better way to handle this.
Please advise me how to create a consistent look across all forms. If later i want to change color or font size, it should be easy.
Thanks
VijayTuesday, April 14, 2009 6:53 PM
Answers
-
If you know how to create a new form, make one and call it FormTemplate or something that tells you what it is used for. Add all of the stuff that you need for it to do or look like. Keep it simple.
Then you can create new forms by adding a new class file. In C#, you could start it like this.....
class StandardForm1 : TemplateForm
{
}
Just remember to define only one class per file. The Form Designer doesn't know what to do if it finds more than one class. It tends to ignore the extra classes.
Mark the best replies as answers. "Fooling computers since 1971."Tuesday, April 14, 2009 7:35 PM -
Hi Vijaykumar Yadavalli,
While in Web application, CSS can predefine the style and be used for div and other web element. In winform, no such predefine is available. You can do this with your custom controls.
For example, if you want to create all buttons in your application with the special background image. You can create MyButton inherited from System.Windows.Forms.Button. Set MyButton's BackgroundImage property. When you want to use it, drag MyButton instead of System.Windows.Forms.Button.
The Margin, Padding property of the button can similar to the CSS property. If you mean the Master Page of ASP.NET. You can use base form and inherited form in winform just as Rudedog2's suggestion.
Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.- Marked as answer by Kira Qian Tuesday, April 21, 2009 3:57 AM
Friday, April 17, 2009 3:23 AM -
Another alternative to throw out there is to use WPF rather than Winforms.
WPF was created with Web developers in mind. It was created so that the UI design and the UI behavior are completely separated. WPF uses a language called XAML which is not too different from HTML. It even has style functionality that works in the same way as CSS.Friday, April 17, 2009 8:29 PM -
Here is an article that may help if you decide to go with the WinForms base form technique.
http://www.code-magazine.com/articleprint.aspx?quickid=0403031&printmode=true
Instead of adding handlers for common events, you could modify this sample code to set standard control properties.
Otherwise I would go with the WPF recommendation. It is much more Web-like.Friday, April 17, 2009 9:00 PM
All replies
-
Create an abstract AMasterForm class, which could implement an interface IMasterForm, that all of your other forms inherit from.
Mark the best replies as answers. "Fooling computers since 1971."Tuesday, April 14, 2009 7:04 PM -
Thanks for your quick repley. Is it possible to give me some samples or steps to create a master Class to handle for Look and feel for Lable and Text Box.
If i created a master Class, do i need to make any changes when i drag and drop controls on to the Form or it automatically use Parent form poperties what we specified in the MasterForm once i inherit it.
VijayTuesday, April 14, 2009 7:29 PM -
If you know how to create a new form, make one and call it FormTemplate or something that tells you what it is used for. Add all of the stuff that you need for it to do or look like. Keep it simple.
Then you can create new forms by adding a new class file. In C#, you could start it like this.....
class StandardForm1 : TemplateForm
{
}
Just remember to define only one class per file. The Form Designer doesn't know what to do if it finds more than one class. It tends to ignore the extra classes.
Mark the best replies as answers. "Fooling computers since 1971."Tuesday, April 14, 2009 7:35 PM -
Implement the functionality in a separate "controller" class.
In this way, you can write new controllers to get different functionality out of the same form, or different forms.
Mark the best replies as answers. "Fooling computers since 1971."Tuesday, April 14, 2009 8:08 PM -
Hi Vijaykumar Yadavalli,
While in Web application, CSS can predefine the style and be used for div and other web element. In winform, no such predefine is available. You can do this with your custom controls.
For example, if you want to create all buttons in your application with the special background image. You can create MyButton inherited from System.Windows.Forms.Button. Set MyButton's BackgroundImage property. When you want to use it, drag MyButton instead of System.Windows.Forms.Button.
The Margin, Padding property of the button can similar to the CSS property. If you mean the Master Page of ASP.NET. You can use base form and inherited form in winform just as Rudedog2's suggestion.
Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.- Marked as answer by Kira Qian Tuesday, April 21, 2009 3:57 AM
Friday, April 17, 2009 3:23 AM -
Another alternative to throw out there is to use WPF rather than Winforms.
WPF was created with Web developers in mind. It was created so that the UI design and the UI behavior are completely separated. WPF uses a language called XAML which is not too different from HTML. It even has style functionality that works in the same way as CSS.Friday, April 17, 2009 8:29 PM -
Here is an article that may help if you decide to go with the WinForms base form technique.
http://www.code-magazine.com/articleprint.aspx?quickid=0403031&printmode=true
Instead of adding handlers for common events, you could modify this sample code to set standard control properties.
Otherwise I would go with the WPF recommendation. It is much more Web-like.Friday, April 17, 2009 9:00 PM