How to save data into database from dynamically created text boxes - vb.net

I have dynamically created text boxes on flowlayout panel. The text box can be any number. I am trying to save those value(integer) from text box into database. It takes me a day to achieve this, and i am a newbie by the way. Please guide me how to achieve this. Thank you so much. I try to save into List(Of...) collection but it only returns last value.Here is how i am trying to achieve this. I declare shared list of List type in another class called clsHelper.
Private Sub saveIntoList(flp As FlowLayoutPanel)
clsHelper.list = New List(Of String)
For Each tb in flp.Controls
If TypeOf tb Is TextBox Then
txtNo = DirectCast(tb,TextBox)
If txtNo.Name = "txtNo" Then
clsHelper.list.Add(txtNo.Text)
End If
End If
Next
End Sub
FlowLayoutPanel with six text boxes

The problem is probably this If statement. Only the content of the TextBox whose name is "txtNo" is added to your list. The content of other TextBoxes with different names are not added. Remove the If and the content all TextBoxes will be added to your list.
If txtNo.Name = "txtNo" Then
clsHelper.list.Add(txtNo.Text)
End If
If TextBoxes are not all immediate children of the FlowLayoutPanel, you will need to modify your code as follows, to recursively iterate all the controls hierarchy below the FlowLayoutPanel.
Private Sub saveIntoList(flp As FlowLayoutPanel)
clsHelper.list = New List(Of String)
_saveIntoList(flp)
End Sub
Private Sub _saveIntoList(control As Control)
If TypeOf control Is TextBox Then
clsHelper.list.Add(DirectCast(control, TextBox).Text)
Return
End If
For Each child As Control In control.Controls
_saveIntoList(child)
Next
End Sub

Related

Making a List(Of ...) from multiple form object

So I'm trying to make a list of more than one type of object, which I done a research on and found out it's, but I also researched into structures and found I can possibly make a use of that instead, but it doesn't return itself as a list.
Imports WinText = System.Windows.Forms.TextBox
Imports WinCombo = System.Windows.Forms.ComboBox
Public Class PSS
Public EntryItems = New List(Of Elements)
Structure Elements
Dim xSchool As WinCombo
Dim xClass As WinCombo
Dim xID As WinCombo
Dim xName As WinText
End Structure
End Class
Edit: So on a form I have a set of ComboBoxes and TextBoxes (about 20 more than on the example here). I want to put them into a list so that when I will be retrieving their results, or getting their names for SQL references, or anything similar, I will be able put them into a loop, thus making the code more efficient and shorter to do.
Structure has similarities to Class and is not a list.
(Read more about here)
You can create a List(Of Structure) but you need to populate it first, like djv stated already in comments.
Private Sub Populate()
Dim list As New List(Of Elements)
list.Add(New Elements)
'Add as much Elements as you want
End Sub
Assuming your Controls are placed on a Form named Form1.
For Each ctrl As Control In Form1.Controls
If TypeOf ctrl Is TextBox Then
Dim tb As TextBox = DirectCast(ctrl, TextBox)
'Do whatever you want with the current TextBox
ElseIf TypeOf ctrl Is ComboBox Then
Dim chk As ComboBox = DirectCast(ctrl, ComboBox)
'Do whatever you want with the current ComboBox
End If
Next
At first, credits to MatSnow.
Taking his concept of As Control I was able to make a List(Of Control) which allowed me to make and add items to it.
Dim ControlList As New List(Of Control)
ControlList.Add(ComboBox1)
ControlList.Add(TextBox1)
'And so on

Looping through all Combo boxes on a VB form

