Creating arrays of controls in visual basic - vb.net

Ok so here's the relevant code:
Public Shared compSelect(9) As ComboBox
Public Shared compPercent(9) As TextBox
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Data.LoadComponents("C:/Users/Jon/Documents/Visual Studio 2013/Projects/QuickBlend/QuickBlend/QuickBlend/Resources/databaseText.txt")
MsgBox("finished loading")
MainForm.compSelect = {CompSelect1, CompSelect2, CompSelect3, CompSelect4, CompSelect5, CompSelect6, CompSelect7, CompSelect8, CompSelect9, CompSelect10}
MainForm.compPercent = {CompPercent1, CompPercent2, CompPercent3, CompPercent4, CompPercent5, CompPercent6, CompPercent7, CompPercent8, CompPercent9, CompPercent10}
For Each box As ComboBox In MainForm.compSelect
box.DataSource = Data.Components
box.DisplayMember = "Name"
For Each comp As String In Data.ComponentNames
box.Items.Add(comp)
Next
MsgBox("looped")
Next
MsgBox("finished loop")
End Sub
As you can see, I've placed various MsgBoxes to see exactly whats going on. It never displays the "looped" message box. Can anybody explain to me why it's completely skipping the for loop? Been working on this for a while and got fed up with it. Thanks in advance for the help! =)

MainForm.compSelect should be Me.compSelect since this is the instance(has been filled with comboboxes) and not just the fully qualified name of the object that has not been filled.

Your problem is that you are setting the datasource for the comboBox, and then trying to add items to it. .NET does not like this, and will just exit the Sub that tries to do this, without warning (unless you have exception handling added in). Your code should be...
Public Shared compSelect(9) As ComboBox
Public Shared compPercent(9) As TextBox
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Data.LoadComponents("C:/Users/Jon/Documents/Visual Studio 2013/Projects/QuickBlend/QuickBlend/QuickBlend/Resources/databaseText.txt")
MsgBox("finished loading")
MainForm.compSelect = {CompSelect1, CompSelect2, CompSelect3, CompSelect4, CompSelect5, CompSelect6, CompSelect7, CompSelect8, CompSelect9, CompSelect10}
MainForm.compPercent = {CompPercent1, CompPercent2, CompPercent3, CompPercent4, CompPercent5, CompPercent6, CompPercent7, CompPercent8, CompPercent9, CompPercent10}
For Each box As ComboBox In MainForm.compSelect
box.DataSource = Data.Components
box.DisplayMember = "Name"
'take this stuff out, it is not needed
'For Each comp As String In Data.ComponentNames
'box.Items.Add(comp)
'Next
MsgBox("looped")
Next
MsgBox("finished loop")
End Sub

Related

When I try to populate a combo box it doesn't populate

Earlier I was able to populate a combo box depending on what another combo box has selected, I then continued on with writing code, and I come back to test it again, and it no longer populates that list, it does with another list, but one of them doesn't. So it populates the drink's, but not the main. I'm not sure what's gone on for this to happen, it all seems okay to me.
Public Shared cmbCatDrinks As String = "Drinks"
Public Shared cmbCatMain As String = "Main"
Public Shared cmbCatBlank As String = ""
Public Shared cmbItemBlank As String = ""
Private Sub frmRestaurantOrd_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cmbCat.Items.Add(cmbCatBlank)
cmbCat.Items.Add(cmbCatDrinks)
cmbCat.Items.Add(cmbCatMain)
End Sub
Private Sub cmbCat_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbCat.SelectedIndexChanged
If cmbCat.SelectedItem = "Drinks" Then
cmbItem.Items.Add("Water")
cmbItem.Items.Add("Orange Juice")
cmbItem.Items.Add("Coca Cola")
cmbItem.Items.Add("Beer")
cmbSize.Items.Clear()
ElseIf cmbCat.SelectedItem = "Main" Then
cmbItem.Items.Add("Cheese and Tomato")
cmbItem.Items.Add("Ham and Pineapple")
cmbItem.Items.Add("Vegetarian")
cmbItem.Items.Add("Meat Feast")
cmbItem.Items.Add("Seafood")
cmbSize.Items.Add("Small")
cmbSize.Items.Add("Medium")
cmbSize.Items.Add("Large")
cmbItem.Items.Clear()
Else cmbCat.SelectedItem = cmbCatBlank
cmbItem.Items.Clear()
End If
End Sub
You are clearing the items you just added to cmbItem when "Main" is selected.
ElseIf cmbCat.SelectedItem = "Main" Then
cmbItem.Items.Add("Cheese and Tomato")
cmbItem.Items.Add("Ham and Pineapple")
cmbItem.Items.Add("Vegetarian")
cmbItem.Items.Add("Meat Feast")
cmbItem.Items.Add("Seafood")
cmbSize.Items.Add("Small")
cmbSize.Items.Add("Medium")
cmbSize.Items.Add("Large")
cmbItem.Items.Clear() ' <-- this is clearing all the items you just added.

