How in VB .NET to correct errors?

D

Dr. Zharkov

Hello. A problem in the following.
In VB .NET 2003 we create the project and in file Form1.vb the following
code is written down:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Create the data:
CreateData() 'Error
End Sub

'Create the data.
Private Sub CreateData()
'Axes X, Y, Z:
MakeSegment(0, 0, 0, 0.5, 0, 0) ' X axis. 'Error
MakeSegment(0, 0, 0, 0, 0.5, 0) ' Y axis.
MakeSegment(0, 0, 0, 0, 0, 0.5) ' Z axis.
End Sub

Public Structure Segment
'Connect the points:
<VBFixedArray(4)> Dim fr_pt() As Single
<VBFixedArray(4)> Dim to_pt() As Single
'Connect the transformed points:
<VBFixedArray(4)> Dim fr_tr() As Single
<VBFixedArray(4)> Dim to_tr() As Single
Public Sub Initialize()
ReDim fr_pt(4)
ReDim to_pt(4)
ReDim fr_tr(4)
ReDim to_tr(4)
End Sub
End Structure

Public Structure Transformation
<VBFixedArray(4, 4)> Dim M(,) As Single
Public Sub Initialize()
ReDim M(4, 4)
End Sub
End Structure

Public NumSegments As Short
Public Segments() As Segment

'Create a segment:
Public Sub MakeSegment(ByVal x1 As Single, ByVal y1 As Single, ByVal z1
As Single, _
ByVal x2 As Single, ByVal y2 As Single, ByVal z2
As Single)
NumSegments = NumSegments + 1
ReDim Preserve Segments(NumSegments)
Segments(NumSegments).fr_pt(1) = x1 'Error
Segments(NumSegments).fr_pt(2) = y1
Segments(NumSegments).fr_pt(3) = z1
Segments(NumSegments).fr_pt(4) = 1
Segments(NumSegments).to_pt(1) = x2
Segments(NumSegments).to_pt(2) = y2
Segments(NumSegments).to_pt(3) = z2
Segments(NumSegments).to_pt(4) = 1
End Sub

Building the project (Build, Build Solution) is carried out without errors.
However at running there are errors in the specified lines and the message:
Object reference not set to an instance of an object.

Inform, please, how to correct errors?

Beforehand many thanks for answer. Best regards, Dr. V.A. Zharkov. Moscow,
Russia.
 
A

Armin Zingler

Dr. Zharkov said:
Hello. A problem in the following.
In VB .NET 2003 we create the project and in file Form1.vb the
following code is written down:

I've already answerd in the other thread. Did you read it? Here it is again:
NumSegments = NumSegments + 1
ReDim Preserve Segments(NumSegments)
Segments(NumSegments).Initialize

Segments(NumSegments).fr_pt(1) = x1 'Error


This means that you have to call Sub Initialize for each Segment that you
create. If you don't call it, the fields within the structure (named fr_pt,
to_pt, fr_tr, to_tr) are Nothing. They are nothing because no array has been
assigned. The Redim statement within sub Initialize creates the arrays and
assigns them to the variables. If you don't call Initialize you get a
NullReferenceException.
 
D

Dr. Zharkov

Dear Mr. Armin Zingler.
Many thanks for your second answer (I have not had time to read your first
answer), which has allowed me to correct a error.
Excuse me, please, but I very much ask you to answer me the second question
on my second error.
In VB .NET 2003 we create the project, on the Form1 box place PictureBox1
and in file Form1.vb write the following code:

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

DrawData(PictureBox1)
End Sub


Private Sub DrawData(ByRef pic As Object)

DrawSomeData(pic)
End Sub


Private Sub DrawSomeData(ByVal pic As Object)

Dim redPen As New Pen(Color.Red, 3)
Dim g As Graphics = PictureBox1.CreateGraphics
g.DrawLine(redPen, 0, 0, 100, 200)
End Sub

