472,364 Members | 2,123 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,364 software developers and data experts.

using g.DrawRectangle(pen,rect) to draw a rectangle on an object

Hi there. I have written a small procedure to draw various shapes on
things. A bit of it is shown below.

Private Sub drawShape(ByVal shapeType As Integer, ByRef g As Graphics)
Select Case shapeType
Case 1 : g.DrawRectangle(New Pen(Color.Black), 0, 0, 50, 10)
Case 2 'draw a circle
Case 3 'draw a triangle
Case 4 'draw other shape
Case 5 'draw other shape
Case 6 'draw other shape
Case Else 'etc. draw other shapes
End Select
End Sub

If on Form1, I override the OnPaint method with

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
drawShape(1, e.Graphics)
End Sub

it paints my rectangle as I would expect drawing it on Form1 at 0,0
with width 50 and height 10.

I have lots of other controls on the form, such as buttons, labels,
groupboxes etc. My question is how do I get 'g' (a valid Graphics
instance) for the buttons, labels, groupboxes and other things.

The obvious thing I tried below doesn't work (I see no rectangle on
the GroupBox, called GroupBox1) but still see the rectangle on the
form. I want to see both.

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
drawShape(1, e.Graphics)
drawShape(1, Me.GroupBox1.Creategraphics())
End Sub
Thank you in advance for all your help and sorry to post more of these
verbose and tricky questions.
Colin
Nov 20 '05 #1
3 10187
Colin - you have come a long way judging by the type of questions you are
now asking.

I'm not going to answer your question directly but provide you with some
information that should enable you to work out the answer to your question
yourself.

If Panel1 is a Panel on your form, TabControl1 is a TabControl on your form,
and Button1 is a Button on your form, when OnPaint is executed

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
Dim g As Graphics = Panel1.CreateGraphics()
drawShape(1, g)
g.Dispose()
g=TabControl1.CreateGraphics()
drawShape(1,g)
g.Dispose()
g=Button1.Creategraphics()
drawShape(1,g)
g.Dispose()
End Sub

you will see your blue rectangle on both Panel1 and TabControl1, but not
Button1. Ask yourself why the rectangle draws on some form objects but not
others - what is different about them and how would/could you change the
observed behaviour.

Regards
Hexathioorthooxalate
PS: Here's an excerpt from the on-line (VB.NET 2003) docs for
CreateGraphics. Quote: "The returned Graphics object must be disposed
through a call to its Dispose method when it is no longer needed.". Your use
of drawShape(1, Me.GroupBox1.Creategraphics()) doesn't and can't dispose of
the graphics object as written.


"Colin McGuire" <co***********@lycos.co.uk> wrote in message
news:ab**************************@posting.google.c om...
Hi there. I have written a small procedure to draw various shapes on
things. A bit of it is shown below.

Private Sub drawShape(ByVal shapeType As Integer, ByRef g As Graphics)
Select Case shapeType
Case 1 : g.DrawRectangle(New Pen(Color.Black), 0, 0, 50, 10)
Case 2 'draw a circle
Case 3 'draw a triangle
Case 4 'draw other shape
Case 5 'draw other shape
Case 6 'draw other shape
Case Else 'etc. draw other shapes
End Select
End Sub

If on Form1, I override the OnPaint method with

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
drawShape(1, e.Graphics)
End Sub

it paints my rectangle as I would expect drawing it on Form1 at 0,0
with width 50 and height 10.

I have lots of other controls on the form, such as buttons, labels,
groupboxes etc. My question is how do I get 'g' (a valid Graphics
instance) for the buttons, labels, groupboxes and other things.

The obvious thing I tried below doesn't work (I see no rectangle on
the GroupBox, called GroupBox1) but still see the rectangle on the
form. I want to see both.

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
drawShape(1, e.Graphics)
drawShape(1, Me.GroupBox1.Creategraphics())
End Sub
Thank you in advance for all your help and sorry to post more of these
verbose and tricky questions.
Colin

Nov 20 '05 #2
Hex - thanks for your help.

The Panel etc are .Net controls, and the Button is a Windows control?
But I still want to draw on the Button too. How?
Thank you
Colin.
Nov 20 '05 #3
Hex isnt online today. Could someone else help me out here please.

I can see that rectangles are only drawn on objects that are not
Windows controls (such as buttons and textboxes).

