Refresh an UltraWinGrid from a separate form - vb.net

I've got a form with a list of customers and another form where customers can be added. When I open the fAddCustomer form, from fCustomerList, I'm calling it using this code:
Dim f As New Form
f = New fAddCustomer(con, False)
f.MdiParent = Me.MdiParent
f.Show()
On fCustomerAdd, I've got a ToolStripButton to add the customer. When the form is closed, I need to refresh the UltraWinGrid that I have on fCustomerList to view the new data on the list automatically.
Because I'm using a ToolStripButton and the form uses f.MdiParent = Me.MdiParent, I can't use the same solution that was used in this answer here, as there is no DialogResult on a ToolStripButton, and you can't use ShowDialog when using MdiParents.
Is there any other way I can achieve this at all?

Here's a simple example:
' ... in fCustomerList ...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim f As New fAddCustomer(con, False)
f.MdiParent = Me.MdiParent
AddHandler f.FormClosed, AddressOf f_FormClosed
f.Show()
End Sub
Private Sub f_FormClosed(sender As Object, e As FormClosedEventArgs)
' ... refresh your UltraWinGrid ...
End Sub

One that you could achieve this without changing the DataSource passing as #Plutonix suggested is to do something like this:
Private Sub fAddCustomer_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Try
If Application.OpenForms.OfType(Of fCustomerList).Any Then
Application.OpenForms("fCustomerList").Close()
Dim f As New fCustomerList()
f.MdiParent = Me.MdiParent
f.Show()
End If
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
End Sub

Related

VB.NET Backgroundworker only show loading form once?

I have a button on a form which basically get data from my database as below.
Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs)
Handles ToolStripButton1.Click
BackgroundWorker1.RunWorkerAsync()
Loading_Screen.ShowDialog()
End Sub
The loading screen is called after my code (obtain data from database) is run in backgroundworker.
When backgroundworker is done, I close the loading form as below
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs)
Handles BackgroundWorker1.RunWorkerCompleted
DataGridView1.DataSource = bSource
SDA.Update(dataTable)
ToolStripLabel1.Text = "RESULT : " + DataGridView1.RowCount.ToString
Loading_Screen.Close()
End Sub
This only works when I started the application for the first time. Whenever I click the button again, the loading form will not show anymore but the code still runs fine. Any idea?
The loading form has no code at all, just a running progress bar every time it is loaded.
What I have done but no luck :
Me.Refresh() the main form after calling loading form.
Me.Refresh() the loading form when on load function.
Tried loadingform.hide() instead of show()
Tried both show() and showdialog()
Tried creating new instance of the loading form.
Me.dispose() loading form on closing function
Me.dispose() main form on closing function
Setting loading form as top most.
UPDATE (I will keep updating my progress here)
As many of you asked to create a new instance, here is what I already did
Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs) Handles ToolStripButton1.Click
BackgroundWorker1.RunWorkerAsync()
ldScreen = New Loading_Screen()
ldScreen.ShowDialog()
Me.Refresh()
End Sub
Then, in run completed,
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
DataGridView1.DataSource = bSource
SDA.Update(dataTable)
ToolStripLabel1.Text = "RESULT : " + DataGridView1.RowCount.ToString
ldScreen.Close()
BackgroundWorker1.Dispose()
End Sub
In my loading form, the code is only this
Private Sub Loading_Screen_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Me.Dispose()
End Sub
Private Sub Loading_Screen_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.Refresh()
End Sub
UPDATE 2
By stripping out most of my code and putting system thread sleep in backgroundworker do work, the loading form shows up properly. So here is my code in backgroundworkerdowork on what is actually happening.
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Connect2Database()
Try
sqlCommand.CommandText = "Select * from kup_table" 'Load full database into gridview
SDA.SelectCommand = sqlCommand
SDA.Fill(dataTable)
bSource.DataSource = dataTable
mySqlConn.Close()
Catch ex As MySqlException
MsgBox(ex.ToString)
If mySqlConn.State = ConnectionState.Open Then
mySqlConn.Close()
End If
Finally
mySqlConn.Dispose()
End Try
'System.Threading.Thread.Sleep(2000)
End Sub
And here is the Connect2Database function codes
Private Sub Connect2Database()
sqlCommand = New MySqlCommand
dataTable = New DataTable
SDA = New MySqlDataAdapter
bSource = New BindingSource
Try
dataTable.Clear()
mySqlConn.ConnectionString = connString
sqlCommand.Connection = mySqlConn
mySqlConn.Open()
Catch ex As MySqlException
MsgBox(ex.ToString)
If mySqlConn.State = ConnectionState.Open Then
mySqlConn.Close()
End If
End Try
End Sub
UPDATE 3
What I have noticed is that when my System.Threading.Thread.Sleep(2000) is not commented, the loading screen will show up normally. But if I changed it to System.Threading.Thread.Sleep(1), loading screen does not shows up. Why is this happening? Code runs super fast after the first time?
This is because calling Close() disposes the Form. So either you have to create a new instance of the form every time, or you need to use Hide(). Judging by your question, I think you want the former.
have you tried to create a new instance of the form?
Private Sub ToolStripButton1_Click(sender As Object, e As EventArgs)
Handles ToolStripButton1.Click
Dim frm As New Loading_Screen
BackgroundWorker1.RunWorkerAsync()
frm.ShowDialog()
End Sub

