-
Jan 7th, 2015, 09:48 AM
#1
Thread Starter
PowerPoster
[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:
Public Sub PlaceMe(ByVal Num As Integer)
Dim Fm As Form
Select Case Num
Case Is = 3
Fm = Form3
Case Is = 4
Fm = Form4
Case Is = 5
Fm = Form5
Case Else
Fm = Form3
End Select
Fm.Show()
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.
-
Jan 7th, 2015, 09:53 AM
#2
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
-
Jan 7th, 2015, 10:11 AM
#3
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."
-
Jan 7th, 2015, 10:13 AM
#4
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
-
Jan 7th, 2015, 09:48 PM
#5
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
-
Jan 8th, 2015, 04:51 AM
#6
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.
-
Jan 8th, 2015, 06:04 PM
#7
Thread Starter
PowerPoster
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:
Private Sub Something(ByVal ThisBox As Integer) LstBx = DirectCast(Me.Controls("ListBox" & ThisBox.ToString), ListBox) If LstBx.Items.count > 0 Then 'Some stuff in here... LstBx.Items.RemoveAt(0) End If 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.
-
Jan 8th, 2015, 06:10 PM
#8
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.
-
Jan 8th, 2015, 07:09 PM
#9
Thread Starter
PowerPoster
Re: I can't discover how to 'Cast' a Form.
Originally Posted by dday9
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:
Public Sub PlaceMe(ByVal Num As Integer)
' Subroutine to place Form2, alongside the form selected in the (now hidden) Form1
' Equally spaced and with tops aligned on screen.
Dim x, y As Integer
Dim Fm() As Form = {Form3, Form4, Form5}
Num -= 3
Fm(Num).Show()
y = Fm(Num).Location.Y
x = Fm(Num).Width + Form2.Width
x = My.Computer.Screen.Bounds.Size.Width - x
x /= 3
Form2.Show()
RePos(Form2, x, y)
x += x + Form2.Width
RePos(Fm(Num), x, y)
End Sub
Public Sub RePos(ByVal This As Control, ByVal X As Integer, ByVal Y As Integer)
Dim Here As New Point(X, Y)
This.Location = Here
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.
-
Jan 8th, 2015, 07:17 PM
#10
Thread Starter
PowerPoster
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.
-
Jan 8th, 2015, 07:56 PM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|