I am making a grade application for a school project and I am wondering how I can loop through and check a value in all combo boxes on a certain form, I have 19 units to check; trying to be efficient without making 19 case statements. I have tried an array and me.controls.
Try this :
For Each c As Control In Me.Controls.OfType(Of ComboBox)()
'You cann acces to ComboBox her by c
Next
Note that if you have controls host by containers (TabControl, SPlitPanel, etc.) you may not find all of your controls.
Here is a recursive call that should return all of your controls:
Sub GetControlList(container As Control, ByVal ctlList As List(Of Control))
For Each child As Control In container.Controls
ctlList.Add(child)
If (child.HasChildren) Then
GetControlList(child, ctlList)
End If
Next
End Sub
I have on occasion had problems with the controls on SplitContainer panels so if you use a splitter make sure you are getting all your controls.
Once you have a complete list of controls you can operate on them. This sample is working with DataGridView controls:
Dim ctrls As New List(Of Control)
GetControlList(Me, ctrls)
For Each dgv As DataGridView In ctrls.OfType(Of DataGridView)()
AddHandler dgv.DataError, AddressOf DataGridView_DataError
Debug.Print(dgv.Name)
Next
FYI the generic data error code:
Private Sub DataGridView_DataError(sender As Object, e As DataGridViewDataErrorEventArgs)
Dim dgv As DataGridView = sender
Dim sGridName As String = dgv.Name.Replace("DataGridView", "")
Dim col As DataGridViewColumn = dgv.Columns(e.ColumnIndex)
Dim sColName As String = col.HeaderText
MsgBox(sGridName & vbNewLine & "Column " & sColName & vbNewLine & e.Exception.Message, MsgBoxStyle.Exclamation)
End Sub
You already have the OfType(OF T) method. You use it like this:
ForEach box As ComboBox In MyForm.Controls.OfType(Of ComboBox)()
Next
But this only checks the direct children of your control. If you have container controls like GroupBox, Panels, FlowControlLayoutPanel, etc, you'll miss the controls nested inside them. But we can build a new OfType() method to search these recursively:
Public Module ControlExtensions
<Extension()>
Public Iterator Function OfTypeRecursive(Of T As Control)(ByVal Controls As ControlCollection) As IEnumerable(Of T)
For Each parent As Control In Controls
If parent.HasChildren Then
For Each child As Control In OfTypeRecursive(Of T)(parent.Controls)
Yield child
Next child
End If
Next parent
For Each item As Control In Controls.OfType(Of T)()
Yield item
Next item
End Function
End Module
And you use it the same way:
ForEach box As ComboBox In MyForm.Controls.OfTypeRecursive(Of ComboBox)()
Next
You'll probably want to check containers for controls of the type you're looking for, here's a little function that should do the trick for you.
Private Function GetControls(Of T)(container As Control, searchChildren As Boolean) As T()
Dim Controls As New List(Of T)
For Each Child As Control In container.Controls
If TypeOf Child Is T Then
DirectCast(Controls, IList).Add(Child)
End If
If searchChildren AndAlso Child.HasChildren Then
Controls.AddRange(GetControls(Of T)(Child, True))
End If
Next
Return Controls.ToArray()
End Function
Here's the usage, if search children is True then all child containers will be searched for the control you're looking for. Also, for the top most container we'll just pass in Me assuming you're calling the code from within your Form, otherwise you could pass the Form instance or a specific Panel, GroupBox, etc.
Dim ComboBoxes As ComboBox() = GetControls(Of ComboBox)(Me, True)

Hyperlinks inside ListBox

