Results 1 to 11 of 11

Thread: [RESOLVED] I can't discover how to 'Cast' a Form.

  1. #1

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,429

    Resolved [RESOLVED] I can't discover how to 'Cast' a Form.

    Hi,

    I've spent most of this forenoon looking through MSDN trying to find how to reference a Form within a subroutine...

    Clicking 'help' from a VB 2012 code page only gives me an advert for Microsoft's VS 2013... Some help !
    I made many attempts to just hack my way through it, using what I could find in MSDN, all (obviously) without success.
    Sometimes I got a code line without any warnings (rarely) but when I stepped through the code, as I stepped past Fm = .....
    The 'Watch' window showed that Fm was still 'nothing'.

    In the end I used this code: -
    vb.net Code:
    1. Public Sub PlaceMe(ByVal Num As Integer)
    2.         Dim Fm As Form
    3.  
    4.         Select Case Num
    5.             Case Is = 3
    6.                 Fm = Form3
    7.             Case Is = 4
    8.                 Fm = Form4
    9.             Case Is = 5
    10.                 Fm = Form5
    11.             Case Else
    12.                 Fm = Form3
    13.         End Select
    14.  
    15.         Fm.Show()
    16.  
    17.     End Sub
    Not very elegant, but at least it works...
    Not too bad in this case I guess, but suppose I'd wanted to select from (say) 20 Forms.


    Poppa.
    Along with the sunshine there has to be a little rain sometime.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,760

    Re: I can't discover how to 'Cast' a Form.

    Typically what I do in that scenario is set the Tag property of the objects to a unique ID. Then I create a collection of those objects. Finally I use LINQ to select the object that matches object by it's unique ID. Take a look at this example, it assumes that you've set the Tag property of each form to it's own unique ID and have an array named forms which is some collection(array, list(of Form), etc.) that has already stored the various forms:
    Code:
    Private Sub PlaceMe(ByVal index As Integer)
        Dim frm As Form = forms.FirstOrDefault(Function (f) f.Tag.ToString = index.ToString)
        frm.Show()
    End Sub
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: I can't discover how to 'Cast' a Form.

    What I'd do is load the forms into a dictionary, and you can use any key you like to reference the forms.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,538

    Re: I can't discover how to 'Cast' a Form.

    1) the case statement should be
    Code:
            Select Case Num
                Case 3
                    Fm = Form3
    You're testing for a value, not a range or a type... might be part of your "nothing" problem.

    2) you should use a new instance of the form, try to avoid the default instances if you can...
    Code:
            Select Case Num
                Case 3
                    Fm = New Form3
    3) Why the function in the first place? If you know you're going to call PlaceMe(3), just create a variable of that form type and instanciate it
    Code:
    'PlaceMe(3)
    Dim frm3 = New Form3
    frm3.Show()
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: I can't discover how to 'Cast' a Form.

    Why pass in a number? ... wouldn't you be better using an enum or by passing the type of form you want to open?

    Kris

  6. #6
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: I can't discover how to 'Cast' a Form.

    I think you've gone down a wrong path here. Can you show us the methods that call the PlaceMe method? It definitely feels like you're solving the wrong problem here.

  7. #7

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,429

    Re: I can't discover how to 'Cast' a Form.

    Hi guys, I'm sorry for the tardy reply... I'm back home now, been travelling all day.

    Oh Dear, looks like I really screwed up that question...

    Techgnome, I have no problem with the code I posted, none, it all works as I expected, and as I said
    Not very elegant, but at least it works...
    I'll try to ask the question a different way...

    Here's a bit of code as an example, it addresses one ListBox from several on a form without specifically naming the ListBox in code, I know it works.
    vb.net Code:
    1. Private Sub Something(ByVal ThisBox As Integer)
    2.  
    3.         LstBx = DirectCast(Me.Controls("ListBox" & ThisBox.ToString), ListBox)
    4.         If LstBx.Items.count > 0 Then
    5.  
    6.            'Some stuff in here...
    7.  
    8.            LstBx.Items.RemoveAt(0)
    9.         End If
    10.  
    11.     End Sub
    What I've been trying to do is address a Form in a similar way...

    I just want to be able to select one form from a list of several in (say) Form1, pass the selection to my subroutine, wherein use a variable to address that Form, without having to specifically name the Form in code, then show and work with that Form.

    Poppa.
    Along with the sunshine there has to be a little rain sometime.

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,760

    Re: I can't discover how to 'Cast' a Form.

    What you should do is create a List(Of Forms) and bind that List to your ListBox. Then when the user selects an item you can get the form from the List's index by using the ListBox's SelectedIndex property.

    Or since you don't want the Form's name, then simply add the numbers 1 to the List's count. Then when the user selects an item you can get the form from the List's index by using the same way.

    Edit: Here's an example:
    Code:
    Option Strict On
    Option Explicit On
    Public Class Form1
        'Globals
        Private forms As List(Of Form)
    
        'Controls
        Private listBox1 As ListBox
        Private button1 As Button
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            forms = New List(Of Form) From {{Me}, {Form2}, {Form3}, {Form4}}
            listBox1 = New ListBox With {.Location = New Point(5, 5)}
            button1 = New Button With {.Left = listBox1.Left, .Text = "Load", .Top = listBox1.Bottom + 5}
    
            For i As Integer = 1 To forms.Count
                listBox1.Items.Add(i.ToString)
            Next
    
            Me.Controls.AddRange({listBox1, button1})
    
            AddHandler button1.Click, AddressOf button1_Click
        End Sub
    
        Private Sub button1_Click(sender As Object, e As EventArgs)
            If listBox1.SelectedIndex > -1 Then
                Dim selectedForm As Form = forms.Item(listBox1.SelectedIndex)
                selectedForm.Show()
            End If
        End Sub
    
    End Class
    Last edited by dday9; Jan 8th, 2015 at 06:15 PM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  9. #9

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,429

    Re: I can't discover how to 'Cast' a Form.

    Quote Originally Posted by dday9 View Post
    What you should do is create a List(Of Forms) and bind that List to your ListBox. Then when the user selects an item you can get the form from the List's index by using the ListBox's SelectedIndex property.

    Or since you don't want the Form's name, then simply add the numbers 1 to the List's count. Then when the user selects an item you can get the form from the List's index by using the same way.
    Oh my word!
    I'm not quite sure what you're trying to tell me, but you've given me the answer...

    vb.net Code:
    1. Public Sub PlaceMe(ByVal Num As Integer)
    2.  
    3.         ' Subroutine to place Form2, alongside the form selected in the (now hidden) Form1
    4.         ' Equally spaced and with tops aligned on screen.
    5.  
    6.         Dim x, y As Integer
    7.         Dim Fm() As Form = {Form3, Form4, Form5}
    8.  
    9.         Num -= 3
    10.         Fm(Num).Show()
    11.         y = Fm(Num).Location.Y
    12.         x = Fm(Num).Width + Form2.Width
    13.         x = My.Computer.Screen.Bounds.Size.Width - x
    14.         x /= 3
    15.         Form2.Show()
    16.         RePos(Form2, x, y)
    17.         x += x + Form2.Width
    18.         RePos(Fm(Num), x, y)
    19.  
    20.     End Sub
    21.  
    22. Public Sub RePos(ByVal This As Control, ByVal X As Integer, ByVal Y As Integer)
    23.  
    24.         Dim Here As New Point(X, Y)
    25.         This.Location = Here
    26.  
    27.     End Sub

    Line 7 is neat, simple and works a treat... And was really all I needed, I don't know why I didn't think of it myself... Too many jars in the bar on vacation I suspect.

    With regard to line 9, yes I could change the parameters for the form numbers to 0 to 2 instead of 3 to 5 where they're called, but this way passes the number of the form and is easier to understand.
    As a point of interest, the alignment of the tops doesn't work unless I 'show' the forms before I reposition them.
    Oh... I should show Sub Repos.... I'll add it before I post !



    Thank you dday9.

    Poppa.
    Along with the sunshine there has to be a little rain sometime.

  10. #10

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,429

    Re: I can't discover how to 'Cast' a Form.

    Oh, I just realised... I've not tried it, but I bet I could just pass the name of the form: -

    PlaceMe(Form3 )
    (Think I'll try it before I go to bed)

    Poppa.

    Edit:

    Sub PlaceMe(ByVal Fm As Form)

    Damn ! Ain't that simple !

    Pop.
    Last edited by Poppa Mintin; Jan 8th, 2015 at 07:30 PM. Reason: Result of test.
    Along with the sunshine there has to be a little rain sometime.

  11. #11
    Frenzied Member Gruff's Avatar
    Join Date
    Jan 2014
    Location
    Scappoose Oregon USA
    Posts
    1,293

    Re: I can't discover how to 'Cast' a Form.

    Following DDays lead I guess I would create the forms and listbox at design-time then store the list of forms within the listbox itself.

    In my example I am displaying the form title in the listbox but you could use the form tag property and make it numbers if you like. As written form1 stays put.

    Code:
    Public Class Form1
      Dim CurrentForm As Form = Nothing
    
      Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim AllForms As List(Of Form)
        AllForms = New List(Of Form) From {{Form2}, {Form3}, {Form4}}
        ListBox1.DisplayMember = "Text"
        ListBox1.Items.AddRange(AllForms.ToArray)
      End Sub
    
      Private Sub ListBox1_Click(sender As System.Object, e As System.EventArgs) Handles ListBox1.Click
        If Not CurrentForm Is Nothing Then
          CurrentForm.Hide()
        End If
        CurrentForm = CType(ListBox1.SelectedItem, Form)
        CurrentForm.Show()
        CurrentForm.Location = New Point(Me.Right, Me.Top)
      End Sub
    End Class
    Last edited by Gruff; Jan 8th, 2015 at 08:13 PM.
    Burn the land and boil the sea
    You can't take the sky from me


    ~T

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