By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,105 Members | 2,620 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,105 IT Pros & Developers. It's quick & easy.

Refresh Listbox from Other Form

P: n/a
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

Sep 23 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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.

Sep 24 '07 #2

P: n/a
"Randy" <sp***********@gmail.comschrieb
>
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?

http://msdn2.microsoft.com/en-us/lib...a5(VS.80).aspx
Armin
Sep 24 '07 #3

P: n/a
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

Sep 24 '07 #4

P: n/a
On Mon, 24 Sep 2007 12:02:39 -0700, Randy <sp***********@gmail.com>
wrote:
>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.
Sep 24 '07 #5

P: n/a
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

Sep 24 '07 #6

P: n/a
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.

On Mon, 24 Sep 2007 15:07:49 -0700, Randy <sp***********@gmail.com>
wrote:
>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
Sep 25 '07 #7

P: n/a
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

Sep 27 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.