Winforms - MDI Parent refresh/re-activate - vb.net

I have a MDI Parent form which has a menustrip for the application. My application startup file is the MDI Parent form which on load calls a child login form. Code as below:
Dim myForm As Form = New Login
Dim formResult As DialogResult = myForm.ShowDialog()
If formResult = Windows.Forms.DialogResult.OK Then
If LoginSucceeded = True Then
Me.tabMainMenu.Visible = True
ApplyUserAccess(eApp.DataAccess.DAL_UserSettings.SelectMenuSettingByUserID(glbUserID))
myForm.Dispose()
End If
End If
The menustrip has a Logout label which when clicked disables the menu strip and displays the login form again.
The boolean field LoginSucceeded determines a successful validation of the user credentials and sets the menu according to the access given to that user. My problem is the first time the main menu on the MDI parent is set properly based on the user's access. After logging out and logging in again, i wanted to set the main menu accordingly again which is not happening.
The Form_Load event on the MDI Parent is being executed only once.
Any tips of re-painting the MDI parent when it receives focus the 2nd time onwards.
Thanks,
ZK
My code for the Logoff is as below:
Dim blnLogout As DialogResult = MessageBox.Show("Are You Sure You Want To Logout?", "eApp", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If blnLogout = Windows.Forms.DialogResult.Yes Then
SetToolbarMenuStyle()
tabMainMenu.Visible = False
LoginSucceeded = False
blnShowLoginTab = True
Dim myForm As Form = New Login
myForm.MdiParent = Me
myForm.WindowState = FormWindowState.Normal
myForm.Show()
End If

Move your login code to its own method in your main form so you can call it multiple times:
Public Class Form1
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown ValidateLogin()
ValidateLogin()
End Sub
Private Sub LoginLogoutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LoginLogoutToolStripMenuItem.Click
ValidateLogin()
End Sub
Private Sub ValidateLogin()
' disable appropriate main form elements so they can't access anything:
Me.tabMainMenu.Visible = False
Using myForm As New Login
If myForm.ShowDialog(Me) = Windows.Forms.DialogResult.OK Then
' login succeeded: re-enable main form elements
Me.tabMainMenu.Visible = True
ApplyUserAccess(eApp.DataAccess.DAL_UserSettings.SelectMenuSettingByUserID(glbUserID))
Else
MessageBox.Show("Login Failed")
End If
End Using
End Sub
End Class
You also don't need the "LoginSucceeded" variable. You can pass a success/failure back to the main form by setting DialogResult to OK in your Login form:
Public Class Login
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If True Then ' <-- perform your check
Me.DialogResult = Windows.Forms.DialogResult.OK ' only return OK if login has succeeded
End If
End Sub
End Class

Here are presumptions on your code, I'm guessing you added the code about on the Form.Load event. The Form.Load event only gets raised when the form is shown for the first time.
According to MSDN
Form.Load Event
Occurs before a form is displayed for the first time.
And now, when you Log-Off, you're setting the visibility of the form to false. So what I suggest is you move your code from the Form.Load event to the Form.VisibleChanged event.
According to MSDN
Form.Load Event
Occurs when the Visible property value changes.

Related

Form Enabled in FormClosing Event Not Working

I'll show you my code first:
Private Sub AddProductToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AddProductToolStripMenuItem.Click
Me.Enabled = False
Dim frmAddProduct As New FormAddProduct
frmAddProduct.Show()
frmAddProduct.Owner = Me
End Sub
That is my Main Form to call AddProduct form, and this is my FormClosing in AddProduct
Private Sub FormAddProduct_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
ButtonReset.PerformClick()
Lock()
Me.Owner = Nothing
Me.Hide()
Dim frmMainIndex As New FormMainIndex
frmMainIndex.Enabled = True
End Sub
So I have set enabled = false in my main form when it call Add Product form, and enabled = true when I close my Add Product form, but enabled = true won't work.
When I close my Add Product, it's only hide Add Product form but not enabling main form, main form still not enabled. Is there something wrong with my code?
This line is your problem:
Dim frmMainIndex As New FormMainIndex
You are instantiating a new FormMainIndex. Whenever you use the New keyword you are creating a completely new and independant object. frmMainIndex is a completely different form than the first one which opened your FormAddProduct form.
Since you've set the FormAddProduct's owner to your FormMainIndex form, just set the owner's Enabled property to True instead:
ButtonReset.PerformClick()
Lock()
Me.Owner.Enabled = True
Me.Owner = Nothing
Me.Hide()
Also, your Me.Hide() call doesn't make any sense since your form is about to be closed.
Because you created a new instance of FormMainIndex, the frmMainIndex.Enabled was applied to this new instance, not the one that created your FormAddProduct. Why don't you show your FormAddProduct as a dialog. like this
Dim frmAddProduct As New FormAddProduct
frmAddProduct.ShowDialog(Me)

Start a process in a VB form without clicking a button

Here is my situation.
I have a windows from application in VB.NET. In the my first form there are two checkboxes. If the one is checked it goes to Form2, if only the second one is checked it goes to Form3. If both are checked then i want first to go on Form2 and when this process is finished go to Form3. All good until this point.
However, I want Form3 to start executing its code (which is inside a background worker) without pressing any button (which is the case when only the second checkbox is checked). Is this possible?
Thanks In advance!
Extra info....
To navigate between the Forms I use a Show/hide/Close scheme. For example:
If CheckBoxTrain.CheckState = CheckState.Checked Then
Me.Hide()
Form2.Show()
ElseIf CheckBoxTrain.CheckState = CheckState.Unchecked And CheckBoxEvaluate.CheckState = CheckState.Checked Then
Me.Hide()
Form3.Show()
At the end of the code in form2 i check if the second checkbox is also checked:
If Form1.CheckBoxTrain.CheckState = CheckState.Checked And Form1.CheckBoxEvaluate.CheckState = CheckState.Checked Then
Form3.Show()
Me.Hide()
End If
Then in Form3 there is a button in which after pressing it i have all the code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i = 1 To n_monte
Button1.Enabled = False
Button2.Enabled = True
BackgroundWorker1.WorkerSupportsCancellation = True
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.RunWorkerAsync()
Next
End Sub
All i want is for this last piece of code is to start running without pressing any buttons (only for the case in which both checkboxes are checked)
Declare a Boolean property in Form3 and set that property in Form1 based on whether or not both CheckBoxes are checked. In the Load event handler of Form3, start the BackgroundWorker or don't based on the value of that property.

How do I use the Tag property with forms and code in VB 2012?

I am writing a program using a database for customers and technicians. The main form (CustomerIncidents) has a toolstripbutton that opens a different form to (SearchByState) where the user inputs a state code and looks for any incidents.
If the user clicks into one of the datagrid cells I want that customers information to be stored in the TAG so that when the form is closed using the OK button that it will show back up in the main form (CustomerIncidents).
Edited 03/11/14 12:21pm
The problem is in the Main Form. When I click the OK button in the Second Form it tries to convert the DialogResult Button to a String. I can't figure out how to fix it.
Customer Form (Main Form) Opens to Secondary Form
Private Sub btnOpenState_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnOpenState.Click
Dim frmSearchState As New FindCustomer
----->>Dim selectedButton As DialogResult = frmSearchState.ShowDialog()
If selectedButton = Windows.Forms.DialogResult.OK Then
CustomerIDToolStripTextBox.Text = frmSearchState.Tag.ToString
End If'
Search By State Form (Secondary Form) Or "Child Form"
Private Sub btnOk_Click(message As String, ByVal e As DataGridViewCellEventArgs) Handles btnOk.Click
message = CustomersDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString
Me.Tag = message
Me.DialogResult = DialogResult.OK
End Sub
The click event for a button does not have a DataGridViewCellEventArgs parameter, and will throw an exception when you try to use it.
You don't need to use the Tag property since you can just create your own property.
In your child form, create a property called GridValue:
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
If dgv.CurrentCell Is Nothing OrElse dgv.CurrentCell.Value Is Nothing Then
MessageBox.Show("A cell needs to be selected.")
Else
Me.DialogResult = DialogResult.OK
End If
End Sub
Public ReadOnly Property GridValue As String
Get
Return dgv.CurrentCell.Value.ToString
End Get
End Property
In your parent form, you can now access your information:
Using frmSearchState As New FindCustomer
If frmSearchState.ShowDialog(Me) = DialogResult.Ok Then
CustomerIDToolStripTextBox.Text = frmSearchState.GridValue
End If
End Using
My personal approach for doing this kind of stuff is to create a public property in the child form, having the same type as the DATA you want to take back to your main form. So instead of storing DataGridView's reference in Tag property, you should really be storing the actual value that was there in the cell that the user clicked on.
For example, if your DGV cell has a string value in it, you could do something like:
Public Readonly Property StateName As String
Get
If YourDGV.SelectedCell IsNot Nothing Then
Return YourDGV.SelectedCell.Value
Else
Return ""
End If
End Get
End Property
(I have written that code by hand, so there may be some syntax problems, but you should be able to get the idea.)
You can now use ShowDialog() in the main form to bring up this child form and upon OK or Cancel, you could check the value of StateName property of your child form to get this value. The thing to remember here is that closing a form doesn't dispose off all its constituent controls and properties and therefore you can access them even after the form has finished ShowDialog() call.

Remove the windows form controls on exit

I'm adding the form controls on loading the form manually:
Me.FieldI = New TextBox()
Me.FieldI.Location = New System.Drawing.Point(50, 10)
Me.FieldI.Name = "FieldI"
Me.FieldI.Size = New System.Drawing.Size(40, 20)
Me.FieldI.TabIndex = 5
Me.Conversion.Controls.Add(Me.FieldI)
[..]
When I close the form window and reopen it, the control is still there (with the old .Text content , because its an textbox in this case).
I would like to remove the controls that have been created while form loading on the form close event, to prevent doubling the elements on my form.
How can I achieve this?
edit
Form closing code looks following (just showing up the main form back):
Private Sub Form1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.FormClosing
Main.Show()
End Sub
The problem here is that the form is not being disposed, so when you open it again the controls are still there from the last time it was opened.
Try the following:
Using frm = New subForm()
frm.ShowDialog()
End Using
The variable frm will be disposed after the using.
Also...
You can also provide feedback from a dialog, to check whether the form was successful or not. For example:
Dim frm As New subForm()
If frm.ShowDialog = DialogResult.OK Then
'YAY!
Else
'Something failed
End If

How can we show the parent form after showing the report Form?

I have a problem in showing the parent form and report form at the same time.When user click on the parent form for print it should pop up with yes or no button when user click on yes button it should print the form screen shot image,if we click "No" button it should show the crystal report.
When we click 'No' button it should show the crystal report.So to show the messagebox i have done like this
me.hide()
if MsgBox('Do you want to print screen shot image?') then
'Print screen shot image
me.Show()
else
'Show CxReport
me.Show()
end if
When I Did like this the parent form is strucking and unable to perform operations.
It's not standard practice to hide a form when showing a dialog. Completely remove the me.hide and me.show lines and try again.
I know that this question is an old one and you probably have figured it out by now, but I thought I would add an answer for future reference. All Forms have a FormClosing and FormClosed Event that you can attach a handler to in your creating Form. Here is a simple example of what I am trying to say.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
If MsgBox("Open Report?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
Dim frm2 As Form2 = New Form2
AddHandler frm2.FormClosed, AddressOf ReportClosing
frm2.Show(Me)
Else
Me.Show()
'Do your work for printing form here
End If
End Sub
Private Sub ReportClosing(sender As Object, e As FormClosedEventArgs)
'Remove handler to prevent memory leaks
RemoveHandler DirectCast(sender, Form2).FormClosed, AddressOf ReportClosing
Me.Show()
End Sub
End Class
Atlast I Found the way to get the access the parent Page hiddenField Value as given below
function getParentPageHField() {
var parentPageHField = window.opener.document.getElementById('hSelectedStandard').value;
document.getElementById('hStandard').value = parentPageHField;
}
Thanks for your inputs :-)