Hallo Manfred
Ich habe mich mal ein bisschen schlau gemacht und bin leider schon wieder stehen geblieben. Nun wollte ich um mein Control einen Rahmen zeichnen. Dazu habe ich ein neues Projekt gemacht (damit der andere Code nicht verwirrt).
Als Vorlage diente mir folgender Code von der MS Website:
' This example creates a PictureBox control on the form and draws to it.
' This example assumes that the Form_Load event handler method is connected
' to the Load event of the form.
Private pictureBox1 As New PictureBox()
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
' Dock the PictureBox to the form and set its background to white.
pictureBox1.Dock = DockStyle.Fill
pictureBox1.BackColor = Color.White
' Connect the Paint event of the PictureBox to the event handler method.
AddHandler pictureBox1.Paint, AddressOf Me.pictureBox1_Paint
' Add the PictureBox control to the Form.
Me.Controls.Add(pictureBox1)
End Sub 'Form1_Load
Private Sub pictureBox1_Paint(ByVal sender As Object, ByVal e As _
System.Windows.Forms.PaintEventArgs)
' Create a local version of the graphics object for the PictureBox.
Dim g As Graphics = e.Graphics
' Draw a string on the PictureBox.
g.DrawString("This is a diagonal line drawn on the control", _
New Font("Arial", 10), Brushes.Red, New PointF(30.0F, 30.0F))
' Draw a line in the PictureBox.
g.DrawLine(System.Drawing.Pens.Red, pictureBox1.Left, _
pictureBox1.Top, pictureBox1.Right, pictureBox1.Bottom)
End Sub 'pictureBox1_Paint Nun habe ich das Ganze umgebaut um einer Textbox einen Rahmen zu verpassen (nur als Beispiel, weil man den Rahmen bei einer Textbox schön sieht).
Mein Code:
Public Class Form1
Private TextBox1 As New TextBox()
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
AddHandler TextBox1.Paint, AddressOf Me.TextBox1_Paint
TextBox1.Location = New System.Drawing.Point(74, 98)
TextBox1.Size = New System.Drawing.Size(100, 20)
TextBox1.BorderStyle = System.Windows.Forms.BorderStyle.None
Me.Controls.Add(TextBox1)
End Sub
Private Sub TextBox1_Paint(sender As Object, e As _
System.Windows.Forms.PaintEventArgs)
Dim blackPen As New Pen(Color.FromArgb(255, 0, 0, 0), 1)
e.Graphics.DrawRectangle(blackPen, TextBox1.Location.X, _
TextBox1.Location.Y, TextBox1.Width, TextBox1.Height)
End Sub
End Class Das TextBox1_Paint wird einfach nicht ausgeführt, wobei das Event im Beispiel gleich ausgeführt wird, nachdem die Form initialisiert wurde.
Kann mir jemand helfen? Kann ich das Paint Event manuell aufrufen? Kann ich System.Windows.Forms.PaintEventArgs selbst erzeugen? Wie?
Freundliche Grüsse
Shivan |