Storing datagridview name in a variable - vb.net

I am working on user rights. I want to load the grid items checked using following code.
Dim l As Integer = 0, vrGridName As New DataGridView, vrGridItemIndex As Integer
taSaveTemplates.Connection.ConnectionString += ";password=" & vrSAPWD
Me.taSaveTemplates.Fill(Me.DsSaveTemplates.tblTemplates, lstTemplateID.Text)
'Load Grids according to data saved
Do While DsSaveTemplates.tblTemplates.Rows.Count > l
vrGridName.Name = DsSaveTemplates.tblTemplates.Rows(l).Item("GridName")
vrGridItemIndex = DsSaveTemplates.tblTemplates.Rows(l).Item("GridItemIndex")
vrGridName.Item(0, vrGridItemIndex).Value = True
l = l + 1
Loop
vrGridName stores the name of grid selected from DB and vrGridItemIndex stores the item that needs to be checked.
The problem is, when I run the code, it says Index is our of range.
I have checked, the vrGridName does not store the name of grid but stores
System.windows.datagridview
Please advise.
Thanks

Your code is treating a control reference as if it were a name (string variable), so you get the Type name (System.windows.datagridview) rather than the name of the control. Since the template DGV happarently has the name, use it:
Dim myGridName As String ' name is a String, not DGV
Dim myGridItemIndex As Integer
Dim myDGV As DataGridView ' no NEW - not creating a new one
' just a reference var
'...
' this is now a For/Each loop
For Each row As DataGridViewRow in DsSaveTemplates.tblTemplates.Rows
myGridName = row.Cell("GridName")
myGridItemIndex = row.Cell("GridItemIndex")
' assuming this code is in a Form:
' get a reference to the control
myDGV = CType(Me.Controls(myGridName), DataGridView)
' talk to it like a DGV:
myDGV.Item(0, myGridItemIndex).Value = True
Next
Note: Option Strict would likely require some conversions for the name and index
If the DGV(s) reside in container controls like Panels or Tabs, you have to "find" the control because they will be in that control's collection, not the Form's. Instead of myDGV = CType(Me.Controls(myGridName), DataGridView):
' have the form search for the ctl by name
' the TRUE param tells it to search child controls like panels
Dim tmpArry = Me.Controls.Find(myGridName, True)
' test for a return
If tmpArry.Length > 0 Then
' tmpArry will be Type Control, so cast it
myDGV = CType(tmpArry(0), DataGridView)
End If
This is usually better from the start so you do not have to remember how the form is laid out when coding.

Related

Form control change to runtime object VB.net

I have a form what contains about 10-15 CheckedListBox objects on a Panel named PanelFilterBoxes.
Each CheckedListBox can contains lots of items (1 to max ~2000).
I have to reload regulary the items of this controls based upon a datatable's column unique values (plus some extras).
Now I have a code like this (this below is just a shorten extract):
For c = 0 To PanelFilterBoxes.Controls.Count - 1
'--get unique datas from datatable
Dim dtUnique() As String = ....
'--change items of the control
Dim contr As CheckedListBox = PanelFilterBoxes.Controls.Item(c)
contr.Items.Clear()
contr.Items.Add("*All", True)
contr.Items.Add("*Search: ", False)
For Each myValue As String In dtUnique
'--add items with checked true
contr.Items.Add(myValue, True)
Next
Next
It is not so long, but takes some times. I'd like to try what if I create a runtime object (I can put data in a runtime created object faster than a form object), and "replace" the control on the form with this?
So my "big challenge" how can I do this?
I tried this ways, but not works :/
For c = 0 To PanelFilterBoxes.Controls.Count - 1
'--get unique datas from datatable
Dim dtUnique() As String = ....
'--change items of the control
Dim contr As CheckedListBox = PanelFilterBoxes.Controls.Item(c)
'--runtime temporary object
Dim tempContr As new CheckedListBox
contr.Items.Clear()
tempContr .Items.Add("*All", True)
tempContr .Items.Add("*Search: ", False)
For Each myValue As String In dtUnique
'--add items with checked true
tempContr .Items.Add(myValue, True)
Next
'--1 - this doesn't works, but doesn't produce any error
contr=tempContr
'--2 - it works, but the items aren't checked
contr.Items.AddRange(tempContr.Items)
Next
Anybody has a solution for this?
Thank you

