|
-
Jun 28th, 2004, 02:10 PM
#1
Thread Starter
Lively Member
creating forms
I have 2 classes (Graphics circle, Graphics Item) Than I have a form that calls these classes and code to draw on this form. what do I need to do to use this code through out my program without using all of this code.
Public Class frmdraw
Inherits System.Windows.Forms.Form
' enums....
Public Enum GraphicsTools As Integer
CirclePen = 0
End Enum
Public Enum GraphicsSizes As Integer
Small = 4
Medium = 10
Large = 20
End Enum
' members.....
Public GraphicsItem As New ArrayList
Public GraphicTool As GraphicsTools = GraphicsTools.CirclePen
Public GraphicsSize As GraphicsSizes = GraphicsSizes.Large
Public GraphicColor As Color = Color.Black
' DoMousePaint - respond to a mouse movement....
Private Sub DoMousePaint(ByVal e As MouseEventArgs)
' store the new item somewhere...
Dim newItem As GraphicsItem
' what tool are you using?
Select Case GraphicTool
' circlepen?
Case GraphicTool.CirclePen
' create a new graphics circle...
Dim circle As New GraphicsCircle
circle.SetPoint(e.X, e.Y, GraphicsSize, GraphicColor, True)
' store this for addition...
newItem = circle
End Select
' where you given an item?
If Not newItem Is Nothing Then
' add it to the list...
GraphicsItem.Add(newItem)
'invalidate...
Invalidate()
End If
End Sub
Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
' is the button down?
If e.Button = MouseButtons.Left Then
DoMousePaint(e)
End If
End Sub
Protected Overrides Sub OnMouseMove(ByVal e As System.Windows.Forms.MouseEventArgs)
' is the button down?
If e.Button = MouseButtons.Left Then
DoMousePaint(e)
End If
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
' go through the list...
Dim item As GraphicsItem
For Each item In GraphicsItem
'ask each item to draw itself
item.Draw(e.Graphics)
Next
End Sub
Last edited by pea33nut; Jun 29th, 2004 at 11:45 AM.
-
Jun 28th, 2004, 02:34 PM
#2
Frenzied Member
I would put everything you can inside a class and just pass in references where applicable. I mean, if it is just one and two liners like most of that example seems to be, there isn't much code to be saved, and not much reason to anyways.
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
-
Jun 28th, 2004, 02:55 PM
#3
Thread Starter
Lively Member
Maybe im going about this all wrong. I have a small form with a textbox and an add button. The user can type in a number in the textbox and then click the add button. I want this to add the number to the database (which I already can do) and then load a form in which the user can draw on. This form needs to be linked with the number that the user ented in the textbox. so that the user can select the number later and it will bring up that form with the drawing on it.
What do you think??
-
Jun 28th, 2004, 03:09 PM
#4
Frenzied Member
Well, it sounds like you need to create forms on the fly and store them in some kind of collection (array, linked list, whatever you like). As part of that form class you are going to want to store your number. Then when you selecdt the number somehow later on, you are going to want to step through your collection, compare against the number, and show the form that matches.
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
-
Jun 28th, 2004, 03:11 PM
#5
Hyperactive Member
i didnt read that whole first part but in answer to the question i have quoted you can use a hashtable to hold the form and use the number as a key.
Originally posted by pea33nut
Maybe im going about this all wrong. I have a small form with a textbox and an add button. The user can type in a number in the textbox and then click the add button. I want this to add the number to the database (which I already can do) and then load a form in which the user can draw on. This form needs to be linked with the number that the user ented in the textbox. so that the user can select the number later and it will bring up that form with the drawing on it.
What do you think??
-
Jun 28th, 2004, 03:14 PM
#6
Thread Starter
Lively Member
yeah, you got any hints on starting to code this beast?? Never used a hashtable before.
-
Jun 28th, 2004, 03:26 PM
#7
Frenzied Member
Look in the help, they are really pretty simple. You create one, probably with what you are doing, the default capacity and load factor, and then you add elements to it by key and value like
Dim MyHashTable as New HashTable()
MyHashTable.Add(1,MyNewForm1)
Dim CurrentForm as MyForm1 = MyHashTable(1)
CurrentForm.Show()
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
-
Jun 28th, 2004, 03:50 PM
#8
Hyperactive Member
I would start with making the form that you will be drawing on work. look at the graphics object which is great for making a drawing program simmilar to paint.
-
Jun 28th, 2004, 03:53 PM
#9
Hyperactive Member
if you want an example go here:
http://www.wrox.com/WileyCDA/WroxTit...load_code.html
and download the code. chapter 14 is a simple drawing program
-
Jun 28th, 2004, 04:09 PM
#10
Thread Starter
Lively Member
I have a form that allows me to draw. Some how I need to create blank forms in my code that allows the user to draw on. Is there a way that I can just use the form that I have my drawing program on and then maybe the user can just save it as a new name?
-
Jun 28th, 2004, 04:15 PM
#11
Frenzied Member
Your new form is a class now...i.e. Class Form1. So in your code you can create new instances of it.
Dim NewForm as New Form1
MyHashTable.Add(1,NewForm)
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
-
Jun 28th, 2004, 04:34 PM
#12
Thread Starter
Lively Member
but it says that Form 1 is not defined
Dim NewForm As New Form1
Dim MyHashTable As New Hashtable
MyHashTable.Add(1, NewForm)
Dim CurrentForm As frmAddItemNumber = MyHashTable(1)
CurrentForm.Show()
-
Jun 29th, 2004, 12:38 AM
#13
Registered User
did you have a form that name form1?
if none.
VB Code:
dim newform as new (NameOfYourForm)
-
Jun 29th, 2004, 07:24 AM
#14
Thread Starter
Lively Member
Yes I do have a form1. this is the error that I get.
An unhandled exception of type 'System.InvalidCastException' occurred in AndersenAsi.exe
Additional information: Specified cast is not valid.
As this statement is highlighted
Dim CurrentForm As frmAddItemNumber = MyHashTable(1)
-
Jun 29th, 2004, 11:45 AM
#15
Thread Starter
Lively Member
So what do I need to do, make a class?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|