how to draw a graph?
-
I've got what I'm sure is a trivial question. I want to draw a graph in Visual Basic on an object on a form. I would like to know what is the best object to use. I tried a panel as my first choice but in the properties I couldn't find something which would override the OnDraw and let me draw my own things. There must be a standard way to do this, but I don't use VB much so I'm not familiar with it. Thanks, Ilan
-
I've got what I'm sure is a trivial question. I want to draw a graph in Visual Basic on an object on a form. I would like to know what is the best object to use. I tried a panel as my first choice but in the properties I couldn't find something which would override the OnDraw and let me draw my own things. There must be a standard way to do this, but I don't use VB much so I'm not familiar with it. Thanks, Ilan
A Panel can be used, or a PictureBox. Either is good. You override the Paint method of the control you use. For example:
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles PictureBox1.Paint
Dim g As Graphics = e.Graphics
' do your painting for the control
End SubAnother way to do it, would be to draw to a Bitmap image and just set the PictureBox Image or Panel BackgroundImage property to the Bitmap you've drawn on.
Dave Kreskowiak Microsoft MVP - Visual Basic
-
I've got what I'm sure is a trivial question. I want to draw a graph in Visual Basic on an object on a form. I would like to know what is the best object to use. I tried a panel as my first choice but in the properties I couldn't find something which would override the OnDraw and let me draw my own things. There must be a standard way to do this, but I don't use VB much so I'm not familiar with it. Thanks, Ilan
It may well be that you want/need to do this from scratch, if, on the other hand, you don't want to reinvent the wheel http://www.codeproject.com/csharp/ZedGraph.asp
-
A Panel can be used, or a PictureBox. Either is good. You override the Paint method of the control you use. For example:
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles PictureBox1.Paint
Dim g As Graphics = e.Graphics
' do your painting for the control
End SubAnother way to do it, would be to draw to a Bitmap image and just set the PictureBox Image or Panel BackgroundImage property to the Bitmap you've drawn on.
Dave Kreskowiak Microsoft MVP - Visual Basic
I was about to ask you where to put that private sub, when I looked again at the properties and again found nothing. Then I double clicked on the pane and voila, the paint proceedure appeared! Sometimes things are so easy that I miss the truely obvious. Your Dim g as graphics would have been my next question, but you answered it. My only mistake was to put a semicolon at the end of the statement (habit is a hard thing to break). Thanks, Ilan
-
It may well be that you want/need to do this from scratch, if, on the other hand, you don't want to reinvent the wheel http://www.codeproject.com/csharp/ZedGraph.asp
I wanted to reinvent the wheel only enough to learn how to do something should I have to do so. Thus I tried Dave's suggestion to get a feel for things. Enough reinventing the wheel, and I will have a longer look at the ZedGraph. Presently it seems I have NET 1.1 so I can't use the latest version 5.0. (I have Visual Studio 2003 and don't want to update just for this.) Thanks, Ilan
-
I wanted to reinvent the wheel only enough to learn how to do something should I have to do so. Thus I tried Dave's suggestion to get a feel for things. Enough reinventing the wheel, and I will have a longer look at the ZedGraph. Presently it seems I have NET 1.1 so I can't use the latest version 5.0. (I have Visual Studio 2003 and don't want to update just for this.) Thanks, Ilan
-
I was about to ask you where to put that private sub, when I looked again at the properties and again found nothing. Then I double clicked on the pane and voila, the paint proceedure appeared! Sometimes things are so easy that I miss the truely obvious. Your Dim g as graphics would have been my next question, but you answered it. My only mistake was to put a semicolon at the end of the statement (habit is a hard thing to break). Thanks, Ilan
If you switch to code view, just above the window you'll find two dropdown boxes next to each other. Pick the control you want to handle an event for in the left side box, then pick the event/method/property you want to handle/override in the right side box. This will automatically create an empty event handler in your code, complete with the correct event header. You can also do the same thing with the Property window. Near the top of that window there is a small button with a little yellow lightning bolt in it. If it doesn't show up, click on the control you want in the designer, or pick it from the dropdown list just above the Properties window. When you click on that lightning bolt, the Properties window will switch to showing all the events exposed by that control. Find the event you want, then double-click in the white box to it's right and it'll create an empty event handler for you.
Dave Kreskowiak Microsoft MVP - Visual Basic
-
If you switch to code view, just above the window you'll find two dropdown boxes next to each other. Pick the control you want to handle an event for in the left side box, then pick the event/method/property you want to handle/override in the right side box. This will automatically create an empty event handler in your code, complete with the correct event header. You can also do the same thing with the Property window. Near the top of that window there is a small button with a little yellow lightning bolt in it. If it doesn't show up, click on the control you want in the designer, or pick it from the dropdown list just above the Properties window. When you click on that lightning bolt, the Properties window will switch to showing all the events exposed by that control. Find the event you want, then double-click in the white box to it's right and it'll create an empty event handler for you.
Dave Kreskowiak Microsoft MVP - Visual Basic
Great! I found it in the code view. Now that I know where it exists I'm in business. I don't see the lighting in the properties page. I'm used to using it all the time in c++, but I don't see it in Visual Basic. In fact, since I'm used to it in c++, this was the place I was looking. Never mind, so long as I know it exists in the code view, I'm in business. It does the dirty work of building the proper templates so they are right the first time around. Thanks again, Ilan
-
As you are most probably already aware, but I wasn't certain so I thought I would just say, you can download version 4.3.5 from the SourceForge project site[^] which works with .NET 1.1.
Alex, I am really impressed. I want to thank you for pointing out the code to me. I was just thinking of making a simple graph with some text and wanted to avoid reinventing the wheel. This isn't a wheel but the whole automobile! I see I can use it in c++, so I'll use it there as well. My trivial question in Visual Basic turned out to a super answer. Ilan