Textbox Enabled True False - vb.net

This is code am trying to solve
Dim i As Integer
i = ListBox1.SelectedIndex
If ListBox1.SelectedIndex = 0 Then
TextBox10.Enabled = False
TextBox25.Enabled = False
TextBox30.Enabled = False
TextBox40.Enabled = False
TextBox55.Enabled = False
TextBox65.Enabled = False
TextBox73.Enabled = False
TextBox84.Enabled = False
TextBox95.Enabled = False
TextBox100.Enabled = False
TextBox185.Enabled = False
Else
TextBox(??).Enabled = True
End If
How to other textboxes to enable True?
I mean
10,25,30,40... 185 are false and all other textboes Enabled true ?

You can find control of type textbox and assign default enabled= true and then write your code to set enabled=false.
For Each c As Control In Me.Controls
If c.GetType Is GetType(TextBox) Then
c.Enabled=true
End If
Next
OR
Dim allTextBox As New List(Of Control)
For Each c As TextBox In FindControl(allTextBox, Me,GetType(TextBox))
c.Enabled=true
Next
Public Shared Function FindControl(ByVal list As List(Of Control), ByVal parent As Control, ByVal ctrlType As System.Type) As List(Of Control)
If parent Is Nothing Then Return list
If parent.GetType Is ctrlType Then
list.Add(parent)
End If
For Each child As Control In parent.Controls
FindControl(list, child, ctrlType)
Next
Return list
End Function

I'd go with two lists:
var falseList = new List<ListBox> { TextBox10, TextBox25, TextBox30, ... };
var trueList = new List<ListBox> { TextBox1, TextBox2, TextBox3, ... };
Then in each condition block in the if enumerate each list and set false:
foreach(var box in falseList) box.Enabled = false;
foreach(var box in trueList) box.Enabled = true;
Another option is to setup a DataSource with two booleans and set DataBinding on each of the true ListBoxes to one of the properties and the other set to the false property then setting the found property on on the datasource they all update in one statement.
Note: Sorry it's c# syntax but it's really close to VB.NET. I just don't know the exact VB syntax

You can iterate over the forms Controls collection. Something like:
Dim i As Integer
i = ListBox1.SelectedIndex
for each c in me.controls
if TypeOf c is TextBox then
dim Textbox as textbox = DirectCast(c, TextBox)
'Determine state of your textbox here.
'Perhaps use the TAG property to assign a selectedindex to each textbox
Textbox.enabled = (Textbox.Tag.ToString() <> i.ToString())
end if
next

Related

How to group multiple objects

I have been searching for a method to group multiple objects to change a common value but have not been successful. I have been forced to do things like this:
Label10.Visible = True
Label11.Visible = True
Label12.Visible = True
Label13.Visible = True
Label14.Visible = True
RectangleShape8.Visible = True
RectangleShape9.Visible = True
RectangleShape10.Visible = True
RectangleShape11.Visible = True
Ect, Is there a method to group, or declare multiple objects to refer to all of them at the same time? I have attempted to declare but i was unsuccessful. Thanks for your help in advanced.
You can use the following:
Dim RectangleShapeGroup() As String = {"RectangleShape8", "RectangleShape9", "RectangleShape10", "RectangleShape11"}
Dim LabelGroup() As String = {"Label10", "Label11", "Label12", "Label13", "Label14"}
For Each ctrl As Control In Me.Controls
If Array.IndexOf(RectangleShapeGroup, ctrl.Name) > -1 Or Array.IndexOf(LabelGroup, ctrl.Name) > -1 Then
ctrl.Visible = True
End If
Next
In case you want to show all Label controls you can use the following:
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is Label Then
ctrl.Visible = True
End If
Next
... or all controls with names starting with Label or RectangleShape:
For Each ctrl As Control In Me.Controls
If ctrl.Name.StartsWith("Label") Or ctrl.Name.StartsWith("RectangleShape") Then
ctrl.Visible = True
End If
Next
Just add your controls to List(Of ControlType) then loop through to change any properties. This code is for Windows Forms. If this is a different type of application plead indicate that in your question.
Private lstLabels As New List(Of Label) From {Label10, Label11, Label12, Label13, Label14}
Private Sub MakeLabelsVisible()
For Each l In lstLabels
l.Visible = True
Next
End Sub

DropDownMenuItem check should uncheck other DropDownMenuItems

I'm creating a ToolStripMenuItem's DropDownItems at runtime.
Me.mnuExtrasSecondaryLCID.DropDownItems.Clear()
Dim iCount As Integer = -1
For Each nLang As clsLanguage In g_LCIDs
If nLang.IsLeader Then
iCount += 1
Dim n As New ToolStripMenuItem
n.Name = "mnuSecondaryLCID" & iCount.ToString()
n.Text = nLang.Title
n.Tag = nLang.LCID
n.Available = True
n.CheckOnClick = True
Me.mnuExtrasSecondaryLCID.DropDownItems.Add(n)
AddHandler n.Click, AddressOf Me.SecondaryLCIDClick
End If
Next
This works fine.
When I then check one of the DropDownItems at runtime, any other DropDownItems in the same "list" stay checked.
I would instead like to have only one checked (=the last clicked one).
Is there a property that would let me do this automatically, or do I need to code this by unchecking all other DropDropItems manually?
You need to code it manually. When clicking on a sub menu, uncheck all other siblings:
For index = 1 To 5
Dim subMenu = New ToolStripMenuItem(index.ToString())
subMenu.CheckOnClick= True
AddHandler subMenu.Click, Sub(obj, arg)
Dim item = DirectCast(obj, ToolStripMenuItem)
For Each sibling In item.Owner.Items.OfType(Of ToolStripMenuItem).Except({obj})
sibling.Checked = False
Next sibling
End Sub
Menu1ToolStripMenuItem.DropDownItems.Add(subMenu)
Next