list of controls for each form

iam using vb.net 2008 ,i have 2 listboxes, one is for listing all forms name in my project and works fine, but what i need is when i click in the forms list, the other listbox should show me all the controls in the selected form name in listbox1! i have been trying and no luck.please help. this the code for listing all forms
Dim myAssembly As System.Reflection.Assembly = System.Reflection.Assembly.GetExecutingAssembly()
Dim types As Type() = myAssembly.GetTypes()
For Each myType As Object In types
If myType.BaseType.FullName.ToString.ToUpper = "SYSTEM.WINDOWS.FORMS.FORM" Then
ListBox1.Items.Add(myType.Name)
End If
Next
and this what i was trying in list2
Dim f As New Form
f.Name = ListBox1.SelectedItem.ToString
For Each c As Control In f.Controls
ListBox2.Items.Add(c.Name)
Next
OK! After several hours hunting - I hope this works in vb2008. It works in vb2015, but let's hope! The code to get a list of all forms came from another source. My problem in finding an answer has always resulted in a error in runtime casting- but finally this seems to work. Hopefully, it works for you as well.
Public Class Form1
Dim allforms() As Form
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
allforms = (From t As Type In Me.GetType().Assembly.GetTypes()
Where t.BaseType Is GetType(Form)
Let f = DirectCast(Activator.CreateInstance(t), Form)
Select f).ToArray
For Each frm As Form In allForms
ListBox1.Items.Add(frm.Name)
Next
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
For Each c As Control In allforms(ListBox1.SelectedIndex).Controls
ListBox2.Items.Add(c.Name)
Next
End Sub
End Class

Checkboxlist applying If statements

I am stating VB and since it is so close to VBScript I have been having fun with it. But now I have come across the "Checkboxlist".
My boss saw me making a Windows Forms Application and asked me to make him a interface (GUI) for one of his batch files. In the batch you start by choosing between lines 1 through 10 and it does the rest. So I made a Checkboxlist and made check-boxes going from 1 to 10. Now I am not sure how to tell it that when I click a button a if statement looks at what has been checked and take to appropriate action.
I think i am suppose to start with something like
If CheckedListBox1.Items() = True then
But i know this does not work.
Anything thing will help.
Thank you.
It sounds like you're looking for the ItemCheck event. This event is fired when the checked state of an item changes.
Private Sub HandleCheckedListBox1ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
Dim item As Object = Me.CheckedListBox1.Items.Item(e.Index)
Dim text As String = Me.CheckedListBox1.GetItemText(item)
Select Case e.CurrentValue
Case CheckState.Unchecked
'...
Case CheckState.Checked
'...
Case CheckState.Indeterminate
'...
End Select
End Sub
Or iterate all checked items:
Private Sub HandleButton1Click(sender As Object, e As EventArgs) Handles Button1.Click
For Each item As Object In Me.CheckedListBox1.CheckedItems
Dim text As String = Me.CheckedListBox1.GetItemText(item)
'...
Next
End Sub

Check if any dialog is open

