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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problems using multiple "With" statements 2

Status
Not open for further replies.

lunaclover

Programmer
Joined
Jun 22, 2005
Messages
54
Hi all -
I have three checkboxes 'chk1' that I have on 3 forms. When checked by user from any form it opens the same form (FrmT) using the code:
Code:
        If chk1.Checked Then
            Dim fT As New FrmT(Me)
            fT.Show()
        End If
frmT is a form consisting mainly of a listbox whose output varies depending on the combinations of chks selected from whichever form it is accessed by. Well, actually that's my problem. That's what I want it to do, and i'm trying to use With statements to make it do it.

Here is the code I'm using.
Code:
Public Class FrmT
    Inherits System.Windows.Forms.Form

    [b]Private frm2 As Form2[/b]
    
#Region " Windows Form Designer generated code "

    Public Sub New(ByVal [b]f2 As Form2[/b])
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()
        'Add any initialization after the InitializeComponent() call
        [b]frm2 = f2[/b]
    End Sub

Then I use:

Code:
Private Sub FrmT_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With frm2
            rb1.Checked = .rb1.Checked
            rb2.Checked = .rb2.Checked
            rb3.Checked = .rb3.Checked
            rb4.Checked = .rb4.Checked
            rb5.Checked = .rb5.Checked
End With

If rb1.checked then
lstbox1.items.add("stuff")
end if

It works fine when there is one With Statement, but when I add another one, like this,

Code:
Public Class FrmThermostat
    Inherits System.Windows.Forms.Form

    Private frm2 As Form2
    [b]Private frm3 As Form3[/b]

#Region " Windows Form Designer generated code "

    Public Sub New(ByVal f2 As Form2)
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()
        'Add any initialization after the InitializeComponent() call
        frm2 = f2
    End Sub
   [b] Public Sub New(ByVal f3 As Form3)
        MyBase.New()
        frm3 = f3
    End Sub [/b]

and

Code:
Private Sub FrmT_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With frm2
            rb1.Checked = .rb1.Checked
            rb2.Checked = .rb2.Checked
            rb3.Checked = .rb3.Checked
            rb4.Checked = .rb4.Checked
            rb5.Checked = .rb5.Checked
End With

If rb1.checked then
lstbox1.items.add("stuff")
end if

[b]
With frm3
            rb6.Checked = .rb6.Checked
            rb7.Checked = .rb7.Checked
            rb8.Checked = .rb8.Checked
            rb9.Checked = .rb9.Checked
            rb10.Checked = .rb10.Checked
End With

If rb8.checked then
lstbox1.items.add("stuff")
end if

[/b]

it says no.

Say I am on form2, and I check the chk1 - it will highlight
[highlight]rb6.Checked = .rb6.Checked[/highlight] and say

*******An unhandled exception of type 'System.NullReferenceException' occurred in Products.exe
Additional information: Object reference not set to an instance of an object.
***********

If I'm on form3 and check the chk1 on this form it will highlight
[highlight]rb1.Checked = .rb1.Checked[/highlight] and give the same error.

I know it's because it doesn't know what form2 is when I am calling frmT from form 3, and vice versus, but I'm not sure how to just make it ignore that object and do the code within the right object. For example, if frm2.chk1 then don't care about the frm3 object code, and only be concerned with the

Code:
With frm2
*code*
End With

Any suggestions/insight/sarcasm? Please let me know if this isn't clear. Any help would of course be appreciated greatly, as always!

Lunaclover


 
I would suggest leaving the base New() method alone, then add your specific New methods:
Code:
public usb new()
  mybase.new()
  'This call is required by the Windows Form Designer. 
  InitializeComponent()
  'Add any initialization after the InitializeComponent() call

end sub


Public Sub New(ByVal f2 As Form2)
  me.new
  frm2 = f2
End Sub

Public Sub New(ByVal f3 As Form3)
  me.new
  frm3 = f3
End Sub

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Well, one thing I see right off is that you need a call to InitializeComponent() in the constructor that takes Form3 as a parameter:

Public Sub New(ByVal f3 As Form3)
MyBase.New()
InitializeComponent()
frm3 = f3
End Sub

Also, I think you may need to put the With blocks and related If blocks in their respective constructors to avoid the NullReference exceptions.

Let me know if this helps.

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
 
The actual problem is that his constructor with the Form3 parameter doesn't call InitializeComponent. So when he attempts to use the With statement to access items on the form, an exception is thrown.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
You will need to add this

Code:
if not frm2 is nothing then
With frm2
            rb1.Checked = .rb1.Checked
            rb2.Checked = .rb2.Checked
            rb3.Checked = .rb3.Checked
            rb4.Checked = .rb4.Checked
            rb5.Checked = .rb5.Checked
End With
endif
and the same for when you use frm3

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
Thanks guys!

I tried all variations of the proposed solutions - and am still receiving the same error.

jebenson when you said,
Also, I think you may need to put the With blocks and related If blocks in their respective constructors to avoid the NullReference exceptions.

Not sure what you mean by this... didn't I already do that?

lunaclover
 
what he meant was this

Code:
Public Sub New(ByVal f2 As Form2)
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()
        'Add any initialization after the InitializeComponent() call
        frm2 = f2
With frm2
            rb1.Checked = .rb1.Checked
            rb2.Checked = .rb2.Checked
            rb3.Checked = .rb3.Checked
            rb4.Checked = .rb4.Checked
            rb5.Checked = .rb5.Checked
End With

If rb1.checked then
lstbox1.items.add("stuff")
end if
end sub

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
Luna, try this:

Three forms -
Form2 and Form3 have CheckBox1, CheckBox2 and CheckBox3 and a button.
FormT has CheckBox1 - 6

The code for the button on Form2 and Form3 is:

Dim ft As New FormT(Me)
ft.Show()

The code for FormT is shown below

Code:
Public Class FormT
  Inherits System.Windows.Forms.Form

  Private f As Form

#Region " Windows Form Designer generated code "

  Public Sub New(ByVal calledby As Form)
    MyBase.New()

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

    'Add any initialization after the InitializeComponent() call
    f = calledby

  End Sub

.
.
.
.

  End Sub

#End Region

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

    If TypeOf f Is Form2 Then
      With CType(f, Form2)
        CheckBox1.Checked = .CheckBox1.Checked
        CheckBox2.Checked = .CheckBox2.Checked
        CheckBox3.Checked = .CheckBox3.Checked
        If CheckBox2.Checked Then
          'do stuff
        End If
      End With
    Else
      With CType(f, Form3)
        CheckBox4.Checked = .CheckBox1.Checked
        CheckBox5.Checked = .CheckBox2.Checked
        CheckBox6.Checked = .CheckBox3.Checked
        If CheckBox5.Checked Then
          'do some other stuff
        End If
      End With
    End If

  End Sub
End Class

Hope this helps.
 
EarthandFire and thatrickguy - both solutions worked magic. Thanks a lot for your help! I really appreciate it.

Lunaclover
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top