How can I tell as a general rule whether the object is drawn by the
framework and not by Windows? Currently a "GroupBox" is drawn by the
framework but what if Microsoft modify Windows so that it is drawn
there - then my code won't work. So what I think I want it a
subroutine to pass a object (like a button, or a panel, or a
groupbox), and the subroutine print out whether the object is drawn by
Windows or the framework (and therefore whether my blue rectangle,
code below, will be drawn).
Thank you
Colin
"Hexathioorthooxalate" <ru***@spameremove.clara.co.uk> wrote in message news:<10****************@damia.uk.clara.net>...
Colin - you have come a long way judging by the type of questions you are
now asking.

I'm not going to answer your question directly but provide you with some
information that should enable you to work out the answer to your question
yourself.

If Panel1 is a Panel on your form, TabControl1 is a TabControl on your form,
and Button1 is a Button on your form, when OnPaint is executed

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
Dim g As Graphics = Panel1.CreateGraphics()
drawShape(1, g)
g.Dispose()
g=TabControl1.CreateGraphics()
drawShape(1,g)
g.Dispose()
g=Button1.Creategraphics()
drawShape(1,g)
g.Dispose()
End Sub

you will see your blue rectangle on both Panel1 and TabControl1, but not
Button1. Ask yourself why the rectangle draws on some form objects but not
others - what is different about them and how would/could you change the
observed behaviour.

Regards
Hexathioorthooxalate
PS: Here's an excerpt from the on-line (VB.NET 2003) docs for
CreateGraphics. Quote: "The returned Graphics object must be disposed
through a call to its Dispose method when it is no longer needed.". Your use
of drawShape(1, Me.GroupBox1.Creategraphics()) doesn't and can't dispose of
the graphics object as written.


"Colin McGuire" <co***********@lycos.co.uk> wrote in message
news:ab**************************@posting.google.c om...
Hi there. I have written a small procedure to draw various shapes on
things. A bit of it is shown below.

Private Sub drawShape(ByVal shapeType As Integer, ByRef g As Graphics)
Select Case shapeType
Case 1 : g.DrawRectangle(New Pen(Color.Black), 0, 0, 50, 10)
Case 2 'draw a circle
Case 3 'draw a triangle
Case 4 'draw other shape
Case 5 'draw other shape
Case 6 'draw other shape
Case Else 'etc. draw other shapes
End Select
End Sub

If on Form1, I override the OnPaint method with

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
drawShape(1, e.Graphics)
End Sub

it paints my rectangle as I would expect drawing it on Form1 at 0,0
with width 50 and height 10.

I have lots of other controls on the form, such as buttons, labels,
groupboxes etc. My question is how do I get 'g' (a valid Graphics
instance) for the buttons, labels, groupboxes and other things.

The obvious thing I tried below doesn't work (I see no rectangle on
the GroupBox, called GroupBox1) but still see the rectangle on the
form. I want to see both.

Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)
drawShape(1, e.Graphics)
drawShape(1, Me.GroupBox1.Creategraphics())
End Sub
Thank you in advance for all your help and sorry to post more of these
verbose and tricky questions.
Colin

Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Joe Kelsey | last post by:
When you use addEventListener (or addEvent in IE) to call an object method, does it call it with the correct this parameter? The ECMAScript reference has a lot to say about the caller using...
7
by: William Apple | last post by:
Despite the fact this deals with webservices I believe it is a VB question. I am working on a test application that passes data to a webservice. The webservices takes a variable type that is...
1
by: Ram | last post by:
Hi All, I am using Object Data Source to bind data in the gridview. I have set the property AllowSorting=true. While running the application, I could sort the data only in ascending order. Is...
6
by: burningodzilla | last post by:
Hi all - I'm preparing to dive in to more complex application development using javascript, and among other things, I'm having a hard time wrapping my head around an issues regarding "inheritance"...
0
by: sajithamol | last post by:
I have a dll of a VB application which performs image manipulations. I want to build this dll file into a .cab file to embed this in the Object tag in client side. Also how to generate the classid...
2
by: Veloz | last post by:
Hiya My question is whether or not you should associated related objects in your software using a scheme of id's and lookups, or wether objects should actually hold actual object references to...
1
by: busiswa | last post by:
Hi All, I am just new in PHP, i am asked to draw a combobox using the draw() method. Can someone help me on how to go about that. Thank you, Busi
9
by: Duggi | last post by:
I used to wonder why MS implemented C# to accept the following code using (Font f = new Font()) { // some code here. } While the same can be achieved through {
12
by: Duggi | last post by:
I used to wonder why MS implemented C# to accept the following code using (Font f = new Font()) { // some code here. } While the same can be achieved through {
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.