1 Attachment(s)
[RESOLVED] Any Better Way? Advise.
I am trying to create a setup program that creates tables before the application loads. The data going to the tables comes from what is entered on the forms.
Below is a sample copy of the project. I just dont know whether this is the best way to do this. I have a feeling there may a simpler way of loading and unloading the forms...for example using "Select Case". Thats why i need your advise on this.
Please, i wont mind your Ideas...i need them.
Re: Any Better Way? Advise.
Hi
Why you do'nt try somethilng like that for next and previous buttons
using the showform method to close the actual form and show the new one
VB Code:
Private Sub showform(ByRef frm As Form)
Dim frmloaded As Form
For Each frmloaded In Me.SplitContainer1.Panel2.Controls
Me.SplitContainer1.Panel2.Controls.Remove(frmloaded)
frmloaded.Close()
frmloaded.Dispose()
Next
frm.TopLevel = False
Me.SplitContainer1.Panel2.Controls.Add(frm)
frm.Dock = DockStyle.Fill
frm.Show()
End Sub
Private Sub btnNext_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
Select Case cls_fp_I
Case 1
Dim frm As New frm1
showform(frm)
cls_fp_I += 1
Case 2
'...
Case 3
'...
Case 4
'...
End Select
End Sub
Hope his helps
Re: Any Better Way? Advise.
Thanks! Will give that a go...and get back.
Re: Any Better Way? Advise.
Jose...that was a brilliant Idea. What i dont know is how i can be able to close and dispose all forms when my page number is 0. I only want to dispose them off when the user shows that he doesnt want to go on with that data he wrote on the pages(i.e removing page1 from the panel or pressing cancel).
So if the user goes back upto page1 and he presses Back again, let all the forms be disposed, but if he presses next,he will get his information previously written and the forms will not be disposed off.
My code is below.
VB Code:
Private cls_FP As New cls_FormProperties
Private Sub btnBack_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
If cls_FP.pageNumber = 1 Then
cls_FP.pageNumber -= 1
Me.SplitContainer1.Panel2.Controls.Remove(cls_FP._frm1)
Me.btnBack.Enabled = False
ElseIf cls_FP.pageNumber = 2 Then
cls_FP.pageNumber -= 1
showform(cls_FP._frm1)
ElseIf cls_FP.pageNumber = 3 Then
cls_FP.pageNumber -= 1
showform(cls_FP._frm2)
ElseIf cls_FP.pageNumber = 4 Then
cls_FP.pageNumber -= 1
showform(cls_FP._frm3)
Me.btnFinish.Enabled = False
End If
End Sub
Private Sub btnNext_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
If cls_FP.pageNumber = 0 Then
cls_FP.pageNumber += 1
Me.btnBack.Enabled = True
showform(cls_FP._frm1)
ElseIf cls_FP.pageNumber = 1 Then
cls_FP.pageNumber += 1
showform(cls_FP._frm2)
ElseIf cls_FP.pageNumber = 2 Then
cls_FP.pageNumber += 1
showform(cls_FP._frm3)
ElseIf cls_FP.pageNumber = 3 Then
cls_FP.pageNumber += 1
showform(cls_FP._frm4)
Me.btnFinish.Enabled = True
End If
End Sub
Private Sub showform(ByRef frm As Form)
Dim ctrl As Control
For Each ctrl In Me.SplitContainer1.Panel2.Controls
If TypeOf ctrl Is Form Then
Me.SplitContainer1.Panel2.Controls.Remove(ctrl)
'If cls_FP.pageNumber = 0 Then
'DirectCast(ctrl, Form).Close()
'DirectCast(ctrl, Form).Dispose()
'End If
End If
Next
frm.TopLevel = False
Me.SplitContainer1.Panel2.Controls.Add(frm)
frm.Dock = DockStyle.Fill
frm.Show()
End Sub
Re: Any Better Way? Advise.
Hi
If you only are showing one form at each time, why have 4 of them instantiated? If you want to save some data of this forms you can do it on a variable on your main form. Doing that and closing & disposing the 'actual' form when you want to open another one. Then you will have what you need
See the code below
VB Code:
Private FormStep As Integer = 1
Private Sub btnBack_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
'Save anything from form here
FormStep -= 1
Dim frm As Form
Select Case FormStep
Case Is = 0
RemoveForms()
Me.btnBack.Enabled = False
Case Is = 1
frm = New frm1
'If you need to initialize somthing in frm1 do it here
Case Is = 2
frm = New frm2
'If you need to initialize somthing in frm2 do it here
Case Is = 3
frm = New frm3
'If you need to initialize somthing in frm3 do it here
Me.btnFinish.Enabled = False
End Select
showform(frm)
End Sub
Private Sub btnNext_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
'Save anything from form here
FormStep += 1
Dim frm As Form
Select Case FormStep
Case Is = 1
Me.btnBack.Enabled = True
frm = New frm1
'If you need to initialize somthing in frm1 do it here
Case Is = 2
frm = New frm2
'If you need to initialize somthing in frm2 do it here
Case Is = 3
frm = New frm3
'If you need to initialize somthing in frm3 do it here
Case Is = 4
frm = New frm4
'If you need to initialize somthing in frm4 do it here
Me.btnFinish.Enabled = True
End Select
showform(frm)
End Sub
Private Sub showform(ByRef frm As Form)
RemoveForms()
frm.TopLevel = False
Me.SplitContainer1.Panel2.Controls.Add(frm)
frm.Dock = DockStyle.Fill
frm.Show()
End Sub
Private Sub RemoveForms()
Dim ctrl As Control
For Each ctrl In Me.SplitContainer1.Panel2.Controls
If TypeOf ctrl Is Form Then
Me.SplitContainer1.Panel2.Controls.Remove(ctrl)
CType(ctrl, Form).close()
CType(ctrl, Form).dispose()
End If
Next
End Sub
I use select case instead of If .. ElseIF, because I think that programatically is clever, and maybe is faster (less comparisons).
Doing in that way probably you will use less computer resources than having a class with all the forms inside.
But if you prefer to do it as you had, then I suggest you to implement the close and dispose methods for each form inside you class, and just call a method from outside whenever you need.
Hope this helps
Re: Any Better Way? Advise.
Jose, thanks for the effort. Using "select case" is better though it serves the same purpose with "if else"...i dont know about the memory part of it.
But..creating a new instance of form every time you press Next and Back removes that data in the controls on the form. so...your method wont work here.
But i got a way to do it.
What i do is to create new instances of all the forms at class level,here i wont loose user data while the user navigates the forms using Next and BacK.
When i Close All the Loaded Forms...i create new instances. So here the user knows that he will loose all the data when he presses the Cancel Button.
I created a thread to close all loaded forms here.
http://www.vbforums.com/showthread.php?t=432428
Re: [RESOLVED] Any Better Way? Advise.
Without looking at your attachment it sounds like you're trying to create a wizard. In my opinion it is always preferable to use a single form for a wizard. You can place multiple Panels on the form and just change their Visible properties to show only the one you want or else adjust the z-order to place the desired Panel on top. If your wizard gets too control-heavy with all controls on the form at once then you can implement each page with a UserControl. Then you can add and remove controls as needed.
Re: [RESOLVED] Any Better Way? Advise.
Yes...i am creating a wizard. I cant imagine a form having more than 6 panels of the same size...with more than 10 controls in each!
But if i use a user control...isnt it a form too! hope you are not talking about adding controls to a user control at run time!
Re: [RESOLVED] Any Better Way? Advise.
A UserControl isn't a Form, it's a UserControl. A UserControl is just a control like any other, but one that you've created at design time by placing other controls on a design surface. The UserControl allows you to treat all those controls as a single unit, which is exactly what you would want in this situation. I'm saying that you'd create multiple UserControls at design time, then at run time you'd have a single form and create and destroy instances of your UserControls as needed. If you've only got six pages with no more than 10 controls on each page though, I'd just use Panels and have everything on the one form at the same time.
Re: [RESOLVED] Any Better Way? Advise.
Thanks JMC. I will buy the usercontrol part of it. I guess i am adding forms onto a panel as one can add controls.
My question is...well a userControl is a control on which we can can place other controls on at design time. Right! Suppose i have 7 userControls(with controls on each) which are like pages to be created and destroyed as needed. What difference does it make to just using forms(like controls) instead of userControls! Memory! i guess not.
Re: [RESOLVED] Any Better Way? Advise.
Actually another brilliant Idea is using a TabControl. then i Hide the Tabs here and there.
Thanks JMC.
Re: [RESOLVED] Any Better Way? Advise.
Using a single form with multiple UserControls is more attractive to the user because the view of the form itself is constant. You don't get that momentary flash as one form is closed and the next is displayed. It is less resource intensive because you're not destroying any forms but just the controls they contain. Navigation is simpler because you only have a single set of buttons on the form for Next, Previous, etc. There is no need to pass data from form to form because there is only one form to maintain all the data. This becomes even more attractive if you're using Panels rather than UserControls because everything, data and controls, is in and on the form at all times.
I've heard several people suggest using TabControls for a wizard interface but I think it's a bad idea. Showing several tabs at a time if they are not accessible is counter-intuitive. The user is justifiably used to being able to click a tab if it's visible so to have visible tabs not selectable is bad UI design. You acnnot change the visibility of a TabPage. It's either in the TabControl or it's not. You'd have to add and remove TabPages if you only wanted one visible at a time. The whole point of a TabControl is that the controls of the UI are spearated so that the user can access all of them easily without taking up too much space. A tabControl with a single TabPage at a time is pointless. There is no advantage in that over using a UserControl, Panel or GroupBox. In fact, having the tab sitting there with no purpose makes the UI look silly.
Re: [RESOLVED] Any Better Way? Advise.
JMC...i think i have bought into using TabControls.
The point is, i dont show any tabPages to the user at all nor do i disable them...i just make them invisible so that it looks like the controls are on the same form. Say i am on TabPage1, and the user presses Next...the controls on TabPage2 will be shown..and on and on.
No flashes are shown at all. Its Brilliant. Try a sample out and you see.