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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Drawing a line on form load

Status
Not open for further replies.

Chopstik

Technical User
Oct 24, 2001
2,180
US
Ok, I spent an hour or two (or more ;-)) banging my head on the desk trying to figure out how to draw a line on a form when it was loading only to discover that it is not possible (because the form is not yet loaded at that time so it is not possible to draw something at that point). So, as a quick test, I used the form paint sub as follows:
Code:
Private Sub frmReport_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
     ' Create a Pen object.
   Dim pen As New Drawing.Pen(System.Drawing.Color.Black, 1)
   Me.CreateGraphics.DrawLine(pen, 0, 60, 1000, 60)
   pen.Dispose()
End Sub
This works ok for the first line, but I am going to have a form that is going to have many dynamic objects (essentially everything below the first title line will be dynamic). What I'm trying to wrap my head around is a method that I can use to create the lines where I will actually need them (the example above is just the line under the headers at the top of the page). I am listing out a series of floors by building and want to separate each floor by a line in between. But, since I don't know how many occupants will be on a given floor, I have to draw the line after I've created the floor/building as a whole. Would I just create a new function that will be called after the form is visible and, if so, how would I reference the spots where the lines will need to be?

I just want to understand what I need to do so any advice or links would be great. I'm using VS2003 and .NET 1.1 if that makes any difference. Thanks!

------------------------------------------------------------------------------------------------------------------------
"Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."
- Winston Churchill
 
Keep an array, collection, arraylist, etc. of where each line needs to go (x & y coordinates). Then, when your paint event is called, you'll loop through your array and draw all of your lines onto an in memory bitmap. You'll then draw the in memory bitmap onto the event's supplied graphics.
 
You could create a user control to hold each 'floor', put your line at the top of the user control just as you have here, then fill the control. Once you have all of the controls populated, add them to the form and you should be good to go.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Rick, feel free to talk down to me. When you say to use a "user control" to hold each floor, can you elaborate? Bear in mind that I am learning as I go so I'm just kind of making it up as I go along... Thanks!

------------------------------------------------------------------------------------------------------------------------
"Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."
- Winston Churchill
 
Dear mister Chopstik,

A usercontrol is a class that inherits from system.windows.forms.control.

The usercontrol has most things built in so that it is easy to get it on a form. A form BTW is also a control (with a twist).

You could have differnt usercontrols. On being the header another being the floor. You give the usercontrol properies so that the user can adjust them to their liking.

The usercontrol(s) would have the line as above in their paint event. You could paint everything in the paintevent.

Hope I wasn't to helpfull.

And now go along.

I think I can hear your mother-in-law calling.

Christiaan Baes
Belgium

"My old site" - Me
 
Pretty much what Christiaan said. Create a new class, inherit it from windows.forms.controls (or something like that). Put your line drawing in the paint event, and add what ever controls to it you had been adding to the form to draw the 'floor'.

Then, back on your form, add a new instance of the user control to the form's controls collection. Call what ever methods you need on the control to populate it, then set it's x and y position.

This should allow you to break a lot of the work down to the 'floor' level. So that you don't have to track different floors in your process. The form only has to worry about self contained floor objects.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
I actually went to look it up on MSDN after I asked (I know, as opposed to "before" I asked) but I think I get the general idea... I'll have to play with it a bit (since it is all still rather new to me) and see how far I get... Thanks for all of the responses (you too, RiverGuy!).

Oh, and Christiaan, that wasn't the mother-in-law calling me yesterday, that was the wife... For whom I used a vacation day to repaint the kitchen... [lol]

------------------------------------------------------------------------------------------------------------------------
"Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."
- Winston Churchill
 
SBendBuckeye said:
Too bad you couldn't just call the Kitchen.Paint event
[lol] Now there's a thought!

------------------------------------------------------------------------------------------------------------------------
"Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."
- Winston Churchill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top