Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing Value from Combobox to new form problem 1

Status
Not open for further replies.

jimbledon

Technical User
Dec 27, 2004
22
GB
Hello,

I have built a form with 2 datagrids bound to a underlaying table; there are 3 tables in a dataset with the relations set up.

There is a combo box with some values extracted from one of the datagrids.

What i would like to do is pass the value from the combobox and open a new form.

I get a cast error saying that the variable passed is a double and should be an integer.

The error received is
Code:
Message is Object reference not set to an instance of an object.

Form a code

Code:
Dim EstNo As New Integer
        Dim test As String
        test = cmbEstNo.SelectedIndex.GetType.ToString
    

        Dim StrMsg As String
        StrMsg = "Confirmation of Value: EstNo " & test & ""
        Dim response As MsgBoxResult

        response = MsgBox(StrMsg, MsgBoxStyle.OKCancel)

        If response = MsgBoxResult.OK Then
            Try
                ' Show Main Form
                'Dim test As New Form
                ' EitemsStock test = New EitemsStock(EstNo))
                Dim Main As New EitemsStock(EstNo)
                Main.Show()


            Catch excNull As System.Exception
                Console.WriteLine("Message is " & excNull.Message)
                MsgBox("An Error has occurred. " & excNull.Message)
            End Try

        Else
            MsgBox("Please Select Correct Value")
        End If
    End Sub

Form b constructor code

Code:
Public Sub New(ByVal EstNo As Integer)
        MyBase.New()
        Try

            'Dim testint As Integer
            'testint = EstNo
            'MsgBox("The contents of the passed EstNo" + testint)
            StatusBar1.Text = "Passed EstNo =" & EstNo


        Catch ex As Exception
            Console.WriteLine("Message is " & ex.Message)
            StatusBar1.Text = "An Error has occurred. " & ex.Message
        End Try



        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call


        ' constructor?

    End Sub

Not sure what is going wrong with this, if anyone can help it would be greatly appreciated

regards

James
 
I think the error might have something to do with the fact that you don't ever actually assign a value to EstNo. You Dim the variable, then pass it to the constructor of the EitemsStock form, but nowhere in the code do you assign EstNo a value. Also, I don't think you need to Dim EstNo as New:

Dim EstNo As Integer

Finally, this line:

test = cmbEstNo.SelectedIndex.GetType.ToString

will put the value "System.Int32" in the "test" variable. If you want the actual value that was selected, use cmbEstNo.SelectedValue, or maybe cmbEstNo.Text if you want the value that is displayed in the combo.

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
whoops, my mistake.

Changed the following.

Code:
Dim EstNo as Integer
EstNo = cmbEstNo.Text

using the debugger the final line that is attempted before an execption is thrown is;

Code:
StatusBar1.Text = "Passed EstNo =" & EstNo

Error Message

Message is Object reference not set to an instance of an object.

This is pretty confusing as i dont know what else to try.

Regards

James
 
Do yo hae a reference to StatusBar1 in the EitemsStock form? Is EitemsStock an MDIChild form? If so, you might try:

Me.MDIParent.StatusBar1.Text = "Passed EstNo =" & EstNo



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Hi,

In regard i put a status bar on all forms when developing to output any messages to aid debugging. There is a status bar on the EitemsStock Form.

Let me try and explain what i'm trying to do.

I have a data entry form which is bound to customer table which contains 2 datagrids that covers address and orders. I populate a combo box with the order numbers in the second datagrid so the user can select one of these to view more details of it.
Where my plan is coming undone is the handing this value to the second form so the data shown is relevant to the first form.

Perferrably i'd like to do it on a single form but there are lots of controls on the form and the customer does not have much computing experience.

MDI forms were not used as i thought they only covered a one-many relationship as i have two forms that are going to update 6 tables this was out of scope.

I suppose i could get around it by using a global variable as this is not good practice i'd like to learn constructors in VB

regards

James
 
DOH! The problem is that you have the try...Catch block before the call to InitializeComponent(), so the StatusBar1 object has not been created/assigned yet, so you get the "Object reference not set to an instance of an object" error. Change the code to this, and it should work:


Public Sub New(ByVal EstNo As Integer)
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call
Try

'Dim testint As Integer
'testint = EstNo
'MsgBox("The contents of the passed EstNo" + testint)
StatusBar1.Text = "Passed EstNo =" & EstNo


Catch ex As Exception
Console.WriteLine("Message is " & ex.Message)
StatusBar1.Text = "An Error has occurred. " & ex.Message
End Try


' constructor?

End Sub

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Your a legend!!

I'm pretty new to this .Net the hardest part is understanding the framework and the order which the events fire.

Works like a charm, the EstNo lost its value when passed into a load datagrid method, so i just put it into a new private variable and bingo!

Thanks again

James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top