Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

variable declaration

Status
Not open for further replies.

kulfanumber

Programmer
Jun 24, 2009
21
0
0
PK
Hi, How can i declare a system.drawing.graphics variable. when i declare it as
Dim a as System.Drawing.Graphics it gives null exception.
 
You need to give the exact error and/or more of the code because as it is that isn't wrong.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Hello kulfanumber

you need to assign to the 'a' variable the Graphics. So you forgot the '=' part.

Examples:
1. Dim a As System.Drawing.Graphics = Me.CreateGraphics
2. Dim a As System.Drawing.Graphics = e.Graphics <-- inside in a Paint Event for example.
 
Thnx TipGiver. Actually i wanted to change the border of rtb. For this i hav created my own rtb class

Public Class RichTextBoxEx
inherits RichTextBox

Public Sub DrawBorder(ByVal Border as System.Drawing.Graphics)
Me.BorderStyle = Windows.Forms.BorderStyle.None
Dim rect as Rectangle
rect.width = me.width
rect.height = me.height
rect.Location = me.location
rect.inflate(1, 1)
border.DrawRectangle(New Pen(color.Black), rect)
End Sub

then i have used onpaint method to implement this
Me.DrawBorder(e.Graphics)

But its not working. the OnPaint method is never called.
 
You need this:

Code:
Public Sub New()
    Me.SetStyle(Windows.Forms.ControlStyles.AllPaintingInWmPaint, True)
    [green]Me.SetStyle(Windows.Forms.ControlStyles.UserPaint, True)[/green]
    Me.SetStyle(Windows.Forms.ControlStyles.OptimizedDoubleBuffer, True)
    Me.SetStyle(Windows.Forms.ControlStyles.ResizeRedraw, True)
End Sub
The only one you have to have is in green, but I strongly suggest all of them.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Some commands are put into the construcotr of the rectangle, so there are lesser lines. Not also that the rect is NEW. (so that i use the constructor)

Code:
    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)

        Dim g As Graphics = e.Graphics

        Dim rect As New Rectangle(Me.Location, Me.Size)
        rect.Inflate(1, 1)
        g.DrawRectangle(Pens.Black, rect)

        MyBase.OnPaint(e)

    End Sub

    Public Sub New()

        Me.BorderStyle = Windows.Forms.BorderStyle.None
        Me.SetStyle(Windows.Forms.ControlStyles.AllPaintingInWmPaint, True)
        Me.SetStyle(Windows.Forms.ControlStyles.UserPaint, True)
        Me.SetStyle(Windows.Forms.ControlStyles.OptimizedDoubleBuffer, True)
        Me.SetStyle(Windows.Forms.ControlStyles.ResizeRedraw, True)
        Me.SetStyle(ControlStyles.UseTextForAccessibility, True)

    End Sub
 
i hav tried this. It draws rectangle around rtb but when i type nothing happens. What maybe the problem?
 
For some controls if you override the OnPaint then you have to handle all of the painting for the control. That seems to be the case with this. I think it has to do with controls that are actually still com controls and what you are dealing with in .Net is just a wrapper for it, but I'm not 100% sure.

In any case it looks like you are going to have to handle the ALL the drawing required by the control yourself. While putting text on the screen is as simple as .DrawString before you .DrawRectangle what isn't simple is if you allow all the other functions of a RichTextBox. This is going to quickly get more and more complicated.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Yes, the text is "written" but not displayed. You can select the invisible text and paste it in a regural control. The text exists, so it is a matter of drawing it.
 
I tried drawing border in the form's paint event instead of rtb's onpaint event and it worked. Thnx
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top