Visual Basic Iterative enabling of Textbox's

Si I'm working on an assignment where I have 10 RadioButtons indicating how many contesters I have, and depending on what I pick between 1 to 10, I need that many of my corresponding TextBoxes to be enabled so I could fill it with names!
Is there a way for me to make a For loop between 1 and the number I picked from the RadioButton and say something like
For i = 0 to Size
{
TextBox&i.Enabled = True
}
Since my TextBoxs are called TextBox1 to TextBox10
I know you can add strings together using &, but how can I do that for an object name?
As of right now I literally have the dumbest way of doing it, which is a click event inside each RadioButton that manually enables the correct number of TextBoxes...
Thank you in advance!
You can iterate over all controls like this:
For Each ctr In Me.Controls
Dim indx As String = ctr.Name
If TypeOf (ctr) Is Textbox Then
' Now compare the name with TextBox&i and do smth
End If
Next
It's not possible to just concatenate a string and use it as an object variable reference like that, but you can search the form's controls by their name property (which is a string) and do it that way. Here's an example:
Private Sub EnableTextBoxes(ByVal Size As Integer)
For i As Integer = 1 To Size
Dim matches() As Control = Me.Controls.Find("Textbox" & i.ToString, True)
If matches IsNot Nothing AndAlso matches.Length = 1 Then matches(0).Enabled = True
Next
End Sub

Refer to forms controls using variable

I've seen many threads on this, but I'm pretty much lost.
For the example below, I have 3 ComboBoxes (cbx_example0, cbx_example1, cbx_example2) located on three TabPages (index 0, 1, 2), respectively. I'd like set the value of the variable myVariable based on the ComboBox on the selected TabPage.
However, I can't seem to figure out how to refer to the ComboBox using a variable. It seems like it should be straightforward, but I guess not. No matter what I do, I get a NullReferenceException.
Function to get TabPage Index number (returns 0 for this example)
Function getTabIndex()
Dim currentTabIndex As Int32 = frm_Main.TabControl1.SelectedIndex
Return currentTabIndex
End Function
Attempt 1 to refer to ComboBox with variable
Dim myVariable As String
Dim i As Integer = getTabIndex
myVariable = frm_Main.Controls("cbx_example" & i).Text
Attempt 2 to refer to ComboBox with variable
Dim cbx_example0 As New ComboBox
Dim i As Integer = getTabIndex()
Dim name As String = "cbx_example" & i.ToString
cbx_example = frm_Main.Controls.Item(name)
myVariable = cbx_example.Text
Your problem is that you think that your cbo is located on the form, while in reality it is on the tabpage. Instead of
cbx_example = frm_Main.Controls.Item(name)
use (for example)
cbx_example = frm_Main.TabPage1.Controls.Item(name)
Keep in mind that your combo is probably not on tab itself but on the tabpage
On another note, I see no need for getTabIndex(). And another way to get any control within hierarchy of your form is to use
form.Controls.Find(key, searchAllChildren)

Can I create a property of a property?

