C# Design Question

  • Thread starter Thread starter Deanna Delapasse
  • Start date Start date
D

Deanna Delapasse

Hi,

This is probably a really dumb question, but I haven't done many C# Windows
apps - I've been living in the web world. What's the usual way of defining
some standard sets of fonts/size/colors for all the labels of a windows app?
I'll have many labels of 3 basic 'styles' and I don't want to go and set the
font size on each of them. I'd rather declare the 3 'styles' and attach the
labels to them. Like I'd do with a stylesheet in a web app.

thanks,
Deanna
 
Deanna Delapasse said:
Hi,

This is probably a really dumb question, but I haven't done many C# Windows
apps - I've been living in the web world. What's the usual way of defining
some standard sets of fonts/size/colors for all the labels of a windows app?
I'll have many labels of 3 basic 'styles' and I don't want to go and set the
font size on each of them. I'd rather declare the 3 'styles' and attach the
labels to them. Like I'd do with a stylesheet in a web app.

thanks,
Deanna

To accomplish something similar to this, I had to create inherited controls
for everything which should respond to the styles - basically every Win
control I intended to provide this support for. Rolling your own is about
the only way.

Best Regards,

Andy
 
There is another way:
- catch the ControlAdded-event of the form
- inside the eventhandler, set the font/size/colors of the added
control to your style
 
Sijin Joseph said:
Hi Tommy,

Will this work at Design Time?

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph

I don't know. I haven't tested it. I forgot 1 thing: only the direct
children of the form are affected. If you put a Panel on your form,
and on that panel, you put child-controls, those controls won't be
affected. To fix this, you can catch the ControlAdded-event of the
form (like before), and when a control (panel?) is added, catch the
ControlAdded-event of that control, and point it to the same method.
(Untested) Code below:

ControlEventHandler formControlAddedHandler = new
ControlEventHandler(FormControlAdded);
ControlEventHandler formControlRemovedHandler = new
ControlEventHandler(FormControlRemoved);

public void SetFormStyle(Form form)
{
form.ControlAdded += formControlAddedHandler;
form.ControlRemoved += formControlRemovedHandler;
}

private void FormControlAdded(object sender, ControlEventArgs e)
{
Control control = e.Control;
control.ControlAdded += formControlAddedHandler;
control.ControlRemoved += formControlRemovedHandler;

control.Font = myFont;
control.ForeColor = myForeColor;
control.BackColor = myBackColor;
}

private void FormControlRemoved(object sender, ControlEventArgs e)
{
RemoveHandlers(e.Control);
}

private void RemoveHandlers(Control control)
{
control.ControlAdded -= formControlAddedHandler;
control.ControlRemoved -= formControlRemovedHandler;
foreach(Control child in control.Controls)
RemoveHandlers(child);
}
 
Back
Top