Building the project (Build, Build Solution) is carried out without errors.
However at running on Form1 there is no line (g.DrawLine(redPen, 0, 0, 100,
200)).

Inform, please, how to construct a line inside PictureBox1 with the help of
Sub DrawSomeData?

Beforehand many thanks for answer. Best regards, Dr. V.A. Zharkov. Moscow,
Russia.
 
A

Armin Zingler

Dr. Zharkov said:
In VB .NET 2003 we create the project, on the Form1 box place
PictureBox1 and in file Form1.vb write the following code:

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

DrawData(PictureBox1)
End Sub


Private Sub DrawData(ByRef pic As Object)

DrawSomeData(pic)
End Sub


Private Sub DrawSomeData(ByVal pic As Object)

Dim redPen As New Pen(Color.Red, 3)
Dim g As Graphics = PictureBox1.CreateGraphics
g.DrawLine(redPen, 0, 0, 100, 200)
End Sub

Building the project (Build, Build Solution) is carried out without
errors. However at running on Form1 there is no line
(g.DrawLine(redPen, 0, 0, 100, 200)).

Inform, please, how to construct a line inside PictureBox1 with the
help of Sub DrawSomeData?


You have to draw the line each time Windows asks you to draw it. This is
done in the Picturebox' paint event:

Private Sub PictureBox1_Paint( _
ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles PictureBox1.Paint

e.graphics.DrawLine(Pens.Red, 0, 0, 100, 200)

End Sub

By the way, as the example shows, instead of creating a new red pen you can
use Pens.Red.
 
D

Dr. Zharkov

Dear Mr. Armin Zingler.
Many thanks for your answer. However your code is a usual code, which is
present in Help of VB .NET and which, naturally, to me is known.
Apparently, I could not formulate competently my question, therefore, if you
do not object, I'll try once again.
In VB .NET 2003 we create the project, on the Form1 box place PictureBox1
and in file Form1.vb write the following code:

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

DrawData(PictureBox1)
End Sub


Private Sub DrawData(ByRef pic As Object)

DrawSomeData(pic, 100, 200, _
System.Drawing.ColorTranslator.ToOle( _
System.Drawing.Color.Black), False)
End Sub


Public Sub DrawSomeData(ByVal pic As Object, _
ByVal first_seg As Short, ByVal last_seg As Short, _
ByVal myColor As Integer, ByVal clear As Boolean)

Dim redPen As New Pen(Color.Red, 3)
Dim g As Graphics = PictureBox1.CreateGraphics

g.DrawLine(redPen, 0, 0, first_seg, last_seg)
End Sub

Building the project (Build, Build Solution) is carried out without errors.
However at running on PictureBox1 there is no line (g.DrawLine(redPen, 0, 0,
first_seg, last_seg)


Inform, please, how to change the procedure Sub DrawSomeData, that this
procedure Sub DrawSomeData built a line (g.DrawLine(redPen, 0, 0, first_seg,
last_seg)) inside PictureBox1?

Beforehand many thanks for answer. Best regards, Dr. V.A. Zharkov. Moscow,
Russia.
 
A

Armin Zingler

Dr. Zharkov said:
Dear Mr. Armin Zingler.

Dear Dr. Zharkov,

if you don't mind you can simply call me "Armin" because this is common
usage in the usenet, but of course you don't have to. :)
Many thanks for your answer. However your code is a usual code, which
is present in Help of VB .NET and which, naturally, to me is
known. Apparently, I could not formulate competently my question,
therefore, if you do not object, I'll try once again.

In VB .NET 2003 we create the project, on the Form1 box place
PictureBox1 and in file Form1.vb write the following code:

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

DrawData(PictureBox1)
End Sub


Private Sub DrawData(ByRef pic As Object)

DrawSomeData(pic, 100, 200, _
System.Drawing.ColorTranslator.ToOle( _
System.Drawing.Color.Black), False)
End Sub


Public Sub DrawSomeData(ByVal pic As Object, _
ByVal first_seg As Short, ByVal last_seg As Short, _
ByVal myColor As Integer, ByVal clear As Boolean)

Dim redPen As New Pen(Color.Red, 3)
Dim g As Graphics = PictureBox1.CreateGraphics

g.DrawLine(redPen, 0, 0, first_seg, last_seg)
End Sub

Building the project (Build, Build Solution) is carried out without
errors. However at running on PictureBox1 there is no line
(g.DrawLine(redPen, 0, 0, first_seg, last_seg)


Inform, please, how to change the procedure Sub DrawSomeData, that
this procedure Sub DrawSomeData built a line (g.DrawLine(redPen, 0,
0, first_seg, last_seg)) inside PictureBox1?

Beforehand many thanks for answer. Best regards, Dr. V.A. Zharkov.
Moscow, Russia.



You formulated your question very well. I do understand what is your
concern. After reading your question again, I still understand it the same
way.

What I was trying to say in my previous post: You have to draw the line in
the Paint event of the Picturebox. You can not draw the line on the
picturebox in Form1_Load because the Form is not yet visible in Form_Load.
It is also not sufficient to draw the line one time, you have to draw it
each time when Windows wants you to draw it. The background is the
following: Imagine you draw the line on a visible picturebox. Then you
switch to a different applicatoin. The program covers your first
application. Then you switch back to the application. The Form and the
picturebox becomes visible again. In this case, Windows requests you to draw
the line *again* because the content of the screen has been overwritten by
the other application meanwhile. In other words: Graphics you draw on the
screen are not persistent. Windows does not remember the graphics you
painted. The graphics have to be drawn each time the part of the screen
becomes visible. Windows knows which window has to be redrawn and sents a
message to the window. Each time you get the message, the Paint event of the
picturebox is raised. That is the place where you can draw the graphics
again. Otherwise the window remains empty.

Additionally: A Picturebox has the property "Image". When you have assigned
an image to this property, the picturebox automatically draws the image
whenever Windows requests the picturebox to paint it's content. Therefore,
instead of manually painting the content of the Picturebox in it's Paint
event, you can assign an Image to the picturebox. Now you can ignore the
Paint event, but you have to draw on the Image instead. The advantage is
that you can draw before the picturebox is visible. You could create a
Bitmap in Form_Load, draw on the Bitmap and assign the Bitmap to the
Picturebox' Image property. Here is an example:

private m_Bitmap As Bitmap

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

m_Bitmap = New Bitmap(PictureBox1.Width, PictureBox1.Height)
DrawData(m_Bitmap)
End Sub

Public Sub DrawData(ByVal img As Image)

Dim g As Graphics = graphics.Fromimage(img)

g.DrawLine(Pens.Red, 0, 0, 100, 200)
g.Dispose
End Sub


If you have any questions, don't hesitate to ask again. :)


If you are interested in more information about message queues (messages
like WM_PAINT sent by Windows to your application):

http://msdn.microsoft.com/library/e...erface/windowing/messagesandmessagequeues.asp

About painting and drawing, expecially sub topic "Using the WM_PAINT message":
http://msdn.microsoft.com/library/en-us/gdi/pantdraw_3wtj.asp
 
C

Cor

Armin,

Excelent

While reading it I thought: "Why not in that way and you did explain it how
to do that, exactly as I thougth that was the alternative".

Cor
 
A

Armin Zingler

Cor said:
Armin,

Excelent

While reading it I thought: "Why not in that way and you did explain
it how to do that, exactly as I thougth that was the alternative".

I don't understand you.
 
C

Cor

Armin,

I found your explanation very good.

There was a part in where you said, why not converting it to an image, so
you don't have to paint it everytime. That idea I got while reading your
explanation.

But even that you wrote therefore my compliments, that is all.

Cor
 
A

Armin Zingler

Cor said:
There was a part in where you said, why not converting it to an
image, so you don't have to paint it everytime.

I did not write to *convert* it to an image.
That idea I got
while reading your explanation.

But even that you wrote therefore my compliments, that is all.

Thx
 
D

Dr. Zharkov

Hello Armin.
Many thanks for your very detailed both very much qualified answer and your
kind consent to answer other my questions. At me to you the following
request.
From one site to me have sent on email the project for VB6 on 3D-graphics.
In this project in three-dimensional measurement are drawn and rotate some
geometrical figures (a cube, a pyramid and others). On my computer is not
present VB6, and is VB .NET 2003. I standartly converted project VB6 in VB
..NET 2003, has corrected some errors (now building this project "Build,
Build Solution" is carried out without errors), but I can not debug it (this
project does not draw the figures). And all my previous questions to you
concern debugging this project.

At me to you the big request, Armin. It is possible for me to send you on
email this project for VB6, to ask you to convert it on VB .NET (or on VB
..NET 2003) and to ask you to inform me, how to start this project on VB .NET
2003?

Beforehand many thanks for answer. Best regards, Dr. V.A. Zharkov. Moscow,
Russia.
 
A

Armin Zingler

Dr. Zharkov said:
Hello Armin.
Many thanks for your very detailed both very much qualified answer
and your kind consent to answer other my questions. At me to you
the following request.
From one site to me have sent on email the project for VB6 on
3D-graphics. In this project in three-dimensional measurement are
drawn and rotate some geometrical figures (a cube, a pyramid and
others). On my computer is not present VB6, and is VB .NET 2003. I
standartly converted project VB6 in VB .NET 2003, has corrected
some errors (now building this project "Build, Build Solution" is
carried out without errors), but I can not debug it (this project
does not draw the figures). And all my previous questions to you
concern debugging this project.

At me to you the big request, Armin. It is possible for me to send
you on email this project for VB6, to ask you to convert it on VB
.NET (or on VB .NET 2003) and to ask you to inform me, how to
start this project on VB .NET 2003?

Beforehand many thanks for answer. Best regards, Dr. V.A. Zharkov.
Moscow, Russia.

You can send the project to me. (ZIP format please; do *not* remove "nospam"
from my mail address) Better: If possible, copy the file to an FTP server
and post the link here, so everyone interested in can download it.
 
D

Dr. Zharkov

Hello Armin.
Many thanks for your kind consent to consider the project for VB6.
Now I have sent the letter to the address, specified by you; answer, please,
it.
Here I will take advantage of your kind permission to set to you questions.
The code, specified by you, does not draw a line:

private m_Bitmap As Bitmap

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

m_Bitmap = New Bitmap(PictureBox1.Width, PictureBox1.Height)
DrawData(m_Bitmap)
End Sub

Public Sub DrawData(ByVal img As Image)

Dim g As Graphics = graphics.Fromimage(img)

g.DrawLine(Pens.Red, 0, 0, 100, 200)
g.Dispose
End Sub

Inform, please, where in this code an error?

Beforehand many thanks for your answer. Best regards, Dr. V.A. Zharkov.
 
A

Armin Zingler

Dr. Zharkov said:
Hello Armin.
Many thanks for your kind consent to consider the project for VB6.
Now I have sent the letter to the address, specified by you; answer,
please, it.

I received the mail but it has no attachment.
Here I will take advantage of your kind permission to set to you
questions. The code, specified by you, does not draw a line:

private m_Bitmap As Bitmap

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

m_Bitmap = New Bitmap(PictureBox1.Width,
PictureBox1.Height) DrawData(m_Bitmap)
End Sub

Public Sub DrawData(ByVal img As Image)

Dim g As Graphics = graphics.Fromimage(img)

g.DrawLine(Pens.Red, 0, 0, 100, 200)
g.Dispose
End Sub

Inform, please, where in this code an error?


You did not assign the Bitmap to the Image property of the Picturebox:

Me.Picturebox1.Image = m_Bitmap
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top