How do i use an array to represent a form, to show and hide forms in VB - vb.net

I want to use a array to help me switch between forms in a quiz that i have to create for a school assignment. When a question is correct it shows the correct form. I am using one button to go to the next form. There are 20 questions each with its own form. This is what i need:
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
Me.Hide()
arrayforms(count).Show()
Thanks

First Collect all of your questionary forms into Array. Name your questionary forms as "Questionary1", "Questionary2" something like that, or set tag to you forms. Then find your form by index and create an instance and ShowDialog, that's it. Try following code.
Public Class Form1
Private forms(20) As Type
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim yourFormIndex = 3
Dim frm As Form = Activator.CreateInstance(forms(yourFormIndex))
frm.ShowDialog()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim myAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
Dim types As Type() = myAssembly.GetTypes()
Dim index As Integer = 1
For Each myType As Object In types
If myType.BaseType.FullName.ToString.ToUpper = "SYSTEM.WINDOWS.FORMS.FORM" Then
If (myType.Name.ToString.StartsWith("Questionary")) Then
forms(index) = myType
index = index + 1
End If
End If
Next
End Sub
End Class

Related

Passing data from one form to another form's textboxes

I am trying to pass the selected nodes from a treeview to another form that is displayed in the text boxes.
this is my code in TreeView1_NodeMouseDoubleClick event
the code works fine when the data from the selected treeview is shown in the same form, but the problem is when I want to pass it to the other form 4 nothing is shown and the debug shows me that if it receives the values ​​but they are not reflected, maybe my code to refer to the other form is wrong I would like a support with this case.
Private Sub TreeView1_NodeMouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
Dim nodeKey As String = TryCast(e.Node.Tag, String)
Dim form4 As New Form4
' Dim nodetext As String = TryCast(e.Node.Tag, String)
'Dim nodeKey As String = DirectCast(e.Node.Tag, String)
If nodeKey IsNot Nothing AndAlso nodeKey.StartsWith("DIST") Then
'You have double clicked a district node
Dim IDDISTRITO As Integer = Integer.Parse(nodeKey.Substring(4))
form4.lblco.Text = IDDISTRITO
'Do something with the district id here
'...
form4.txtdis.Text = e.Node.Text
form4.txtpro.Text = e.Node.Parent.Text
form4.txtdepa.Text = e.Node.Parent.Parent.Text
End If
End Sub
so I call form 3 where is the treeview
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim for3 As New Form3
for3.Show()
End Sub
As you assumed, yes you are referencing the forms incorrectly. Please follow this technique. In form3 class, declare a public variable Public Dim form4 As New Form4.
Then modify the form3 calling procedure like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim for3 As New Form3
for3.form4 = Me
for3.Show()
End Sub
Now modify your existing code to this :
Private Sub TreeView1_NodeMouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
Dim nodeKey As String = TryCast(e.Node.Tag, String)
' Dim nodetext As String = TryCast(e.Node.Tag, String)
'Dim nodeKey As String = DirectCast(e.Node.Tag, String)
If nodeKey IsNot Nothing AndAlso nodeKey.StartsWith("DIST") Then
'You have double clicked a district node
Dim IDDISTRITO As Integer = Integer.Parse(nodeKey.Substring(4))
form4.lblco.Text = IDDISTRITO
'Do something with the district id here
'...
form4.txtdis.Text = e.Node.Text
form4.txtpro.Text = e.Node.Parent.Text
form4.txtdepa.Text = e.Node.Parent.Parent.Text
End If
End Sub
I have not tested the above code, but I believe you have got the idea.
Well I managed to solve it, the simplest thing was complicated, I publish the solution.
form 4 by calling 3
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim for3 As New Form3
AddOwnedForm(for3)
for3.ShowDialog()
End Sub
just add this code and voila
Dim form4 As Form4 = CType(Owner, Form4)

Visual Basic .net, create reference to an object

