updating control on form2 from form1

R

R. Harris

Hi.

I have 2 forms:
form1
form2

On Form2 I have a listbox and a button. When I click the button it calls a
function from form1 and within that function it updates the listbox on
form2.
My problem is I don't see the items added to the listbox unless I use
form2.show() to open another instance of Form2.

I'm sure this is something simple.

Thanks.

Rob
 
G

Guest

Rob,

The function on form1 needs a reference to the current form2.

I suspect that the function is creating a new instance of form2 and not
using the current instance.

You might want to post your code if you can't get it working.

Kerry Moorman
 
R

R. Harris

I'm not sure I understand what 'The function on form1 needs a reference to
the current form2' means.
Ok, here is a portion of my code - The last if/else statement on Form1 is
where I assign a value to the listbox on Form2:

FORM2---------------------------------------
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim searchButton As String, textBoxString As String, deleteRecord As
Boolean
deleteRecord = True
searchButton = "SearchButton"
textBoxString = txtName.Text.ToUpper
Form1.FindMatch(searchButton, textBoxString, True)
End Sub


FORM1---------------------------------------
Public Function FindMatch(ByVal whatButton As String, Optional ByVal
SearchPar As String = "", Optional ByVal deleteRecord As Boolean = False)
'call sub to get record location.
openDatForRecordLocation()
' Clear the contents of the text box
TextBox1.Text = ""
' Open the file to read from and assign each line to string array called
readText()
Dim readText() As String
If closeApp = False Then
readText = File.ReadAllLines(pathToRecords)
'Use lineOfText in the For Each to iterate thru the string array
readText assigning matches to lineOfText.
Dim lineOfText As String
'matchesFound will be used to store matching results to display in
results text box or populate the combo box on form open
Dim matchesFound As String = ""
'countResults will be used to track the number of results found
Dim CountResults As Integer
'countRecords will be used to track the number of records in the
file
Dim countRecords As Integer
'Use a boolean variable to determine whether or not to process the
rest of the function if
'whatButton is equal to FormOpenCombo
Dim finishFunction As Boolean
For Each lineOfText In readText
'Count number of total records in the file
countRecords += 1
If lineOfText.ToUpper.Contains(SearchPar) Then
If Not lineOfText.StartsWith("[") And Not
lineOfText.EndsWith("]") Then
'Count the number of results found
CountResults += 1
'Sort the array
Array.Sort(readText)
If deleteRecord = False Then
' if not deleting a record do this
'For every match found, assign to matchesFound
variable with a carriage return.
matchesFound += lineOfText & vbCrLf
finishFunction = True
Else
'HERE IS WHERE I AM HAVING THE PROBLEM
'If deleting a record then do this
Form2.lstResults.Items.Add(lineOfText)
finishFunction = False
End If
End If

If i add change the above to this:

Form2.lstResults.Items.Add(lineOfText)
Form2.lstResults.Show()
finishFunction = False

Form2 will reopen with the original behind it, showing lstResults listbox
with the value of lineOfText.
 
G

Guest

Rob,

Is that VS2005's new feature of referring to a form? I don't use that
technique, so I can't really comment on it.

Maybe you could do a Form2.Refresh to get the data to show up in the
listbox. Or maybe a lstResults.Refresh.

Kerry Moorman


R. Harris said:
I'm not sure I understand what 'The function on form1 needs a reference to
the current form2' means.
Ok, here is a portion of my code - The last if/else statement on Form1 is
where I assign a value to the listbox on Form2:

FORM2---------------------------------------
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim searchButton As String, textBoxString As String, deleteRecord As
Boolean
deleteRecord = True
searchButton = "SearchButton"
textBoxString = txtName.Text.ToUpper
Form1.FindMatch(searchButton, textBoxString, True)
End Sub


FORM1---------------------------------------
Public Function FindMatch(ByVal whatButton As String, Optional ByVal
SearchPar As String = "", Optional ByVal deleteRecord As Boolean = False)
'call sub to get record location.
openDatForRecordLocation()
' Clear the contents of the text box
TextBox1.Text = ""
' Open the file to read from and assign each line to string array called
readText()
Dim readText() As String
If closeApp = False Then
readText = File.ReadAllLines(pathToRecords)
'Use lineOfText in the For Each to iterate thru the string array
readText assigning matches to lineOfText.
Dim lineOfText As String
'matchesFound will be used to store matching results to display in
results text box or populate the combo box on form open
Dim matchesFound As String = ""
'countResults will be used to track the number of results found
Dim CountResults As Integer
'countRecords will be used to track the number of records in the
file
Dim countRecords As Integer
'Use a boolean variable to determine whether or not to process the
rest of the function if
'whatButton is equal to FormOpenCombo
Dim finishFunction As Boolean
For Each lineOfText In readText
'Count number of total records in the file
countRecords += 1
If lineOfText.ToUpper.Contains(SearchPar) Then
If Not lineOfText.StartsWith("[") And Not
lineOfText.EndsWith("]") Then
'Count the number of results found
CountResults += 1
'Sort the array
Array.Sort(readText)
If deleteRecord = False Then
' if not deleting a record do this
'For every match found, assign to matchesFound
variable with a carriage return.
matchesFound += lineOfText & vbCrLf
finishFunction = True
Else
'HERE IS WHERE I AM HAVING THE PROBLEM
'If deleting a record then do this
Form2.lstResults.Items.Add(lineOfText)
finishFunction = False
End If
End If

If i add change the above to this:

Form2.lstResults.Items.Add(lineOfText)
Form2.lstResults.Show()
finishFunction = False

Form2 will reopen with the original behind it, showing lstResults listbox
with the value of lineOfText.




Kerry Moorman said:
Rob,

The function on form1 needs a reference to the current form2.

I suspect that the function is creating a new instance of form2 and not
using the current instance.

You might want to post your code if you can't get it working.

Kerry Moorman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top