Re: Drawing lines - another noob question
- From: "Andy Bates" <andy@xxxxxxxxxx>
- Date: Fri, 1 Sep 2006 20:13:53 +0100
It's a difficult question to answer regarding OOP. Yes you can still write
simple procedural C type applications but it makes it difficult to use the
..NET framework effectively (you'll effectively be working against it).
OOP provides a lot of power and initially can seem quite daunting but once
you appreciate the way it allows you to work then it makes life a lot
easier.
There are obviously various elements to OOP, including encapsulation and
polymorphism.
In C you would declare structures and global variables to be your data. You
would then write functions to manipulate the data. There is no control so
any function can modify any data so the two are seperate; bugs can be a real
nightmare to track down due to this.
At the simplest level OOP combines the two so data and functionality exist
together. You create a class and the methods it exposes allow you to work
with the class and manipulate the data (which generally should be private to
the class)..
Advantages that this offers is i) the functionality can be re-implemented
without impacting the users (if the interface doesn't change), ii) access to
the data is through a controlled mechanism (the classes interface).
Polymorphism says okay you can now define an abstract class with methods
that have to be implemented (say a shape) with a Draw method. You then
derive shapes from this base class and because the root template stipulates
that the class must have a Draw method you have to implement it. Then when
you have a Shape reference you can always ask it to draw itself; you don't
need to care what the shape is. So it aids extensibility. In this instance a
Shape class wouldn't make sense to instantiate on its own so you'd probably
make it abstract (i.e. so you cannot do new Shape() as it doesn't make
sense).
Interfaces allow you to control polymorphism so that you don't create huge
classes that have the same disparate functionality listed over and over. You
inherit your class from an interface and you then have to implement the
methods that the interface requires (this is the same interface for all
classes that derive from it, so you can query whether an object implements
an interface via if (o is IComparable)...).
You mention that you can't do globals; several solutions to this:
1. Create a singleton.
2. Add a public static field to your own class.
3. Create a class called Globals (name can be changed) and only add static
fields to it (Math class is a prime example but this just contains static
methods).
If you get the model of your application right then all of the pieces fall
into place and the coding becomes a pleasure; however if you get the model
wrong then it can also become a nightmare to work with.
It probably makes sense to start out slowly and try some simple modelling
using OO and then build on it. Once you've tried it I'm 100% confident that
you will not go back to procedural coding which C enforces. It's a case of
biting the bullet and trying it...
- Andy
"davetelling" <davetelling@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:0F2A5986-8E46-4225-91FC-D789DA18BC58@xxxxxxxxxxxxxxxx
Andy,
Thanks for the detailed reply. I have to admit much of it does not make
sense to me just reading through it, but I'll give it a try when I have
the
chance. I was finally able to get the picturebox writing method to work.
The
problem was that I was not "triggering" the call to the line drawing
function
properly, so it never ran. I finally was able to step through and find
that I
never reached that portion of code, and now (so far!) I think I can do
what I
want to do.
As a side note - how long does it normally take to get used to this OOP
thing? I have dabbled in C for several years, and I find it confusing and
frustrating to not be able to do things (such as easily declare global
variables, which I use quite a bit in my microcontroller programs) in C#.
I'm
hoping that, as time goes on, the process will become less painful!
Thanks again!
"Andy Bates" wrote:
Probably not the most appropriate control to use; the picture box is for
displaying images.
To use the picture box you would have to create an image that contained
the
changes you wanted and give it to the picture box. Could be useful if you
wanted to save the image (say for a web page) but not the most efficient
way
to go, especially if the data is dynamic (as it sounds yours is).
I'd suggest using a user control. This is a visual control which can be
used
to group common controls and functionality or provide custom drawing etc.
User controls can contain the common toolbox elements and also other user
controls so they are very powerful. The other advantage is that if you
put
them into their own assembly and ensure that the encapsulation is correct
you can share the control betwen multiple projects very easily (if you
need
the same functionality in multiple places).
To do this:
1. Open or create a Windows Forms application and build it.
2. Right click on the project in Solution Explorer and select Add->User
Control. Enter a name i.e. MyUserControl.cs and click OK.
3. A blank control will open up.
4. In the properties, click on the lightning bolt (for events). Scroll
down
to Paint and double click on it. This will create a Paint event.
5. In this event place the following code (method signature etc. is shown
but just paste the line in the middle):
private void MyUserControl_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawLine(new Pen(Brushes.Red), new Point(0, 0), new
Point(this.Width,
this.Height));
}
6. Build your project.
7. Open the applications main form.
8. On the toolbox on the left hand side (enable via View.Toolbox if not
present) look at the top and you should see a section called "<<AppName>>
Components" and in here should be the component (MyUserComponent) that
you
created in step 2 above. Select this and drop it onto the form.
9. The control can now be docked or positioned on the form as you
require.
10. Build and run the application. The control will appear with a red
line
running through it.
This is just an example, you obviously need to revise the painting above
and
add controls etc. to the user control as you application requires but it
should get you started.
HTH
- Andy
"davetelling" <davetelling@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:47FC52F7-932D-4F97-8EC0-51A782F3DDC6@xxxxxxxxxxxxxxxx
I am not a programmer, I'm an engineer trying to make an interface to a
product I'm designing. I have used C# to make a form that interrogates
the
unit via the serial port and receives the data. I want to be able to
draw
lines in a picturebox based upon certain data points I have received. I
dragged a picturebox from the toolbar onto my form, but after having
gone
through the help files, looking online and trying a variety of things,
I
absolutely cannot seem to draw lines on the picturebox. The sample code
in
the help files seems to assume that I knwo more about the process than
I
do,
as I can find nothing that says, "How to draw lines in a picturebox". I
was
able to successfully add a background image, but I'm also wondering if
this
graphic data has to accompany the application, or if it gets "embedded"
into
the actual application?
At any rate, is there a tutorial that gives some explicit instructions
as
to
how to use a method (not part of the picturebox click event handler) to
write
to the picturebox. If I can't do this, then how can I easily do it? The
lines
have to be able to be easily updated when new data are read in.
.
- References:
- Re: Drawing lines - another noob question
- From: Andy Bates
- Re: Drawing lines - another noob question
- From: davetelling
- Re: Drawing lines - another noob question
- Prev by Date: Re: Send Mail Problem
- Next by Date: Re: DataSet.Tables[0]
- Previous by thread: Re: Drawing lines - another noob question
- Next by thread: Re: Drawing lines - another noob question
- Index(es):
Relevant Pages
|