I'm trying to create a simple application. It has 2 listboxes, but both lists are mostly the same. So in my code, I want to be able to execute the same code on either listbox easily.
In VB6, I could name both controls the same, assign an index to them, and that works. Here in VB.net 2008, it seems to not be possible.
What I ideally want, is to create a variable during the program that I can assign to either of the listbox, and then during the rest of the code use this new variable to control the listbox attached to it.
Here's an example:
Private Sub lVegetables_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lVegetables.Click
ListToEditor(0, lVegetables.SelectedIndex)
End Sub
Private Sub lFruits_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lFruits.Click
ListToEditor(1, lFruits.SelectedIndex)
End Sub
Private Sub ListToEditor(ByVal iList, ByVal iIndex)
Select Case iList
Case 0
Dim lList As lVegetables
Case 1
Dim lList as lFruits
End Select
tEditor.text = iList.item(iIndex)
End Sub
Is there a way I can use a different variable to reference to a listbox on the fly, or can I assign an index to them so they have the same name?
You can use the same handler for both ListBoxes and cast the sender:
Sub ListBox_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lVegetables.Click, lFruits.Click
Dim lb = DirectCast(sender, ListBox)
ListToEditor(lb, lb.SelectedIndex)
End Sub
Private Sub ListToEditor(lb As ListBox, index as Int32)
tEditor.text = lb.Items(index).ToString()
End Sub
The whole code could be simplified to:
Sub ListBox_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lVegetables.Click, lFruits.Click
Dim lb = DirectCast(sender, ListBox)
If lb.SelectedIndex >= 0 Then tEditor.Text = lb.SelectedItem.ToString()
End Sub

vb.net textbox not displaing value

In my frmMain class I have a textbox(txtCustomer) which populates from a database. I want to pass this value to another textbox in frmDepartment(txtDeptCustomer).
I am failing to see the logic of why the code I am using is not displaying a value in txtDeptCustomer. I can query the database ok with the variable, so the string is being passed through, but just not displaying in txtDeptCustomer. I would be grateful if someone could point out my error. Thanks
frmDepartment
Dim customer As Object = frmMain.txtCustomer.Text
This is passing correct value to db.
sql = "SELECT * FROM Departments where Customer = '" & CType(customer, String) & "'"
textbox txtDeptCustomer <--- NOT DISPLAYING VALUE
Private Sub txtDeptCustomer_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDeptCustomer.TextChanged
txtDeptCustomer.Text = CType(customer, String)
End Sub
Public Customer as String = Nothing
Private Sub btnDO_Click(sender As Object, e As EventArgs) Handles btnDoWork.Click
Customer = Database Call
Dim frmDepartmentInstance as new frmDepartment
frmDepartment.ShowDialog(Me)
End Sub
Then in the Load event of frmDepartment you can say
txtDeptCustomer.Text = frmMain.Customer
Proof of concept: New Project. Two forms | Form 1 has a button and a textbox | Form2 just has textbox
Public Class Form1
Public Test As String = Nothing
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Test = TextBox1.Text
Dim frm2 As New Form2
frm2.ShowDialog(Me)
End Sub
End Class
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = Form1.Test
End Sub
End Class
You must declare then as public your customer variable in frmDepartment, like:
Public customer as String
And in the button click from your frmMain you pass the value like:
frmDepartment.customer = txtCustomer.Text
frmDepartment.Show()
And then in Loading your frmDepartment you have now the option of assigning customer to txtDeptCustomer like:
Private Sub frmDepartment_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
txtDepartment.Text = customer
End Sub

vb2010 pass data from one form to another

I am trying to pass a value of a textbox on frmMain to a textbox on frmDepartment. I have tried the following code which I thought would work but it dosen't. I ma a new user to VB and come from a php background where it would have been a simple task of setting a seesion. Can anyone help with this? If you need to see more code, please ask. many thanks
txtDeptCustomer.Text = frmMain.txtCustomerActive.Text
In frmMain, I am getting value like this:
Dim value As Object = UserDataGridView.Rows(e.RowIndex).Cells(0).Value
txtCustomerActive.Text = CType(value, String)
Private Sub btnDepts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDepts.Click
frmDepartment.Show()
End Sub
Which is showing in frmMain ok.
In frmDepartment I have this code
Private Sub txtDeptCustomer_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDeptCustomer.TextChanged
'Dim customer As String
txtDeptCustomer.Text = frmMain.txtCustomerActive.Text
End Sub
instead of putting the code within the txtDeptCustomer.TextChanged sub, try putting it within the frmDepartment_load:
Private Sub frmDepartment_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
txtDeptCustomer.Text = frmMain.txtCustomerActive.Text
End Sub
or you could set the frmDepartment text box text on the frmMain button click:
Private Sub btnDepts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDepts.Click
frmDepartment.txtDeptCustomer.Text = txtCustomerActive.Text
frmDepartment.Show()
End Sub

Why is my string variable in Audiobooks(secondaryform) not considered a member of ShoppingCart(mainform)