How do I make a DataGridView invisble when I press a button?

I have a Button and a DataGridView. When I press the button I want the DataGridview to be visible and when I press it again be invisible
This is what I have tried so far:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim buttonId As New Button
Dim dvg As New DataGridView
Try
dvg = DirectCast(sender, DataGridView)
dvg.Visible = True
Catch ex As Exception
End Try
End Sub
I know the question might sound very basic, but I am quite inexperienced, so the help will be very much appreciated
first off, i would have the datagridview object as a member of your class.
then i would turn it on and off like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If dgv.Visible Then
dgv.Hide()
Else
dgv.Show()
End If
End Sub
hope this helps
Not sure adding controls dynamically is a good idea, but...
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
Try
Dim dgv As New DataGridView
dgv.Name = "dgvN"
dgv.Size = New Size(Me.ClientSize.Width - 20, 300)
dgv.Top = Me.ClientSize.Height / 2 - dgv.Height / 2
dgv.Left = Me.ClientSize.Width / 2 - dgv.Width / 2
dgv1.BringToFront()
Me.Controls.Add(dgv) ' use Controls() of desired container
Dim newButton As New Button
newButton.Text = "DGV On/Off"
newButton.Width = TextRenderer.MeasureText(newButton.Text, newButton.Font).Width + 20
newButton.Tag = "dgvN"
Me.Controls.Add(newButton) ' use Controls() of desired container
AddHandler newButton.Click, AddressOf DGVVisibleButtonClick
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Sub DGVVisibleButtonClick(sender As Object, e As EventArgs)
Dim dgv As DataGridView = Me.Controls(sender.tag)
dgv.Visible = Not dgv.Visible
End Sub

Bring MDIChild form to front if already open

I've been struggling to get this to work...I have a button on a MDIchild form that opens another MDIchild form, but if the form is already open, it does not recognize it and opens a new one instead of bringing it to front. This is the code i've got:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim MDIForm4 As New Form4
MDIForm4.MdiParent = Me
MDIForm4.Show()
End Sub
This works for the button to open the new form, and then I tried adding this:
If Not Form4 Is Nothing Then
Form4.BringToFront()
End If
But with no positive outcome. Does someone have any ideas?
Regards,
Jorge Brito
Here is how I typically do that:
For Each f As Form In Application.OpenForms
If TypeOf f Is frmTest Then
f.Activate()
Exit Sub
End If
Next
Dim myChild As New frmTest
myChild.MdiParent = Me
myChild.Show()
Notice this uses Application.OpenForms, you can use your Me.MdiChildren (assuming Me = this MDI form) if you want just the children of your main form.
Fixed now!
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Each f As Form In Application.OpenForms
If TypeOf f Is Form4 Then
f.Activate()
Exit Sub
End If
Next
Dim MDIForm As New Form4
MDIForm.MdiParent = Form2
MDIForm.Show()
End Sub
I was defining the MDI parent on the wrong form!

danamically creating controlltabs that can be used

