Open second form based on user input

Open second form based on user response to Yes/No Message Box

Page 1 of 1

6 Replies - 4471 Views - Last Post: 14 April 2009 - 04:36 PM Rate Topic: -----

#1 lwq1caq   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-April 09

Open second form based on user input

Posted 14 April 2009 - 01:35 PM

Hi everyone! I am relatively new to the programming world. Anyway I am working on a project using Visual Studio .NET 2003.

I am trying to open a second form based on user response to Yes/No Message Box. I have tried two different code variations. The first variation is:

Dim MsgAnswer As Boolean
MsgAnswer = True
Dim MsgRes As DialogResult
Dim CustomerInfo1 As New Form

MsgRes = MessageBox.Show("Is this an existing customer?", "New Customer?", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)

If MsgRes = DialogResult.No Then
CustomerInfo1.ShowDialog()
End If



With this code a form opens but it is blank not the one I designed.

The second variation is the same except I changed the DIM statement to:

Dim CustomerInfo1 As Form


and I changed the IF...Else statement to:

If MsgRes = DialogResult.No Then
CustomerInfo1.Show()
End If



With this code I get an exception error: An unhandled exception of type 'System.NullReferenceException' occurred in OrpheyOnOpen.exe

Additional information: Object reference not set to an instance of an object.


Any help would be greatly appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Open second form based on user input

#2 microstoc   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 59
  • Joined: 04-April 09

Re: Open second form based on user input

Posted 14 April 2009 - 02:25 PM

View Postlwq1caq, on 14 Apr, 2009 - 12:35 PM, said:

Hi everyone! I am relatively new to the programming world. Anyway I am working on a project using Visual Studio .NET 2003.

I am trying to open a second form based on user response to Yes/No Message Box. I have tried two different code variations. The first variation is:

Dim MsgAnswer As Boolean
MsgAnswer = True
Dim MsgRes As DialogResult
Dim CustomerInfo1 As New Form

MsgRes = MessageBox.Show("Is this an existing customer?", "New Customer?", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)

If MsgRes = DialogResult.No Then
CustomerInfo1.ShowDialog()
End If



With this code a form opens but it is blank not the one I designed.

The second variation is the same except I changed the DIM statement to:

Dim CustomerInfo1 As Form


and I changed the IF...Else statement to:

If MsgRes = DialogResult.No Then
CustomerInfo1.Show()
End If



With this code I get an exception error: An unhandled exception of type 'System.NullReferenceException' occurred in OrpheyOnOpen.exe

Additional information: Object reference not set to an instance of an object.


Any help would be greatly appreciated!



Try this ....


If (MsgBox(anymessage,msgboxstyle.yesno,"title") = MsgBoxResult.Yes) Then

	Dim FormX as New YourFormNameGoesHere
	
	FormX.ShowDialog()

End If



This post has been edited by microstoc: 14 April 2009 - 02:27 PM

Was This Post Helpful? 0
  • +
  • -

#3 lwq1caq   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-April 09

Re: Open second form based on user input

Posted 14 April 2009 - 02:52 PM

Thanks for the suggestion but it didn't work. Now I get a message stating that "my form name" is an undefined type.

This post has been edited by lwq1caq: 14 April 2009 - 02:54 PM

Was This Post Helpful? 0
  • +
  • -

#4 microstoc   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 59
  • Joined: 04-April 09

Re: Open second form based on user input

Posted 14 April 2009 - 03:17 PM

View Postlwq1caq, on 14 Apr, 2009 - 01:52 PM, said:

Thanks for the suggestion but it didn't work. Now I get a message stating that "my form name" is an undefined type.


I presume you are trying to show the form named CustomerInfo1 ? Is that the name of the form when you view it's properties in the designer? Go to the Designer and look at the Name property of the Form.

Dim FormX as New CustomerInfo1
FormX.ShowDialog()
Was This Post Helpful? 0
  • +
  • -

#5 lwq1caq   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 14-April 09

Re: Open second form based on user input

Posted 14 April 2009 - 03:42 PM

You would be correct -- I am trying to load the form "CustomerInfo1" when the user selects the No button in the message box.

Snapshot after I add code. I can't rebuild solution because of "type" error

 Dim MsgAnswer As Boolean
		MsgAnswer = True
		Dim diaRes As DialogResult

		diaRes = MessageBox.Show("Is this an existing customer?", "Select One", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)

		If diaRes = DialogResult.No Then
			Dim FormX As New CustomerInfo1
			FormX.ShowDialog()
		End If



Error: Type "CustomerInfo1" is not defined.

I did verify the name -- it is CustomerInfo1
Was This Post Helpful? 0
  • +
  • -

#6 kasbaba   User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 134
  • Joined: 03-November 08

Re: Open second form based on user input

Posted 14 April 2009 - 03:53 PM

View Postlwq1caq, on 14 Apr, 2009 - 02:42 PM, said:

Error: Type "CustomerInfo1" is not defined.
I did verify the name -- it is CustomerInfo1


hi,

please check if you have already declared a VARIABLE by the same name...I mean CustomerInfo1.

if not then...try this..

Dim MsgAnswer As Boolean
		MsgAnswer = True
		Dim diaRes As DialogResult

		diaRes = MessageBox.Show("Is this an existing customer?", "Select One", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)

		If diaRes = DialogResult.No Then
			Dim FormX As New System.Windows.Forms.Form
			FromX = My.Forms.CustomerInfo1
			FormX.ShowDialog()
		End If


hope this works.
Was This Post Helpful? 0
  • +
  • -

#7 microstoc   User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 59
  • Joined: 04-April 09

Re: Open second form based on user input

Posted 14 April 2009 - 04:36 PM

View Postkasbaba, on 14 Apr, 2009 - 02:53 PM, said:

View Postlwq1caq, on 14 Apr, 2009 - 02:42 PM, said:

Error: Type "CustomerInfo1" is not defined.
I did verify the name -- it is CustomerInfo1


hi,

please check if you have already declared a VARIABLE by the same name...I mean CustomerInfo1.

if not then...try this..

Dim MsgAnswer As Boolean
		MsgAnswer = True
		Dim diaRes As DialogResult

		diaRes = MessageBox.Show("Is this an existing customer?", "Select One", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)

		If diaRes = DialogResult.No Then
			Dim FormX As New System.Windows.Forms.Form
			FromX = My.Forms.CustomerInfo1
			FormX.ShowDialog()
		End If


hope this works.



Also, try this to test if you can call up another form.

Quickly create a blank form called Form3

Then substitute CustomerInfo1 with Form3 (in my code or kasbabas code)

Dim FormX As New Form3 'CustomerInfo1
FormX.ShowDialog

This post has been edited by microstoc: 14 April 2009 - 04:37 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1