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!
Related
I would like to reset my form using vb.net i would try to below code but form is close can't open new form.
Private Sub resert_button_Click(sender As Object, e As EventArgs) Handles resert_button.Click
Dim client = New client_entry
client.Show()
Me.Close()
End Sub
Try Me.Hide and swap the order
Me.Hide()
client.Show()
There could be multiple ways to handle this one but myapproach would be like calling form_shown even where I will load required data and during reset i will call this event.
Private Sub Me_Shown(sender As Object, e As EventArgs) Handles Me.Shown
LoadData() 'This function/sub will load data for this form
End Sub
Private Sub resert_button_Click(sender As Object, e As EventArgs) Handles resert_button.Click
Me_Shown(sender,e)
End Sub
Use Me.Dispose() instead of Me.Close() it will dispose the form then when you call it again Yourform.Show(), It will Generate a new one.
One way of doing this would be to add a property to Form2. I'm assuming that you have two forms Lets call them Form1 and Form2. Somewhere in the code for Form1, you declare an instance of Form2 ..
Dim frm2 As New Form2
and at some point you want to show Form2 as a modal window ..
frm2.ShowDialog()
For the moment lets look at the Form2 code
I'm assuming here that you have a button to reset the form and to close the form, you just click on the form close button in the top right hand corner and maybe you have a button to close the form as well. Consider the following code for Form2
Public Class Form2
Friend Property resetOnClose As Boolean = False
Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
resetOnClose = True
Me.Hide()
End Sub
Private Sub btnclose_Click(sender As Object, e As EventArgs) Handles btnclose.Click
resetOnClose = False
Me.Hide()
End Sub
End Class
There is a property called resetOnclose which is a Boolean type. If you click on the reset button, this property is set to True If you click on the close button, the resetOnClose property is set to false.
In all these bits of code, frm2 is Hidden - not closed. This means that the form and its resetOnclose property is still available to Form1. OK now to look at the Form1 code ..
Public Class Form1
Dim frm2 As New Form2
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Shown
frm2.ShowDialog()
Do
If frm2.resetOnClose Then
frm2.Close()
frm2 = New Form2
frm2.ShowDialog()
Else
frm2.Close()
End If
Loop Until frm2.resetOnClose = False
End Sub
End Class
For this example, frm2 is opened as soon as Form1 is shown, but you can put the relevant code anywhere you need
In the Form1.Shown code you will see a loop. The loop will continue looping as long as resetOnClose is True. When frm1 is shown modally, execution in this Form1 code waits until frm2 is hidden or closed. Next, the Form1 code checks to see if the resetOnClose property is true or false. If it's false, frm2 is closed and the loop terminates. If the property is true, frm2 is closed and reassigned a new instance of Form2 in its default state.
Voila!
This is my form1, it contains lots of checkboxes and a button, to show the form2:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.show()
End Sub
End Class
And here is my form2, also it contains lot of checkboxes and a button, for unchecking all the checkboxes on form1 and on form2:
Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'code for clearing all the checkboxes, I don't know how to do it
End Sub
End Class
My question is: how can I make a code in Form2.Button1 for unchecking all the checkboxes in the Form1, Form2 and even other forms??
I tried this code, that unchecks only the checkboxes in the form where is it placed:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each cc As Control In Me.Controls
If TypeOf cc Is CheckBox Then
DirectCast(cc, CheckBox).Checked = False
End If
Next
End Sub
Maybe I can do that by editing this code.. I don't know
Thank you all in advance
You need to keep a list of open forms in a central location, when any form is created it should add itself to the list and when disposed, remove itself (you can inherit all your forms from a common base form that does this)
Then you can give all forms to a similar method that iterates through them and does the job.
Also, please note that your code does not uncheck all checkboxes on the form, it just unchecks checkboxes Directly on the form. If you might have chackboxes on panels, groupboxes, etc, you need to improve the code to recursively check for inner controls.
Likely #Alireza said you need to recursive check for inner controls, Hopefully following is what you want,
Thanks,
For every form, you just need to insert
Dim x As New Class1
x.TempClass(Me)
For example code
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim x As New Class1
x.TempClass(Me)
End Sub
End Class
and then you should create a new class for all CheckBoxs action, like below
Public Class Class1
Public Function TempClass(ByRef form As Control)
Dim allTxt As New List(Of Control)
For Each txt As CheckBox In FindControlRecursive(allTxt, form, GetType(CheckBox))
txt.Enabled = False
txt.Checked = False
Next
Return form
End Function
Public Shared Function FindControlRecursive(ByVal list As List(Of Control), ByVal parent As Control, ByVal ctrlType As System.Type) As List(Of Control)
If parent Is Nothing Then Return list
If parent.GetType Is ctrlType Then
list.Add(parent)
End If
For Each child As Control In parent.Controls
FindControlRecursive(list, child, ctrlType)
Next
Return list
End Function
End Class
Finally I got a solution:
For Each cc As Control In Me.Controls
If TypeOf cc Is CheckBox Then
DirectCast(cc, CheckBox).Checked = False
End If
Next
For Each cc As Control In Form1.Controls
If TypeOf cc Is CheckBox Then
DirectCast(cc, CheckBox).Checked = False
End If
Next
I am having two forms in vb where main form contains button and on button click event form2 will load.
I wrote this code:
Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs) Handles newCustomer.Click
Dim d As BlankPage1 = New BlankPage1
d.show()
End Sub
where Blankpage1 is the name of second form. The problem is after clicking button, Blankpage1 doesn't appear.
Try BlankPage1.show() only, don't initialize first.
Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs) Handles newCustomer.Click
form2.show() //name of the form
End Sub
Try like this :
Dim d As New BlankPage1
d.MdiParent = Me 'or the main form's name(MDI Parent Form)
d.StartPosition = FormStartPosition.CenterScreen
d.Show()
d.Focus()
When I use Me.BringToFront() to Form4 it doesn't bring the form to the front as you can see in the picture:
And when I use Form1.Tab.SendToBack() to the Tab, the Tab disappear.
How can I bring Form4 to the front and prevent the Tab from disappearing?
Im able to solve it. I turn the propeties of Form1 IsMdiContainer to False then use this code
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim F As Form4
F = New Form4
F.Show()
F.Owner = Me
End Sub
Having a problem linking my form3 back to form2 in windows forms. I want the "back" button to take me back to form 2 but it doesnt do so. I am trying form2.show() but it doesnt work.
My current form3 code:
Public Class Form3
Private Sub CheckedListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CheckedListBox1.SelectedIndexChanged
MessageBox.Show("Developer Succsessfully Added to Sprint", "Developer Added")
End Sub
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
**Form2.Show()**
End Sub
End Class
What happening is most likely you already closed your form and can't open it again.
Assuming, you have both instances well and alive
In Form2 you should have
me.Hide()
Form3.Show()
And In Form3 you should have
me.Hide()
Form2.Show()
It can be something like this
shared sub Main
dim f2 as new Form2()
dim f3 as new Form3()
f2.Next = f3
f3.previous = f2
end sub
To link forms you creating properties, Next and Previous
And then use that as way to operate the form that should open
In form code do
private sub BtnNext_Click(....).....
Me.Hide()
Me.Next.Show()
End Sub
and the same way for the previous. If you have wizard, you could chain all your forms this way.
and of course, to accomplish this, minimum, you need an interface that contracts your forms to implement Properties Next and Previous or you can have a base class with implementation of the buttons and properties and then it will all work.
simple code redirect one form1 to form2 Using C#
Form2 f2=new Form2();
f2.show() OR f2.ShowDialog();