I'm trying to dynamically create tabcontrols which works fine; the tabs are being created however. Once created I would also like them to become clickable and execute other code, this now posing a problem.
The code I’m using to create a tab is as follows
' do whatever wtih filename
Dim myTabPage As New TabPage()
myTabPage.Text = TextBox4.Text
TabControl1.TabPages.Add(myTabPage)
TabPage1.Hide()
Not so nice cause I can now fill my form with as many tabs as I like however none of them can be clicked to execute futher code???
EDIT:
Private Sub TabControl_SelectedIndexchaged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
Dim TabName As String
TabName = TabControl1.SelectedTab.Name
If TabName.Contains("TabPage") Then
' Do something
MsgBox("new tab created")
End If
End Sub
You have to add an event handler for the TabPage click event:
Dim myTabPage As New TabPage
myTabPage.Text = TextBox4.Text
AddHandler myTabPage.Click, AddressOf TabPage1_Click
TabControl1.TabPages.Add(myTabPage)
Which will call this code:
Private Sub TabPage1_Click(sender As Object, e As EventArgs)
MessageBox.Show(DirectCast(sender, TabPage).Text)
End Sub
Per your edit, you would have to add the name property:
myTabPage.Name = TextBox4.Text
And your SelectedIndexChanged event:
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As EventArgs) _
Handles TabControl1.SelectedIndexChanged
If TabControl1.SelectedTab IsNot Nothing Then
MessageBox.Show(TabControl1.SelectedTab.Name)
End If
End Sub

VB.NET Crystal Report Viewer Locks other Forms

What will I do to allow other forms to perform while crystal report view is running?
Heres the code:
Public Sub printCurrentHistory()
If dt.Columns.Count = 0 Then
With dt
.Columns.Add("update_time")
.Columns.Add("sender")
.Columns.Add("humidity")
.Columns.Add("temperature")
.Columns.Add("rain")
.Columns.Add("wind_dir")
.Columns.Add("wind_speed")
End With
End If
For Each dr As DataGridViewRow In frmMain.dgvSearch.Rows
dt.Rows.Add(dr.Cells("Updated").Value, dr.Cells("Sender").Value, dr.Cells("Humidity").Value, dr.Cells("Temperature").Value, dr.Cells("Rain").Value, dr.Cells("Wind_Direction").Value, dr.Cells("Wind_Speed").Value)
Next
Dim rptDoc As CrystalDecisions.CrystalReports.Engine.ReportDocument
rptDoc = New CrystalReport1
rptDoc.SetDataSource(dt)
frmPrint.CrystalReportViewer1.ReportSource = rptDoc
frmPrint.ShowDialog()
dt.Rows.Clear()
dt.Clear()
dt.Dispose()
rptDoc.Dispose()
End Sub
Crystal Viewer Report Form:
The difference between Show and ShowDialog is that ShowDialog is application Modal meaning it prevents anything else from happening until you close it. Show on the other hand throws up the Form and continues on its way. In looking at your code I would guess that your problem is that you are disposing of rptDoc and your datasource from underneath your frmPrint. You will need to change your frmPrint to be more autonomous. I would do something like.
frmPrint = New ParentFormName(dt) 'The Form frmPrint is instantiated from
frmPrint.Show()
And then
Public Sub New( dt as DataTable)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Dim rptDoc As CrystalDecisions.CrystalReports.Engine.ReportDocument
rptDoc = New CrystalReport1
rptDoc.SetDataSource(dt)
CrystalReportViewer1.ReportSource = rptDoc
End
And in your FormClosing event dispose of your Objects then.
This is untested code I am just trying to give you a few ideas.
Another option as I stated in my Comment is to add an Event to your print form and subscribe to it in Main Form like this.
Public Class Form1
Dim frm2 As Form2
Private Sub CloseMyData()
'Dispose of your Data here
RemoveHandler frm2.myClosingEvent, AddressOf CloseMyData
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
frm2 = New Form2
AddHandler frm2.myClosingEvent, AddressOf CloseMyData
frm2.Show()
End Sub
End Class
Public Class Form2
Public Event myClosingEvent()
Private Sub Form2_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
RaiseEvent myClosingEvent()
End Sub
End Class
Instead of
frmPrint.ShowDialog()
use
frmPrint.Show()