ListView.SelectedItems.Clear

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Ok I'm having a problem with a listview. I have a listview on a WinForm
called ProgramInformationFrm. I call this from from 2 other forms. One is
ProgramSearch Frm, the other is OpenNCFrm. The search form accepts criteria
from a user ans searches a sql table for any nc program that matches the
criteria. The open form gives the user a list of programs that have not been
added to table yet. Ok, enough background. What is happening is if I use the
search from and get 8 results the first item is selected per my code. Now I
close that form get into the add form, add a new record. It writes to the
table, clears the listview on the info table, and then shows the infofrom.
The item is not selected, but is the only item in the list. It should be
selected. Also in debug I still have the previous value from the first time
the form was shown.
Here's my code
OpenNCFrm.Visible = False
ProgramInformationFrm.ProgramsLV.SelectedItems.Clear()
ProgramInformationFrm.ProgramsLV.Items.Clear()
Dim item As New ListViewItem(tmpNCFile)
ProgramInformationFrm.ProgramsLV.Items.Add(item)
ProgramInformationFrm.ProgramsLV.Focus()
ProgramInformationFrm.ProgramsLV.Items(0).Selected = True
ProgramInformationFrm.ShowDialog()
 
Ok I'm having a problem with a listview. I have a listview on a WinForm
called ProgramInformationFrm. I call this from from 2 other forms. One is
ProgramSearch Frm, the other is OpenNCFrm. The search form accepts criteria
from a user ans searches a sql table for any nc program that matches the
criteria. The open form gives the user a list of programs that have not been
added to table yet. Ok, enough background. What is happening is if I use the
search from and get 8 results the first item is selected per my code. Now I
close that form get into the add form, add a new record. It writes to the
table, clears the listview on the info table, and then shows the infofrom.
The item is not selected, but is the only item in the list. It should be
selected. Also in debug I still have the previous value from the first time
the form was shown.
Here's my code
OpenNCFrm.Visible = False
ProgramInformationFrm.ProgramsLV.SelectedItems.Clear()
ProgramInformationFrm.ProgramsLV.Items.Clear()
Dim item As New ListViewItem(tmpNCFile)
ProgramInformationFrm.ProgramsLV.Items.Add(item)
ProgramInformationFrm.ProgramsLV.Focus()
ProgramInformationFrm.ProgramsLV.Items(0).Selected = True
ProgramInformationFrm.ShowDialog()

1) Focus() (ProgramsLV.Focus()) usually has no meaning before a form
is displayed as that is usually determined by what control on the form
has the TabStop Index of "0", or, focus of a particular control is
called in the form's activated event.
(Note: In VB6, changing a TabIndex in a control reindexed the other
controls that had a TabIndex. I don't know about earlier versions,
but in VB2005, changing a TabIndex of a control does not reindex the
others, so you can wind up with more than one control with the same
index value and unpredicitable results.)

2) Run the .exe in bin\debug a couple of time and see if you are
getting the expected value results.

Gene
 
Ok got it working. To pass the value I am doing this;
ProgramInformationFrm.ProgramsLV.SelectedItems.Clear()
ProgramInformationFrm.ProgramsLV.Items.Clear()
Dim item As New ListViewItem(tmpNCFile)
ProgramInformationFrm.ProgramsLV.Items.Add(item)
ProgramInformationFrm.ProgramsLV.Items(0).Selected = True
ProgramInformationFrm.ProgramsLV.Select()
ProgramInformationFrm.ProgramsLV.Items(0).Focused = True
ProgramInformationFrm.ShowDialog()
On form load of ProgramInformationFrm I know use
tmpNCFile = Me.ProgramsLV.Items(0).Text instead of
Me.ProgramsLV.SelectedItems.Item(0).text to return value of the first item
in the list.
 
Back
Top