Refresh Listbox from Other Form

R

Randy

Hi,
I've got a listbox on a form that gets updated by opening another form
for data entry. Once the user enters the new item name and clicks
"accept", the data entry form closes and the original form with the
listbox displays. I'm having trouble getting the listbox to reflect
the additional entry. If I close the form and reopen it, the new item
displays just fine, but I know that there is a more direct way to
force the listbox to refresh then closing and reopening. Can anybody
help?

Here is the code that I am using to get the listbox to refresh (but it
is obviously not working):

With frmJobsItems 'this is the name of the form with the
listbox on it
.bsJobs.Dispose() 'the listbox is tied to a binding source
.dsTimesheets.Dispose() 'this is the dataset that the
bindingsource pulls from
.taJobs.Fill(frmJobsItems.dsTimesheets.Jobs) 'this is the
tableadapter that holds the data
End With

Any help is appreciated.
Thanks,
Randy
 
C

Cor Ligthert[MVP]

Randy,

Create your method as public and start it from your other form.

Why are you using that dispose method by the way, the disposing of the
controls is done in the standard by the form created method? The dataset
does AFAIK is even nothing with that from components automaticly implemented
method.

If you want to clear a dataset then use that method or set it to nothing and
create it new (the latter can be a time concumming method with a strongly
typed dataset).

Cor
 
R

Randy

Create your method as public and start it from your other form.

Sorry, I don't understand how to implement this advice. Can anybody
tell me how to do this?

Thanks.
 
R

Randy

Thanks, but I'm still stuck. I should have been more specific in my
reply. I know what public methods are. But in this case, there is no
method to make public. I have a listbox on form1 (which is bound to a
single column in a SQL table) and a textbox on form2. When the Accept
button on form2 is clicked, it initiates the code above, which submits
the text in the textbox into the SQL table and then attempts to
refresh the listbox from form1. Since the SQL table now includes this
new entry, the listbox will display it - if only I can trigger it to
refresh its databinding.

Sorry if I am being dense, but I am spinning my wheels over a couple
of lines of code.

Thanks again,
Randy
 
J

Jack Jackson

Thanks, but I'm still stuck. I should have been more specific in my
reply. I know what public methods are. But in this case, there is no
method to make public. I have a listbox on form1 (which is bound to a
single column in a SQL table) and a textbox on form2. When the Accept
button on form2 is clicked, it initiates the code above, which submits
the text in the textbox into the SQL table and then attempts to
refresh the listbox from form1. Since the SQL table now includes this
new entry, the listbox will display it - if only I can trigger it to
refresh its databinding.

Sorry if I am being dense, but I am spinning my wheels over a couple
of lines of code.

If I understand you correctly, this is the code that you are calling
from the Accept button on form2:

With frmJobsItems 'this is the name of the form with the
listbox on it
.bsJobs.Dispose() 'the listbox is tied to a binding source
.dsTimesheets.Dispose() 'this is the dataset that the
bindingsource pulls from
.taJobs.Fill(frmJobsItems.dsTimesheets.Jobs) 'this is the
tableadapter that holds the data
End With

First, frmJobItems is a variable containing a reference to form1. It
may coincidently be the name of the form, but the name does not
matter.

You Dispose the binding source and the dataset, then call the
TableAdapter passing a reference to something in the dataset that you
just disposed. You dispose something when you are finished using it,
not when you want to reuse it.

How are you modifying the table in form2? If you not doing the
modification by using dsTimesheets and taJobs, then you need to
refetch data into dsTimesheets.
 
R

Randy

Yes, frmJobItems is what I was referring to as form1 (just trying to
restate it simpler terms). I am using the dispose command simply as a
bad attempt to essentially "reboot" the listbox by closing its
bindingsource and then refilling the table adapter. Sorry if that is
a silly attempt, but I've tried many ways and this is simply the
latest bad attempt.

As for modifying the table, I didn't post that code since that part is
working fine and I was trying to keep this straightforward. Here is
the entire code (frmJobItems is "form1" which contains the listbox
that I am trying to refresh). It's only the last few lines of code
that don't work.

Private Sub btnAddJob_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnAddJob.Click
Dim strSQL As String
strSQL = "SELECT JobName FROM Jobs WHERE JobName = @JobName"
Dim da As New SqlDataAdapter(strSQL, cn)
da.SelectCommand.Parameters.AddWithValue("@JobName",
tbNewJob.Text)
Dim tbl As New DataTable
da.Fill(tbl)
da.SelectCommand.Parameters.Clear()
If tbl.Rows.Count > 0 Then 'this entry is already in the table
MessageBox.Show("This job already exists in your list." &
vbCrLf & "Entry Aborted.", "Duplicate Entry Detected",
MessageBoxButtons.OK, MessageBoxIcon.Stop)
Exit Sub
End If

If tbNewJob.Text = "" Then
MessageBox.Show("Please enter a name for your job.",
"Invalid Entry", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Exit Sub
End If

'Insert Recipe Header Row
strSQL = "INSERT INTO Jobs (JobName) VALUES (@JobName)"

Dim cmdInsert As New SqlCommand(strSQL, cn)
cmdInsert.Parameters.AddWithValue("@JobName", tbNewJob.Text)

Try
cn.Open()
cmdInsert.ExecuteNonQuery()
cmdInsert.Parameters.Clear()
frmJobsItems.taJobs.Fill(dsTimesheets.Jobs)
Catch ex As Exception
MessageBox.Show(ex.Message)
cn.Close()
End Try

cn.Close()

With frmJobsItems
.bsJobs.Dispose()
.dsTimesheets.Dispose()
.taJobs.Fill(frmJobsItems.dsTimesheets.Jobs)
End With

Me.Close()
End Sub

Thanks for considering this, Jack.

Randy
 
J

Jack Jackson

So it looks like you are not using form1's DataSet and DataAdapter to
update the table. In that case you need to reload the data into
form1's DataSet.

My guess is that would be something like:
With frmJobsItems
.taJobs.Fill(.dsTimesheets.Jobs)
End With

That is exactly what you do in the try block to update
dsTimesheets.Jobs, which I guess is a DataSet in from2. You could try
calling the listbox's Refresh() method, but I'm not sure why that
would be necessary.

Also, I would recommend creating a Sub in form1 to do all of this, and
calling that Sub from form2. It usually isn't a good idea to have
code someplace else (in form2 in this case) knowing about the names of
controls and objects in another object.
 
R

Randy

That was it. I simply had to create a public sub on form1 containing
one line of code to fill the table adapter and call it from form2.
This was probably what Cor was suggesting, but I wasn't smart enough
to implement it.

Thanks to everybody who replied, particularly Jack.
Randy
 

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