Does anybody see my mistake here?
I am unable to recognize if a form is shown as a dialog in my app.
Public Class Form1
Private m As Form2
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
Me.Text = DateTime.Now.ToLongTimeString & " " & IsAnyDialogShown()
End Sub
Public Function IsAnyDialogShown() As Boolean
For Each f As Form In Me.OwnedForms
If f.Owner Is Me Then
Return True
End If
Next
End Function
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
m = New Form2
m.ShowDialog()
End Sub
End Class
What you are looking is for the property modal.
Check if the form's modal property is true (that is meaning that the form is showed with ShowDialog)
For Each f As Form In Me.OwnedForms
If f.Modal=True Then
'your code here
End If
Next
Now for your mistake (I haven't visual studio to try it right now) but your IsAnyDialogShown(), it seems that it returns always true :
For Each f As Form In Me.OwnedForms ' (So f belongs to Me)
If f.Owner Is Me Then 'f.Owner is always me because you are seaching in forms that have as owner the Me form
Return True
End If
Next
Hope I helped a little.
Tell me if I can do something more
So after your comments.
Try this:
For Each frm as Form In Application.OpenForms
If frm.Modal=True Then
'do something
'Actually you should have only one form because only one can be in modal state
end if
Next
You need to check Visible property of the form, which is the boolean.
If it is true, then form is shown, else it's hidden.
That's just doing forms owned by Me. Nothing to do with whether they are dialog forms.
ie.e it will pick up normal forms.
Also if you want this to work as expected , you should use the overload where you pass the owner.
as in m.ShowDialog(Me);
Not something I've ever done but if Owner isn't me in Me.OwnedForms I want my money back.

Search ListBox elements in VB.Net

I'm migrating an application from VB6 to VB.Net and I found a change in the behavior of the ListBox and I'm not sure of how to make it equal to VB6.
The problem is this:
In the VB6 app, when the ListBox is focused and I type into it, the list selects the element that matches what I type. e.g. If the list contains a list of countries and I type "ita", "Italy" will be selected in the listbox.
The problem is that with the .Net version of the control if I type "ita" it will select the first element that starts with i, then the first element that starts with "t" and finally the first element that starts with "a".
So, any idea on how to get the original behavior? (I'm thinking in some property that I'm not seeing by some reason or something like that)
I really don't want to write an event handler for this (which btw, wouldn't be trivial).
Thanks a lot!
I shared willw's frustration. This is what I came up with. Add a class called ListBoxTypeAhead to your project and include this code. Then use this class as a control on your form. It traps keyboard input and moves the selected item they way the old VB6 listbox did. You can take out the timer if you wish. It mimics the behavior of keyboard input in Windows explorer.
Public Class ListBoxTypeAhead
Inherits ListBox
Dim Buffer As String
Dim WithEvents Timer1 As New Timer
Private Sub ListBoxTypeAhead_KeyDown(sender As Object, _
e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.A To Keys.Z, Keys.NumPad0 To Keys.NumPad9
e.SuppressKeyPress = True
Buffer &= Chr(e.KeyValue)
Me.SelectedIndex = Me.FindString(Buffer)
Timer1.Start()
Case Else
Timer1.Stop()
Buffer = ""
End Select
End Sub
Private Sub ListBoxTypeAhead_LostFocus(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.LostFocus
Timer1.Stop()
Buffer = ""
End Sub
Public Sub New()
Timer1.Interval = 2000
End Sub
Private Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
Timer1.Stop()
Buffer = ""
End Sub
End Class
As you probably know, this feature is called 'type ahead,' and it's not built into the Winform ListBox (so you're not missing a property).
You can get the type-ahead functionality on the ListView control if you set its View property to List.
Public Function CheckIfExistInCombo(ByVal objCombo As Object, ByVal TextToFind As String) As Boolean
Dim NumOfItems As Object 'The Number Of Items In ComboBox
Dim IndexNum As Integer 'Index
NumOfItems = objCombo.ListCount
For IndexNum = 0 To NumOfItems - 1
If objCombo.List(IndexNum) = TextToFind Then
CheckIfExistInCombo = True
Exit Function
End If
Next IndexNum
CheckIfExistInCombo = False
End Function