OnPaint-Ereignis für mehrere Formen?

Moin Moin,
vielleicht kann mir jemand helfen. ;o)
Ich möchte aus einer „Klasse(CformenFarbe.cs)“ heraus Farben
die in einem „switch (iStyle)“ definiert sind (in Form1) an weitere Formen übergeben. Der Button „FormFarbe“ befindet sich auf Form 1.
Wenn dieser gedrückt wird, sollen diese über die „Klasse(CformenFarbe.cs)“ auf allen Formen gleichzeitig wechseln.

Geht das so überhaupt???

Mit freundlichem Gruß
Andreas

////////////// Profekt FormFarbe ////////////////////

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;

namespace FormFarbe
{
///
/// Zusammendfassende Beschreibung für Form1.
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnFarbe;
private System.Windows.Forms.Button btnForm2;
private System.Windows.Forms.Button btnCancel;
///
/// Erforderliche Designervariable.
///
private System.ComponentModel.Container components = null;

/*** Startet Form 2***/
Form2 f2 = new Form2();

private int iStyle = 0;
private static LinearGradientBrush brush0;
private static SolidBrush brush1;
private static Pen pen;

public Form1()
{
//
// Erforderlich für die Windows Form-Designerunterstützung
//
InitializeComponent();

//
// TODO: Fügen Sie den Konstruktorcode nach dem Aufruf von InitializeComponent hinzu
//
}

///
/// Die verwendeten Ressourcen bereinigen.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
///
/// Erforderliche Methode für die Designerunterstützung.
/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
///
private void InitializeComponent()
{
this.btnFarbe = new System.Windows.Forms.Button();
this.btnForm2 = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnFarbe
//
this.btnFarbe.Location = new System.Drawing.Point(16, 24);
this.btnFarbe.Name = „btnFarbe“;
this.btnFarbe.Size = new System.Drawing.Size(88, 24);
this.btnFarbe.TabIndex = 0;
this.btnFarbe.Text = „FormFarbe“;
this.btnFarbe.Click += new System.EventHandler(this.btnFarbe_Click);
//
// btnForm2
//
this.btnForm2.Location = new System.Drawing.Point(16, 56);
this.btnForm2.Name = „btnForm2“;
this.btnForm2.Size = new System.Drawing.Size(88, 24);
this.btnForm2.TabIndex = 1;
this.btnForm2.Text = „Form2“;
this.btnForm2.Click += new System.EventHandler(this.btnForm2_Click);
//
// btnCancel
//
this.btnCancel.Location = new System.Drawing.Point(16, 88);
this.btnCancel.Name = „btnCancel“;
this.btnCancel.Size = new System.Drawing.Size(88, 24);
this.btnCancel.TabIndex = 2;
this.btnCancel.Text = „Cancel“;
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(464, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.btnCancel,
this.btnForm2,
this.btnFarbe});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
this.Name = „Form1“;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = „Form1“;
this.ResumeLayout(false);

}
#endregion

///
/// Der Haupteinstiegspunkt für die Anwendung.
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void btnCancel_Click(object sender, System.EventArgs e)
{
this.Close();
}

private void btnForm2_Click(object sender, System.EventArgs e)
{
f2.TopLevel = false;
f2.Parent = this;
f2.Dock = DockStyle.Right;
f2.Show();
}