So I recently grasped the concept of using classes in my Visual Basic programming, and I found it tremendously helpful. In my current project, I have several groups boxes of check boxes (each check box denotes a "Behavior") and in each group box, there is always one check box that has a textbox control instead of a label (to allow the user to specify an "Other" behavior). It is that user-generated label that is giving me trouble...
I created a class called "Behaviors" that basically does the following:
getChecked > This method gets each checked checkbox and adds it to
the BehaviorCollection for a given Form.
behaviorCollection > represents the collection of checked
checkboxes.
getOtherChecked > does the same as "getChecked" except with the
"Other Behavior" checkboxes.
otherBehaviorCollection > represents the collection of checked
"Other" checkboxes.
The issue is that for each checked "Other Behaviors" checkbox, I need to store the value of its corresponding textbox. I would like to set my getOtherChecked() method to do this, so that in the end, I would be able to something like this...
Dim myBoxes as new Behaviors
Dim cBox as Checkbox
Dim cBoxLabel as String
myBoxes.getOtherChecked(myUserForm) 'This would get each checked "Other Behaviors" checkbox object, and also somehow add another property to it called "LinkedTextboxLabel" that would be assigned the value of the corresponding textbox.
cBox = myBoxes.otherBehaviorCollection.item(0) 'Assign a checkbox from my "Other Behaviors" collection to a variable.
cBoxLabel = cBox.LinkedTextboxLabel 'Assign the user-inputted value of the linked textbox to a variable.
So basically how could/should I add a custom-property to a collection item or checkbox?
I thought about just adding the names of the controls to a temporary DataTable or SQL table, so that each row would have the name of a checkbox in one column and its corresponding textbox value in the next, but I am hoping there is a more commonly used and accepted method.
Thank you in advance!
You could add a property for the text associated with the "Other Behaviors" checkbox.
EDIT: You might be trying to generalize your data too far, because the "Other behaviors" is a special case and deserves separate consideration.
If you have a look at what the following code (in a new Windows Forms project) creates, it might give you ideas:
Public Class Form1
''' <summary>
''' A behaviour domain and its characteristics, with one user-defined entry.
''' </summary>
''' <remarks></remarks>
Public Class BehavioursSectionDescriptor
Property BehaviourTypeName As String
Property BehaviourNames As List(Of String)
Property CustomBehaviours As String
End Class
''' <summary>
''' Return a GroupBox containing CheckBoxes and one Checkbox with a TextBox adjacent to it.
''' </summary>
''' <param name="behaviourSet"></param>
''' <returns></returns>
''' <remarks></remarks>
Private Function GetBehaviourGroupPanel(behaviourSet As BehavioursSectionDescriptor) As GroupBox
Dim gb As New GroupBox
gb.Text = behaviourSet.BehaviourTypeName
Dim fixedBehaviourNames As List(Of String) = behaviourSet.BehaviourNames
Dim customBehavioursValue As String = behaviourSet.CustomBehaviours
Dim cbVertSeparation As Integer = 4
Dim gbPadding As Integer = 20
Dim cb As New CheckBox
Dim yLoc As Integer = gbPadding
For i = 0 To fixedBehaviourNames.Count - 1
cb = New CheckBox
cb.Location = New Point(gbPadding, yLoc)
cb.Text = fixedBehaviourNames(i)
' you can use the .Tag Object of a Control to store information
cb.Tag = behaviourSet.BehaviourTypeName & "-Cb-" & i.ToString()
gb.Controls.Add(cb)
yLoc += cb.Height + cbVertSeparation
Next
cb = New CheckBox
cb.Text = ""
cb.Location = New Point(gbPadding, yLoc)
cb.Tag = behaviourSet.BehaviourTypeName & "-Custom behaviours"
gb.Controls.Add(cb)
Dim tb As New TextBox
tb.Location = New Point(gbPadding + 18, yLoc)
tb.Width = 100
tb.Text = customBehavioursValue
gb.Controls.Add(tb)
' make sure the textbox appears in front of the checkbox's label area
tb.BringToFront()
gb.Size = New Size(160, yLoc + gbPadding * 2)
Return gb
End Function
Private Function GetTestData() As List(Of BehavioursSectionDescriptor)
Dim bsds = New List(Of BehavioursSectionDescriptor)
bsds.Add(New BehavioursSectionDescriptor With {.BehaviourTypeName = "In water", _
.BehaviourNames = New List(Of String) From {"Floats", "Spins"}, _
.CustomBehaviours = "Paddles"})
bsds.Add(New BehavioursSectionDescriptor With {.BehaviourTypeName = "Under light", _
.BehaviourNames = New List(Of String) From {"Shines", "Glows", "Reflects"}, _
.CustomBehaviours = "Iridesces"})
bsds.Add(New BehavioursSectionDescriptor With {.BehaviourTypeName = "Near food", _
.BehaviourNames = New List(Of String) From {"Sniffs", "Looks"}, _
.CustomBehaviours = ""})
Return bsds
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim bsds As List(Of BehavioursSectionDescriptor) = GetTestData()
Dim gbs As New List(Of GroupBox)
Dim xLoc As Integer = 20
Dim yLoc As Integer = 20
' make some GroupBoxes to present the data input fields
For i = 0 To bsds.Count - 1
Dim gb = GetBehaviourGroupPanel(bsds(i))
gb.Location = New Point(xLoc, yLoc)
gb.Dock = DockStyle.None
yLoc += gb.Height + 30
Me.Controls.Add(gb)
Next
' size the form to fit the content
Me.Size = New Size(240, yLoc + 40)
End Sub
End Class
I know it doesn't answer the question of adding a property to a property, but could you create a class for the Other checkbox and override it's capabilities? Then you could add checkboxes and OtherCheckBoxes to your generic collection? for instance, (by no means complete, but you should get the idea)
EDIT: Changed code to show Shadows
Public Class OptionalCheckbox : Inherits CheckBox
Private mOptionalText As String
Public Shadows Property Text() As String
Get
Return mOptionalText
End Get
Set(value As String)
mOptionalText = value
MyBase.Text = value
End Set
End Property
End Class
For each item, if you were to retrieve .Text, you would either get your textbox value or your checkbox label (if it was a normal checkbox)
And how to utilize in other parts of your code. Again, this is just more of an example. You would still need to work with the textbox that is assigned to the OtherCheckBox to get it to write the text to that, as well as read from that into the .Text property of the Class.
Dim newCheckBoxCollection As New Collection
Dim cBox As New CheckBox
cBox.Text = "Standard Value Here"
'other properties of the checkbox can be modified here
newCheckBoxCollection.Add(cBox)
Dim cOBox As New OptionalCheckbox
cOBox.Text = "Optional Text Here"
'other properties of the checkbox can be modified here
newCheckBoxCollection.Add(cOBox)
For Each cb As CheckBox In newCheckBoxCollection
Me.FlowLayoutPanel1.Controls.Add(cb)
Next
If you are trying to just save the data into something like a DataTable or SQL table the code would be a bit of an overkill. I suggest you use a stream reader/writer and try checking the values that way as the code would be a lot more simple.