I instantiated a place in memory with the Dim MyCollection As New AudioBooks statement in the Public Class ShoppingCart Shopping cart form. Here is where I declared my variable in audiobooks Dim StrLearnCalclusInOneDayAB As String in the Public Class AudioBooks form.
If ListBox1.Items.Contains(Me.MyCollection.StrLearnCalclusInOneDayAB)
Here is my main syntax error, I need to look through listbox1 to see if the variable from audiobooks is there. I qualifying this wrong, and i know its so frustratingly simple.
Here is the full code. I need a VB tutor and will pay $5 per question below is the code in full.
Public Class ShoppingCart
Dim Tax As Decimal
Dim PB1 As Decimal
Dim AB1 As Decimal
Dim MyCollection As New AudioBooks
'My overall objective is to move the strings from audiobooks and printbooks to shopping cart and calculate the total
'I was instructed to use a module
Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
Tax = 0.06
End Sub
Private Sub PrintBooksToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintBooksToolStripMenuItem.Click
'Create an instance of the printbook
Dim frmPrintBook As New PrintBook
'Display the form in modal styple
frmPrintBook.ShowDialog()
End Sub
Private Sub AudioBooksToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AudioBooksToolStripMenuItem.Click
'Create an instance of the audiobook
Dim frmAudioBook As New AudioBooks
'Display the form in modal style
frmAudioBook.ShowDialog()
End Sub
Public Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
'Dim MyCollection As New AudioBooks
'I want to run an if else statement which checks Listbox1 to see what books are in the cart and calculates the total
'I'm having trouble finding these variables(from audiobooks) with intelligsense, 'StrLearnCalclusInOneDayAB' etc.
'I'm pretty sure I need a new instance of memory from the audio bookcooks class to use the object variables
'declared in that class. I'm having trouble getting the syntax right. Intelligsense only will find the lstboxaudio
'The book says, form's class-level variables are accessible to statements in the form file,
'they are not accessible by default to statements outside the form file
'I want the code to look like this, only lstboxAudio comes up in Intelligsense, why is this, I want StrLearnCalclusInOneDayAB to
'Also I'm not sure I'm using contains correctly, hope I was clear
'I will eventually need to total the books in the cart, why Can't intellisense find the variable?
'This is my primary question
If ListBox1.Items.Contains(MyCollection.StrLearnCalclusInOneDayAB) Then
End If
Cart.IDidItYourWay = 11.95
Cart.TheHistoryofScotland = 14.5
Cart.LearnCalculusInOneDay = 29.95
Cart.FeelTheStress = 18.5
Cart.LearnCalculusInOneDayAB = 29.95
Cart.RelaxationTechniques = 11.5
Cart.TheScienceOfBodyLanguage = 12.95
Cart.TheHistoryOfScotlandAB = 14.5
'I feel I shouldn't have to reference an object to change properties in a class, why must I do that?
'This question might not make sense.
End Sub
Private Sub lblSubtotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblSubtotal.Click
End Sub
End Class
Public Class AudioBooks
Public Sub lstboxAudio_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstboxAudio.SelectedIndexChanged
End Sub
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Dim AudioBooks1 As String
'AudioBooks1 = lstboxAudio.SelectedItem.ToString
'Save the selected item to a string
'ShoppingCart.ListBox1.Items.Add(AudioBooks1)
'shopping cart form, listbox1, items, add, that string of audiobooks
Dim StrLearnCalclusInOneDayAB As String
StrLearnCalclusInOneDayAB = lstboxAudio.Items(0).ToString
'If strLearnCalculusInOneDayAB is selected then add to the shopping cart listbox
If lstboxAudio.SelectedIndex = 0 Then
ShoppingCart.ListBox1.Items.Add(StrLearnCalclusInOneDayAB)
End If
' if the selectedindex of lslboxaudio is 0, the string is selected
'then move it to the shoppingcart
Dim StrTheHistoryOfScotlandAB As String
StrTheHistoryOfScotlandAB = lstboxAudio.Items(1).ToString
If lstboxAudio.SelectedIndex = 1 Then
ShoppingCart.ListBox1.Items.Add(StrTheHistoryOfScotlandAB)
End If
Dim StrTheScienceOfBodyLangAB As String
StrTheScienceOfBodyLangAB = lstboxAudio.Items(2).ToString
If lstboxAudio.SelectedIndex = 2 Then
ShoppingCart.ListBox1.Items.Add(StrTheScienceOfBodyLangAB)
End If
Dim StrRelaxationTechniquesAB As String
StrRelaxationTechniquesAB = lstboxAudio.Items(3).ToString
If lstboxAudio.SelectedIndex = 3 Then
ShoppingCart.ListBox1.Items.Add(StrRelaxationTechniquesAB)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class
It relates to the Dim statement being classified private by default in the Audiobooks form. The must explicitly be declared public. Not knowing access levels and static shared variable dynamics creates this Question.