What is the best way to loop this program?

This is one of the form, all the usercontrol value in this form will store in My.Settings
I have another form with a FlowLayoutPanel, everytime when the application start,
if Active checked then it will add a Button with discount value to the FlowLayoutPanel.
Should I add those usercontrol to a list and then loop through the list? Or what is the best way to solve this kind of problem?
UPDATED
How can I add multiple item to list in 1 code? I getting this error when system run to line 5
An exception of type 'System.NullReferenceException' occurred in XXX.exe but was not handled in user code
Additional information: Object reference not set to an instance of an object.
Public Sub RefreshDiscount(ByRef ref As scr_mainDiscount)
Dim li_disName As New List(Of TextBox)
Dim li_disValue As New List(Of TextBox)
Dim li_disType As New List(Of ComboBox)
Dim li_active As New List(Of CheckBox)
Dim tb_disName As TextBox() = {ref.tb_name1, ref.tb_name2, ref.tb_name3, ref.tb_name4, ref.tb_name5, ref.tb_name6, ref.tb_name7, ref.tb_name8, ref.tb_name9, ref.tb_name10}
Dim tb_disValue As TextBox() = {ref.tb_value1, ref.tb_value2, ref.tb_value3, ref.tb_value4, ref.tb_value5, ref.tb_value6, ref.tb_value7, ref.tb_value8, ref.tb_value9, ref.tb_value10}
Dim cb_disType As ComboBox() = {ref.cb_type1, ref.cb_type2, ref.cb_type3, ref.cb_type4, ref.cb_type5, ref.cb_type6, ref.cb_type7, ref.cb_type8, ref.cb_type9, ref.cb_type10}
Dim chkb_active As CheckBox() = {ref.CheckBox1, ref.CheckBox2, ref.CheckBox3, ref.CheckBox4, ref.CheckBox5, ref.CheckBox6, ref.CheckBox7, ref.CheckBox8, ref.CheckBox9, ref.CheckBox10}
li_disName.AddRange(tb_disName)
li_disValue.AddRange(tb_disValue)
li_disType.AddRange(cb_disType)
li_active.AddRange(chkb_active)
For index As Integer = 0 To li_active.Count - 1
If li_active(index).Checked = False Then
li_disName.RemoveAt(index)
li_disValue.RemoveAt(index)
li_disType.RemoveAt(index)
li_active.RemoveAt(index)
Else
Dim btn As New ctrl_DiscountButton
With btn
.Text = li_disName(index).Text
.Price = li_disValue(index).Text
.Type = li_disType(index).Text
End With
scr_sales.flp_discount.Controls.Add(btn)
End If
Next
li_disName.Clear()
li_disValue.Clear()
li_disType.Clear()
li_active.Clear()
End Sub
Here's a simple example showing how to find CheckBox1 thru CheckBox10, "by name", using the "searchAllChildren" option of Controls.Find():
For i As Integer = 1 To 10
Dim ctlName As String = "CheckBox" & i
Dim matches() As Control = Me.Controls.Find(ctlName, True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is CheckBox Then
Dim cb As CheckBox = DirectCast(matches(0), CheckBox)
' do something with "cb"
If cb.Checked Then
' ... code ...
' possibly use code just like this to find the matching discount value control?
End If
End If
Next

Loop through Multiple Listbox Controls And Check Selected Index Of Each

I'm using the below code to try and loop through all ListBox controls in a panel on a WinForm. I want to check if any of them have a SelectedIndex above 0. If they do, I want to set a boolean value in an array to True, else set it to False:
Dim i As Integer = -1
For Each cntrl As Control In Form1.Panel3.Controls
If TypeOf cntrl Is ListBox Then
i = i + 1
If cntrl.selectedindex <> 0 Then
ReportArray(i) = True
Else
ReportArray(i) = False
End If
End If
Next
The issue I am having is that cntrl.selectedindex is not valid as .selectedindex is being picked up that it is not a member of Windows.Forms.Control
How do I get this to see it as a ListBox?
Try converting the cntrl to listbox first like this
Dim i As Integer = -1
For Each cntrl As Control In Form1.Panel3.Controls
If TypeOf cntrl Is ListBox Then
Dim TmpCntrl As ListBox = TryCast(cntrl, ListBox)
i = i + 1
If TmpCntrl.selectedindex <> 0 Then
ReportArray(i) = True
Else
ReportArray(i) = False
End If
End If
Next

Setting ReadOnly attribute to all Textboxes in Array of Controls

I have the following code looping through a variety of arrayed controls in a form:
For r As Long = LBound(ctrlArray) To UBound(ctrlArray)
If TypeOf ctrlArray(r) Is TextBox Then
ctrlArray(r).Text = ""
If ctrlArray(r).ReadOnly = False Then
ctrlArray(r).ReadOnly = True
End If
Else
If ctrlArray(r).Enabled = True Then
ctrlArray(r).Enabled = False
End If
End If
Next
I receive the error "'ReadOnly' is not a member of System.Windows.Forms.Control" when trying to set textboxes as read only.
Solved this right before I hit the submit button. Thought I would share anyway:
Dim tbx As TextBox
For r As Long = LBound(ctrlArray) To UBound(ctrlArray)
If TypeOf ctrlArray(r) Is TextBox Then
ctrlArray(r).Text = ""
tbx = ctrlArray(r)
If tbx.ReadOnly = False Then
tbx.ReadOnly = True
End If
Else
If ctrlArray(r).Enabled = True Then
ctrlArray(r).Enabled = False
End If
End If
Next