Strange Problem

M

Mike Fellows

im running the code below when a new item is selected within a combobox the
code runs fine but when it gets to me.close it crashes - it doesnt start the
next form just crashes (its not debugabble) it seems to be a problem with
the .net framwork - have i done something silly, or have i found a known
bug - if so is there a workaround?

Thanks in advance for your help

Mike Fellows

Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged
If Loading = False Then
TimeAddy = Me.ComboBox3.Text.ToString
SaveFactFind()

If Me.ComboBox3.Text = "36 Months or over" Then
Dim x As New TotalTimeAtAddress
x.ID = ID
x.show()
Else
Dim x As New PreviousAddresses
x.ID = ID
x.Show()
End If
Me.Close()
End If
End Sub
 
J

Jonathan Allen

How was the program started?

If you use
Application.Run(new MyForm)
, then the application will exit when the aforementioned form is closed.

If you use
Dim X as new MyForm
x.Show
Application.Run
, then the application will exit when all forms are closed.
 
A

Armin Zingler

Jonathan Allen said:
How was the program started?

If you use
Application.Run(new MyForm)
, then the application will exit when the aforementioned form is
closed.
If you use
Dim X as new MyForm
x.Show
Application.Run
, then the application will exit when all forms are closed.


No, it does not exit. You would have to call application.exitthread to exit
Application.Run.

Armin
 
M

Mythran

Armin Zingler said:
No, it does not exit. You would have to call application.exitthread to
exit Application.Run.

Armin

Yes, it does exit. The Application.Run(Form) overload adds an event handler
to the Closed event for the Form. When the Closed event it raised, the
event handler calls ExitThread to clean up the application.

MSDN Library Link:
ms-help://MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemwindowsformsapplicationclassruntopic3.htm

This is explained in the above link :)

HTH,
Mythran
 
A

Armin Zingler

Mythran said:
Yes, it does exit. The Application.Run(Form) overload adds an event
handler to the Closed event for the Form. When the Closed event it
raised, the event handler calls ExitThread to clean up the
application.
MSDN Library Link:
ms-help://MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfsystemwindowsformsapplicationclassruntopic3.htm

This is explained in the above link :)


I was referring to the last statement I quoted. Application.run without
arguments does not exit when all forms are closed.

Armin
 
M

Mythran

I was referring to the last statement I quoted. Application.run without
arguments does not exit when all forms are closed.

Armin

Ahh, my apologies, I got ahead of myself :)

Mythran
 
M

Mike Fellows

im using:

Dim x As New TotalTimeAtAddress
x.ID = ID
x.show()
me.close()


the problem is the code crashes out (not exits, crashes) before the new form
is even displayed

i understand that if all forms where closed then the program would exit -
but that isnt the case - it crashes out (but not when in debug mode)
 
A

Armin Zingler

Mike Fellows said:
im using:

Dim x As New TotalTimeAtAddress
x.ID = ID
x.show()
me.close()


the problem is the code crashes out (not exits, crashes) before the
new form is even displayed

i understand that if all forms where closed then the program would
exit - but that isnt the case - it crashes out (but not when in debug
mode)

What does "crash" mean? Do you hear anything? ;-) Do you see a message? What
does it say?

Armin
 
M

Mike Fellows

ok i have worked out my problem, i just dont know how to fix it

on loading my form i do a loas of stuff to comboboxes (set values etc..)
while the form is loading i have a variable called loading that i set to
true.

so when i change a value of combobox and run some code i check that loading
= false
so that when setting values of comboboxes on form load it doesnt run this
code

looks somehting like this:

Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged
If Loading = False Then

do_stuff_here()

End If
End Sub

now if i add a me.close into the if loading = false statement so it looks
like this

If Loading = False Then

do_stuff_here()

me.close()
End If


when it eventully runs it does the do_stuff_here function then closes the
form
but then in debug mode it carries on after the me.close to the endif
statement
and crashes which has obvioulsy gone due to the me.close and crashes with
object reference not set to an instance of an object

i cant think of a solution to this and have no idea what to try, any help
would
be appreciated as im completely stuck now
 
C

Cor Ligthert

Mike,

Everybody is asking the same to you.

I try it in my words. What is *me* it is obviously a form I can see from
your answers. However, how did you start this form.

In other words:
Is it a mainform that starts automaticly conform VBNet rules.
Did you creade an extra module to start that mainform
Is it maybe a form instanced in the mainform where the mainform is no owner.

etc etc.

Cor
 
M

Mike Fellows

apologies

me is justa windows form called from the main form it is started like:

dim x as new myform
myform.show

me = myform

thanks
 
C

Cor Ligthert

Mike,

Another questions,

What is the sense of this
If Loading = False Then

do_stuff_here()

me.close()
End If

You probably start another form in that do_stuff_here.

With me.close you try to delete all references that you have in your
program.
(To your mainform where it is instanced and to your subform that you
instanced)

My expection is that this kind of program should crash or give unpredictable
results which can be the reason that it crashes in the debugger..

However maybe have others better ideas. This is just a guess.

Cor
 
M

Mike Fellows

ok

how would you recommend that i open a new form that no longer requires the
current form

i need to populate a public variable within this form when it loads

to be honest i have always written the above like this

dim x as new mynewform()
x.publicvariable = variable
x.show
me.close()

never had any problems with the above until now

am i doing this wrong? im self taught in vb.net as i moved from c++ so i
have no idea if what i am doing is corrent

thanks

Mike
 
C

Cor Ligthert

Mike,

I tried it in a test like this. The result was be in my test that the form x
will be showed. (Not actualy because before it is painted it is already
removed because that in my test the parent was closed)

\\\
Label1.Text = x.ToString
If x < 10 Then
Dim frm As New Form12
frm.x = x + 1
frm.Show()
If x > 5 Then
Me.Close()
End If
End If
///

I would do your problem with events, and close the forms from the place I
have instanced them. However, that is my approach.

Cor
 
M

Mike Fellows

Thanks Cor

i sort of understand what your saying

but the way im doing it now is the way i have always done it, and i have
never ever had a problem before

all very strange

thanks though
 
C

Chris Dunaway

You mention that it crashes. Is there an error? If so what is it and
can you print out the stack trace?
 

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