private void btnFarbe_Click(object sender, System.EventArgs e)
{
switch(iStyle)
{
case 0:
this.Invalidate();
iStyle++;
break;
case 1:
this.Invalidate();
iStyle++;
break;
case 2:
this.Invalidate();
iStyle++;
break;
case 3:
this.Invalidate();
iStyle = 0;
break;
}
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
int breite;
breite = this.Width;
int hoehe;
hoehe = this.Height;

PointF[] points = { new Point(0, 0),
new Point(breite-6, 0),
new Point(breite-6, hoehe-22),
new Point(0, hoehe-22),
new Point(0, 0)};

if (pen != null) pen.Dispose();
if (brush0 != null) brush0.Dispose();
if (brush1 != null) brush0.Dispose();

switch (iStyle)
{
case 3:
pen = new Pen(Color.FromArgb(102, 102, 126), 2);
brush1 = new SolidBrush(Color.FromArgb(102, 102, 126));
brush0 = new LinearGradientBrush(new Rectangle(0, 0, breite, hoehe), Color.FromArgb(242, 241, 243), Color.FromArgb(204, 206, 210), 90.0f);

break;

case 1:
pen = new Pen(Color.FromArgb(0, 60, 116), 2);
brush1 = new SolidBrush(Color.FromArgb(0, 60, 116));
brush0 = new LinearGradientBrush(new Rectangle(0, 0, breite, hoehe), Color.FromArgb(244, 243, 238), Color.FromArgb(213, 208, 238), 90.0f);
break;

case 0:
//break;
case 2:
//break;
pen = new Pen(Color.FromArgb(128, 128, 64), 2);
brush1 = new SolidBrush(Color.FromArgb(128, 128, 64));
brush0 = new LinearGradientBrush(new Rectangle(0, 0, breite, hoehe), Color.FromArgb(236, 233, 216), Color.FromArgb(220, 215, 184), 90.0f);
break;
}

float[] relativeIntensities = {0.0f, 0.3f, 1.0f};
float[] relativePositions = {0.0f, 0.7f, 1.0f};

Blend blend = new Blend();
blend.Factors = relativeIntensities;
blend.Positions = relativePositions;
brush0.Blend = blend;

e.Graphics.FillRectangle(brush0, 0, 0, breite, hoehe);
e.Graphics.DrawLines(pen, points);

}
}
}
////////////////Ende Form1//////////////
////////////////Beginn Form2//////////////
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace FormFarbe
{
///
/// Zusammendfassende Beschreibung für Form2.
///
public class Form2 : System.Windows.Forms.Form
{
///
/// Erforderliche Designervariable.
///
private System.ComponentModel.Container components = null;

public Form2()
{
//
// Erforderlich für die Windows Form-Designerunterstützung
//
InitializeComponent();

//
// TODO: Fügen Sie den Konstruktorcode nach dem Aufruf von InitializeComponent hinzu
//
}

///
/// Die verwendeten Ressourcen bereinigen.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
///
/// Erforderliche Methode für die Designerunterstützung.
/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
///
private void InitializeComponent()
{
//
// Form2
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.Color.DeepSkyBlue;
this.ClientSize = new System.Drawing.Size(232, 273);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = „Form2“;
this.Text = „Form2“;

}
#endregion
}
}

////////////////Ende Form2//////////////
////////////////Beginn CformenFarbe.cs//////////////
using System;

namespace FormFarbe
{
///
/// Zusammendfassende Beschreibung für CformenFarbe.
///
public class CformenFarbe
{
public CformenFarbe()
{
//
// TODO: Fügen Sie hier die Konstruktorlogik hinzu
//
}
private int myFormFarbe;
public static int FormStyle;

public int Farbe
{
set
{
myFormFarbe = value;
}
get
{
return myFormFarbe;
}
}

}
}

////////////////Ende CformenFarbe.cs//////////////

Hola

Da gibt es viele möglichkeiten wie man dies implementieren kann. Am elegantesten finde ich es über einen Singleton (http://www.yoda.arachsys.com/csharp/singleton.html)

Form1 teilt der CformFarbe (dein singleton) welche farbe gewählt wurde, und der singleton schmeisst dann ein event, welches von allen anderen forms die darauf reagieren sollen, abboniert wird.

gruss

[Bei dieser Antwort wurde das Vollzitat nachträglich automatisiert entfernt]

Hallo Guiseppe,
erst einmal Danke für die schnelle Antwort. !!!
Da ich in C-sharp" noch nicht so fit bin, habe ich die Bitte
(falls es Ihre Zeit erlaubt), daß Sie mir anhand des Quellcodes
aufzeigen wie ich das mit der „Singleton.cs“ mache???

Danke im Voraus
Andreas

hallo

habs dir eine demo gemailt.

gruss

[Bei dieser Antwort wurde das Vollzitat nachträglich automatisiert entfernt]