im having syntax issues pulling data from 6 text boxes with identical names (other than a number at the end) in a for loop

I have 6 different text boxes with similar names:
txtBox1, txtBox2....txtBox6
I also have an array:
dim intValue (0 to 5) as Integer
Now I want to use a for loop to assign the value in each text box to a corresponding space in the array, concatenating the value of the loop counter with the string "txtBox" and retrieving the data using the .text method.
here is my code:
For count As Integer = 0 To 5
Dim strCount As String = count
Dim strTxtBox As String = "txtBox" & strCount
intValues(count) = Convert.toInt32(strTxtBox.Text)
Next
the issue is that the string name doesn't point to txtBox1, txtBox 2 etc.
Thanks in advance for the help.
If you want to access the control name, you have to use the property "Name".
You should try something like that:
For i As Integer = 0 To 5
For Each c As Control In Controls
If c.Name = "txtBox" & i Then
intValue(i) = Convert.ToInt32(c.Text)
End If
Next
Next i
The above code is just an example, but it should work for you.
Anyway you can write a better code checking the type of the control or maybe using a different loop logic, but the basic idea is that if you want to use in your code the name od the control you have to use the NAME property.
strTxtBox is a string, nothing more. You cannot refer to control names by String without special code. Rather than go down that path, I'd create an array and populate it with text boxes:
Dim txtBoxes As TextBox() = {txtBox1, txtBox2, txtBox3, txtBox4, txtBox5, txtBox6}
Dim intValues As Integer(0 To 5)
For count As Integer = 0 To 5
intValues(count) = Convert.ToInt32(txtBoxes(count).Text)
Next
The easiest way is to iterate through the contents of Controls to find the textboxes. You can either:
Use the .Tag property to label textboxes to identify them, or as the other answer points out, use the .Name property.
If you have a lot of other controls on the form, and they may not be in order, you could also check each control to see if it's a textbox. Assume they are in reverse order, as the Controls array goes from end to beginning.
Dim nums() As Integer = {10, 20, 30, 40, 50, 60}
Dim ctrl As Control
For Each ctrl In Me.Controls
If TypeOf (ctrl) Is TextBox Then
ctrl.Text = nums(Val(ctrl.Name(ctrl.Name.Length - 1)) - 1)
End If
Next
You could also instantiate an array of textboxes programmatically, but you'd have to set all of the placement, etc. by hand.
As I commented on the answer below, you may need to cast the ctrl to a System.Web.UI.Control to access the .Name, using DirectCast.