Re: Drawing lines - another noob question
- From: davetelling <davetelling@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 1 Sep 2006 10:33:02 -0700
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.
- Follow-Ups:
- Re: Drawing lines - another noob question
- From: Andy Bates
- Re: Drawing lines - another noob question
- References:
- Re: Drawing lines - another noob question
- From: Andy Bates
- Re: Drawing lines - another noob question
- Prev by Date: Re: advise for 3 tier OO beginner
- Next by Date: Re: When exactly is BeginReceive on a socket considered completed?
- Previous by thread: Re: Drawing lines - another noob question
- Next by thread: Re: Drawing lines - another noob question
- Index(es):
Relevant Pages
|