Results 1 to 13 of 13

Thread: [RESOLVED] Any Better Way? Advise.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Resolved [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.
    Attached Files Attached Files

  2. #2
    Hyperactive Member josep's Avatar
    Join Date
    Sep 2006
    Location
    Barcelona
    Posts
    409

    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:
    1. Private Sub showform(ByRef frm As Form)
    2.         Dim frmloaded As Form
    3.         For Each frmloaded In Me.SplitContainer1.Panel2.Controls
    4.             Me.SplitContainer1.Panel2.Controls.Remove(frmloaded)
    5.             frmloaded.Close()
    6.             frmloaded.Dispose()
    7.         Next
    8.         frm.TopLevel = False
    9.         Me.SplitContainer1.Panel2.Controls.Add(frm)
    10.         frm.Dock = DockStyle.Fill
    11.         frm.Show()
    12.     End Sub
    13.  
    14.     Private Sub btnNext_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
    15.         Select Case cls_fp_I
    16.             Case 1
    17.                 Dim frm As New frm1
    18.                 showform(frm)
    19.                 cls_fp_I += 1
    20.             Case 2
    21.                 '...
    22.             Case 3
    23.                 '...
    24.             Case 4
    25.                 '...
    26.         End Select
    27.     End Sub

    Hope his helps
    Last edited by josep; Oct 11th, 2006 at 05:09 AM.
    Useful links:DB connection strings ADO.NET VB.NET Tutorials

    • Don't forget to close the thread if solved
    • If this post helps you rate it

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: Any Better Way? Advise.

    Thanks! Will give that a go...and get back.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    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:
    1. Private cls_FP As New cls_FormProperties
    2.     Private Sub btnBack_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
    3.         If cls_FP.pageNumber = 1 Then
    4.             cls_FP.pageNumber -= 1
    5.             Me.SplitContainer1.Panel2.Controls.Remove(cls_FP._frm1)
    6.             Me.btnBack.Enabled = False
    7.         ElseIf cls_FP.pageNumber = 2 Then
    8.             cls_FP.pageNumber -= 1
    9.             showform(cls_FP._frm1)
    10.         ElseIf cls_FP.pageNumber = 3 Then
    11.             cls_FP.pageNumber -= 1
    12.             showform(cls_FP._frm2)
    13.         ElseIf cls_FP.pageNumber = 4 Then
    14.             cls_FP.pageNumber -= 1
    15.             showform(cls_FP._frm3)
    16.             Me.btnFinish.Enabled = False
    17.         End If
    18.     End Sub
    19.  
    20.     Private Sub btnNext_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
    21.         If cls_FP.pageNumber = 0 Then
    22.             cls_FP.pageNumber += 1
    23.             Me.btnBack.Enabled = True
    24.             showform(cls_FP._frm1)
    25.         ElseIf cls_FP.pageNumber = 1 Then
    26.             cls_FP.pageNumber += 1
    27.             showform(cls_FP._frm2)
    28.         ElseIf cls_FP.pageNumber = 2 Then
    29.             cls_FP.pageNumber += 1
    30.             showform(cls_FP._frm3)
    31.         ElseIf cls_FP.pageNumber = 3 Then
    32.             cls_FP.pageNumber += 1
    33.             showform(cls_FP._frm4)
    34.             Me.btnFinish.Enabled = True
    35.         End If
    36.     End Sub
    37.  
    38.     Private Sub showform(ByRef frm As Form)
    39.         Dim ctrl As Control
    40.         For Each ctrl In Me.SplitContainer1.Panel2.Controls
    41.             If TypeOf ctrl Is Form Then
    42.                 Me.SplitContainer1.Panel2.Controls.Remove(ctrl)
    43.                 'If cls_FP.pageNumber = 0 Then
    44.                 'DirectCast(ctrl, Form).Close()
    45.                 'DirectCast(ctrl, Form).Dispose()
    46.                 'End If
    47.             End If
    48.         Next
    49.         frm.TopLevel = False
    50.         Me.SplitContainer1.Panel2.Controls.Add(frm)
    51.         frm.Dock = DockStyle.Fill
    52.         frm.Show()
    53.     End Sub

  5. #5
    Hyperactive Member josep's Avatar
    Join Date
    Sep 2006
    Location
    Barcelona
    Posts
    409

    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:
    1. Private FormStep As Integer = 1
    2.     Private Sub btnBack_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBack.Click
    3.         'Save anything from form here
    4.         FormStep -= 1
    5.         Dim frm As Form
    6.         Select Case FormStep
    7.             Case Is = 0
    8.                 RemoveForms()
    9.                 Me.btnBack.Enabled = False
    10.             Case Is = 1
    11.                 frm = New frm1
    12.                 'If you need to initialize somthing in frm1 do it here
    13.             Case Is = 2
    14.                 frm = New frm2
    15.                 'If you need to initialize somthing in frm2 do it here
    16.             Case Is = 3
    17.                 frm = New frm3
    18.                 'If you need to initialize somthing in frm3 do it here
    19.                 Me.btnFinish.Enabled = False
    20.         End Select
    21.         showform(frm)
    22.     End Sub
    23.  
    24.     Private Sub btnNext_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
    25.         'Save anything from form here
    26.         FormStep += 1
    27.         Dim frm As Form
    28.         Select Case FormStep
    29.             Case Is = 1
    30.                 Me.btnBack.Enabled = True
    31.                 frm = New frm1
    32.                 'If you need to initialize somthing in frm1 do it here
    33.             Case Is = 2
    34.                 frm = New frm2
    35.                 'If you need to initialize somthing in frm2 do it here
    36.             Case Is = 3
    37.                 frm = New frm3
    38.                 'If you need to initialize somthing in frm3 do it here
    39.             Case Is = 4
    40.                 frm = New frm4
    41.                 'If you need to initialize somthing in frm4 do it here
    42.                 Me.btnFinish.Enabled = True
    43.         End Select
    44.         showform(frm)
    45.     End Sub
    46.  
    47.     Private Sub showform(ByRef frm As Form)
    48.         RemoveForms()
    49.         frm.TopLevel = False
    50.         Me.SplitContainer1.Panel2.Controls.Add(frm)
    51.         frm.Dock = DockStyle.Fill
    52.         frm.Show()
    53.     End Sub
    54.  
    55.     Private Sub RemoveForms()
    56.         Dim ctrl As Control
    57.         For Each ctrl In Me.SplitContainer1.Panel2.Controls
    58.             If TypeOf ctrl Is Form Then
    59.                 Me.SplitContainer1.Panel2.Controls.Remove(ctrl)
    60.                 CType(ctrl, Form).close()
    61.                 CType(ctrl, Form).dispose()
    62.             End If
    63.         Next
    64.     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
    Useful links:DB connection strings ADO.NET VB.NET Tutorials

    • Don't forget to close the thread if solved
    • If this post helps you rate it

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    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

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    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!

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    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.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [RESOLVED] Any Better Way? Advise.

    Actually another brilliant Idea is using a TabControl. then i Hide the Tabs here and there.
    Thanks JMC.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width