5 Replies - 1071 Views - Last Post: 05 October 2010 - 04:09 PM Rate Topic: -----

#1 vennesschan   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 20-April 10

For Each method question

Posted 05 October 2010 - 11:16 AM

Hi,

I am trying to search if a name exist on the Form1's listview if the button is clicked from the Form2. But for somehow, I don't know why my code is never get fired. Can someone help me with the following code. Thank for very much.

Private Sub Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn.Click
 
Dim f1 As New Form1()

For Each itm As ListViewItem In f1.lv.Items
If itm.SubItems(0).Text = "Name: " & name_label.Text Then 'Never get fired here
'do something
Else
'do something
End If
Next
End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: For Each method question

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5612
  • View blog
  • Posts: 14,686
  • Joined: 18-April 07

Re: For Each method question

Posted 05 October 2010 - 11:27 AM

Does the event get fired at all do you know? If you put something in the "Else" section of your if, does it show? Try putting a messagebox there and see if it shows the messagebox for each item of the listview. Try to give us more of an idea of what is firing and what isn't.

Thanks! :)
Was This Post Helpful? 0
  • +
  • -

#3 vennesschan   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 20-April 10

Re: For Each method question

Posted 05 October 2010 - 11:39 AM

Hi, i put messagebox with the if and else, both messagebox didn't show. What did I do wrong on the code? Thank you for your help.
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: For Each method question

Posted 05 October 2010 - 11:55 AM

Add f1.Show() right after dim f1 as new form1 and it should do something.

Or You could use the default instance of form1

remove the dim f1 as new form1 line and changed the f1.Lv.Items to Form1.Lv.Items

This post has been edited by CharlieMay: 05 October 2010 - 11:58 AM

Was This Post Helpful? 0
  • +
  • -

#5 vennesschan   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 20-April 10

Re: For Each method question

Posted 05 October 2010 - 12:40 PM

Hi Charlie,

"remove the dim f1 as new form1 line and changed the f1.Lv.Items to Form1.Lv.Items" It doesn't work.

After add f1.show(), it works. Instead of showing the form1, how can I have the same result when the btn is clicked?

Thank you.
Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: For Each method question

Posted 05 October 2010 - 04:09 PM

Show the code you have the doesn't work after switching to the default instance.

Just thought of something.

Are you closing form1 and opening Form2 and then trying to access Form1? If so, you will need to repopulate the listview.

You are creating a new instance of the form and by default, the controls on that new instance are empty.

You can use this to see what I'm talking about.
       Dim f1 As New Form1()
        f1.LV.Items.Add("Name: Charlie").SubItems.Add("Age: Old")
        name_label.Text = "Charlie"
        For Each itm As ListViewItem In f1.LV.Items
            MessageBox.Show(itm.SubItems(0).Text)
            If itm.SubItems(0).Text = "Name: " & name_label.Text Then 'Never get fired here
                MessageBox.Show("Found")
            Else
                MessageBox.Show("Not Found")
            End If
        Next

Now when you run this code, you will get a message box that will have Name: Charlie" in it. Then when you click OK, you will get a second messagebox that had Found in it.

Now remark out the f1.LV.Items.Add line and you will not receive any message box. But why wouldn't you get Not Found???

The answer is because you are inside a For Each statement and since there is nothing in the listview the for each exits immediately jumping to the Next and skipping over everything inside the block.

Also note, you might have to remove the .SubItems.Add("Age: Old") as I had two columns in my listview and if you only have 1 column, you will get an error.

This post has been edited by CharlieMay: 05 October 2010 - 04:27 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1