Hi!
Ich schreibe gerade ein Textbearbeitungsprogramm und habe folgendes schon (die combobox, mit der soll man bei der richtextbox1 die hintergrundfarbe wählen können)
Mein Projekt (du brauchst : Form1 (fenster), Combobox(heißt cmbColor) und richtextbox )
Imports System
Imports System.ComponentModel
Public Class UserControl1
Inherits System.Windows.Forms.UserControl
Private mSelectedColor As Color
Private mCTRL As Control
Private strColor() As String
Public Event ColorChanged(ByVal col As Color)
' --> Das Description Attribut legt fest welcher Text im Infoteil
' --> des Property Grid gezeigt wird wenn die Eigenschaft ausgewählt wird
<Description("Gibt die ausgewählte Farbe zurück")> _
Public ReadOnly Property SelectedColor() As Color
Get
Return mSelectedColor
End Get
End Property
<Description("Bindet ein Buddy Conrol an die ColorCombo")> _
Public Property BuddyCTRL() As Control
Get
Return mCTRL
End Get
Set(ByVal Value As Control)
mCTRL = Value
End Set
End Property
Private Sub GetKnownColor()
Dim kc As New KnownColor
strColor = System.Enum.GetNames(kc.GetType())
For i As Integer = 0 To strColor.GetUpperBound(0)
Me.cmbColor.Items.Add(strColor(i))
Next
End Sub
Private Sub ColorItem(ByVal ItemGraphics As Graphics, _
ByVal ItemRectangle As Rectangle, ByVal ItemIndex As Integer)
' --> Der Name des Farbeintrages
Dim ItemText As String = strColor(ItemIndex)
' --> Die benötigten Objekte zum zeichnen
Dim ColorBrush As New SolidBrush(System.Drawing.Color.FromName( _
ItemText))
Dim Pen As New Pen(System.Drawing.Color.Black, 1)
Dim TextBrush As New SolidBrush(System.Drawing.Color.FromKnownColor( _
System.Drawing.KnownColor.MenuText))
' --> der Zeichenvorgang
With ItemGraphics
.FillRectangle(ColorBrush, ItemRectangle.Left + 2, _
ItemRectangle.Top + 2, 20, ItemRectangle.Height - 4)
.DrawRectangle(Pen, New Rectangle(ItemRectangle.Left + 1, _
ItemRectangle.Top + 1, 21, ItemRectangle.Height - 3))
.DrawString(ItemText, Me.cmbColor.Font, TextBrush, _
ItemRectangle.Left + 28, _
ItemRectangle.Top + (ItemRectangle.Height - _
Me.cmbColor.Font.GetHeight()) / 2)
End With
' --> GDI Objekte sind sehr speicherintensiv, deshalb entledigen wir
' uns dieser sofort
TextBrush.Dispose()
ColorBrush.Dispose()
Pen.Dispose()
End Sub
Private Sub cmbColor_DrawItem(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles cmbColor.DrawItem
If e.Index <> -1 Then Call ColorItem(e.Graphics, e.Bounds, e.Index)
End Sub
Private Sub cmbColor_SelectedIndexChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles cmbColor.SelectedIndexChanged
mSelectedColor = Color.FromName(Me.cmbColor.SelectedItem)
If Not mCTRL Is Nothing Then mCTRL.BackColor = mSelectedColor
RaiseEvent ColorChanged(mSelectedColor)
End Sub
Private Sub UserControl1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
GetKnownColor()
End Sub
Private Sub UserControl1_Resize(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Resize
With cmbColor
.Left = 0
.Top = 0
.Height = Me.Height
.Width = Me.Width
End With
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
End Sub
End ClassSo und meine Frage: wie kann ich die ausgewählte farbe von cmbColor (also von meiner combobox)
auf meine richtextbox als background übertragen????
danke im voraus! |