Re: Drawing lines - another noob question
- From: davetelling <davetelling@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 1 Sep 2006 11:52:02 -0700
Andy,
I tried what you suggested, and I did end up with a control on my form that
had a red line through it. However, I now can't find a way to draw graphics
from within my existing form. How do I reference this control from (and I
struggle to know how to say this in OOP context) my main function/method? For
example, I have a method that would look at data in the various text boxes,
do some math, then draw lines in the graphics control based upon those data.
I got this to work using a picturebox control, using the line:
Graphics g = pictueBox1.CreateGraphics(); but if I substitute the name of my
new control for my picturebox, the application won't build. I keep getting
error messages that say that the control name (MyUserControl1) does not exist
in the current context.
Thanks for any light you can add to this!
Dave T.
P.S.
Here's the code for the buttonclick event that is supposed to do the drawing:
private void sendButton_Click(object sender, EventArgs e)
{
if (rpmIn.Text != "")
{
System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(System.Drawing.Color.Black);
Graphics g = pictureBox1.CreateGraphics();
float line_start = ((float.Parse(seg1startIn.Text) / 1000) *
40) + 35;
float line_end = (float.Parse(seg1endIn.Text) / 1000 * 40) +
35;
float line_height = (float)167 -
(float.Parse(seg1advIn.Text) * (float)3.2);
g.DrawLine(myPen, line_start, 167, line_end, line_height);
myPen.Dispose();
g.Dispose();
}
else
{
MessageBox.Show("No curve data to send. Read distributor
first.");
}
}
"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: How to properly get Handle of started process?
- Next by Date: Re: Send Mail Problem
- Previous by thread: Re: Drawing lines - another noob question
- Next by thread: Re: Drawing lines - another noob question
- Index(es):