Hello, can someone help me with my project that I'm working on? Anything is appreciated!
So, what do I need? Its simple, I need link and name bars to actually create a hyperlink into the LSB listbox. Can someone please help me??
PS: Could you please keep it simple for me, I just started with this language.
OK This isn't as simple as I would like it to be for you but here goes. I'll try to explain as I go along
OK all the bits code below goes in your Form1 class as one lump - without my explanations that is. What I've done here is create a new class called Hyperlink
It has a couple of properties called Name and Link and a constructor called New
Class HyperLink
Private friendlyName As String
Private link As String
Public Property Name As String
Get
Return friendlyName
End Get
Set(ByVal value As String)
friendlyName = value
End Set
End Property
Public Property URL As String
Get
Return link
End Get
Set(value As String)
link = URL
End Set
End Property
Public Sub New(nm As String, ln As String)
friendlyName = nm
link = ln
End Sub
End Class
Here I'm creating list called linkList - this will hold the list of hyperlinks - I'm doing this so that later on the listbox will be set to use this as it's source of list items
Dim linklist As New List(Of HyperLink)
This sub called addLinkToListbox actually does several things. The important bits are that it tells you program to temporarily stop responding when the index of the listbox changes(which happens even when you're changing the contents off the listbox)
Then it adds a new **hyperlink* to the list of hyperlinks taking data from the link textbox and the name textbox.
To actually refresh the data shown in the textbox, I have to change the listbox datasource to nothing and then back to to the linkList
The next two lines tell the listbox to display the Name property inn the list box and when the item is actually clicked, to return the URL property.
Finally, I tell the program to start responding again when the index of the listbox index changes.
Private Sub addLinkToListbox(linkName As String, linkURL As String)
RemoveHandler ListBox1.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged
linklist.Add(New HyperLink(linkName, linkURL))
ListBox1.DataSource = Nothing
ListBox1.DataSource = linklist
ListBox1.DisplayMember = "Name"
ListBox1.ValueMember = "URL"
AddHandler ListBox1.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged
End Sub
These last two bits of code are hopefully self-explanatory
Private Sub btnAddLink_Click(sender As Object, e As EventArgs) Handles btnAddLink.Click
addLinkToListbox(txtName.Text, txtLink.Text)
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
MessageBox.Show(ListBox1.SelectedValue.ToString)
End Sub

how to reference to data from listviews on different tabpages?

i have several tabpages in one tabcontrol, each contains nothing but a listview. I need to get the data/items from the listview in the selected tab. My problem is, I can't refer to the listview. I tried
exportpath = TabControl1.SelectedTab.Controls(0).
but at this point, the IDE doesn't know that there's a listview, so I can't choose the
.items(i).tostring
The variables to create the tabpage and the listviews are already overwritten at this moment. Do you think I should choose another way to create the tabs and the listview (maybe create a list(of listviews) to remember the stuff), or is there a way close to my example try?
Best regards, Jan
dim lst as listview = TabControl1.SelectedTab.Controls(0)
dim exportpath as string = lst.items(i).tostring
? Would that work.
edit: assuming the listview is the only control you have there.
I wouldn't load the control based off index in the collection.
I'm assuming since you are trying to access these dynamically you are also creating them dynamically?
Add a tag that can be referenced on the control or use the same naming convention. listview for the name ie MainListView# where # is just incremented or just assign all the tags on the listview to something consistent.
Then use helper function to loop through the controls collection for that tab and find the actual control.
Then loop through the collection of controls and find the one you want to target.
Dim lstView As ListView = ControlNameStartsWith(TabControl1.SelectedTab, "MainListView")
lstView.Items.Add("test")
FUNCTIONS:
Function ControlByTag(ByRef parent As Control, ByVal tag As String)
For Each Cntl As Control In parent.Controls
If Cntl.Tag.ToString.ToLower = tag.ToLower Then
Return Cntl
Exit For
End If
Next
Return Nothing
End Function
Function ControlNameStartsWith(ByRef parent As Control, ByVal likePhrase As String)
For Each Cntl As Control In parent.Controls
If Cntl.Name.ToLower.StartsWith(likePhrase.ToLower) Then
Return Cntl
Exit For
End If
Next
Return Nothing
End Function
Function ControlByName(ByRef parent As Control, ByVal name As String)
For Each Cntl As Control In parent.Controls
If Cntl.Name.ToLower = name.ToLower Then
Return Cntl
Exit For
End If
Next
Return Nothing
End Function

Issue when binding combobox and listbox to List (VB.NET)

I'm trying to bind a ComboBox and a ListBox to a List(Of String) in Vb.Net (VS2013), this is for a WinForms application, the thing is that after setting the DataSource property on both the ComboBox and the ListBox, selecting one item on either one of them affects the other control, for example, after the controls have been populated with the information if I select an item from the ListBox, then the same item gets selected in the ComboBox, and this goes the same way for the ComboBox, if I select an item from it, then that item gets also selected in the ListBox, so my question is... how can I bind the combobox and listbox to the same List(Of String) without affecting the behavior on the controls, the purpose is to keep all the controls within that form syncronized based on the contents of the List, I declared the List in a module like this:
Public listaAreas As New List(Of String)
then the controls are populated like this on form load:
cmbArea.DataSource = listaAreas
lstAreas.DataSource = listaAreas
And I run this method whenever I need to update the information:
Private Sub RefreshLists()
lstAreas.DataSource = Nothing
lstAreas.DataSource = listaAreas
cmbArea.DataSource = Nothing
cmbArea.DataSource = listaAreas
End Sub
Please let me know if I'm missing some information, this is my first post but I think it is clear enough so you get the idea of what I'm trying to accomplish here ... =)
Thanks in advance!
Try setting up separate BindingSources and try using a BindingList(Of String) instead of just a List, which won't report item changes:
Private listaAreas As New BindingList(Of String)
Private cbSource As New BindingSource(listaAreas, String.Empty)
Private lbSource As New BindingSource(listaAreas, String.Empty)
Public Sub New()
InitializeComponent()
cmbArea.DataSource = cbSource
lstAreas.DataSource = lbSource
End Sub
The same currency position is being used in your code, but by defining two separate binding sources, each one will have its own position property.