Setting ReadOnly attribute to all Textboxes in Array of Controls - vb.net

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

Related

How can I remove the cell selection from a checkbox column ? VB.NET

Greetings to the community.
I am trying to modify a checkbox column of a DataGridView in VB.NET. I hope you can help me.
I am looking to go from this:
To this:
That is, even if I click or double click on the cell, the checkbox is not selected.
Part of the code where the columns are added to DataGridView (DvgNeumaticos):
If BtnVistaPorFacturar.Checked Then
For i As Integer = 0 To DgvNeumaticos.Columns.Count - 1
DgvNeumaticos.Columns(i).Visible = False
Next
DgvNeumaticos.Columns("ColChk").Visible = True
DgvNeumaticos.Columns("CodOS").Visible = True
DgvNeumaticos.Columns("Tipo").Visible = True
DgvNeumaticos.Columns("NroOrden").Visible = True
DgvNeumaticos.Columns("NroOrden").DisplayIndex = 0
DgvNeumaticos.Columns("FechaOS").Visible = True
DgvNeumaticos.Columns("F.Cierre").Visible = True
DgvNeumaticos.Columns("Observacion").Visible = True
DgvNeumaticos.Columns("CodNeumatico").Visible = True
DgvNeumaticos.Columns("Externo").Visible = True
DgvNeumaticos.Columns("CodTercero").Visible = True
DgvNeumaticos.Columns("CodProducto").Visible = True
DgvNeumaticos.Columns("Producto").Visible = True
DgvNeumaticos.Columns("Trabajo").Visible = True
DgvNeumaticos.Columns("NroDocRecepcion").Visible = True
DgvNeumaticos.Columns("FechaRecepcion").Visible = True
New Edit:
If BtnVistaPorFacturar.Checked Then
Dim chkCol As New DataGridViewCheckBoxColumn
chkCol.Name = "ColChk"
chkCol.HeaderText = "Chk"
DgvNeumaticos.Columns.Add(chkCol)
dt = WFOrdServicioNeumaticos.Instancia.fdu_NEUM_ORDSERV_VerOrdenesPorFacturar(.Cells("IDAgente").Value)
End If
Thanks for the replies.

What is lacking in my VBA code? Looking to have multiple checkboxes that when one is selected, it hides all other rows

Brand new to coding junk in VBA for Microsoft Word. I have a table with 12 rows and I want to place a standard content control checkbox next to each row, and when any given checkbox is checked, the other rows disappear.
Currently I've had good luck at this with purely text, but trying to bookmark to hide an entire row of a table only seems to work for the very first checkbox. (Sorry if my code is more complicated than it needs to be. I also skipped pasting all of the code since the other 10 lines are the same, so the final 12 End Ifs are necessary):
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim cc As ContentControl
For Each cc In ActiveDocument.ContentControls
If cc.Title = "impact" Then
If cc.Checked = True Then
ActiveDocument.Bookmarks("bfganalytical").Range.Font.Hidden = True
ActiveDocument.Bookmarks("EA").Range.Font.Hidden = True
ActiveDocument.Bookmarks("fascia1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("fascia2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("grille1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("grille2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("shutter1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("shutter2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("liner").Range.Font.Hidden = True
ActiveDocument.Bookmarks("license").Range.Font.Hidden = True
ActiveDocument.Bookmarks("lamp1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("lamp2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("blank").Range.Font.Hidden = True
ActiveDocument.Bookmarks("impact").Range.Font.Hidden = False
ActiveDocument.Bookmarks("beamanalytical").Range.Font.Hidden = False
Else: ActiveDocument.Bookmarks("impact").Range.Font.Hidden = False
ActiveDocument.Bookmarks("bfganalytical").Range.Font.Hidden = False
ActiveDocument.Bookmarks("EA").Range.Font.Hidden = False
ActiveDocument.Bookmarks("fascia1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("fascia2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("grille1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("grille2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("shutter1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("shutter2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("liner").Range.Font.Hidden = False
ActiveDocument.Bookmarks("license").Range.Font.Hidden = False
ActiveDocument.Bookmarks("lamp1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("lamp2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("beamanalytical").Range.Font.Hidden = False
ActiveDocument.Bookmarks("blank").Range.Font.Hidden = False
End If
Exit Sub
Else: If cc.Title = "license" Then
If cc.Checked = True Then
ActiveDocument.Bookmarks("beamanalytical").Range.Font.Hidden = True
ActiveDocument.Bookmarks("impact").Range.Font.Hidden = True
ActiveDocument.Bookmarks("fascia1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("fascia2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("grille1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("grille2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("shutter1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("shutter2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("liner").Range.Font.Hidden = True
ActiveDocument.Bookmarks("license").Range.Font.Hidden = False
ActiveDocument.Bookmarks("lamp1").Range.Font.Hidden = True
ActiveDocument.Bookmarks("lamp2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("blank2").Range.Font.Hidden = True
ActiveDocument.Bookmarks("blank3").Range.Font.Hidden = True
ActiveDocument.Bookmarks("EA").Range.Font.Hidden = True
ActiveDocument.Bookmarks("bfganalytical").Range.Font.Hidden = False
Else: ActiveDocument.Bookmarks("impact").Range.Font.Hidden = False
ActiveDocument.Bookmarks("bfganalytical").Range.Font.Hidden = False
ActiveDocument.Bookmarks("EA").Range.Font.Hidden = False
ActiveDocument.Bookmarks("fascia1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("fascia2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("grille1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("grille2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("shutter1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("shutter2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("liner").Range.Font.Hidden = False
ActiveDocument.Bookmarks("license").Range.Font.Hidden = False
ActiveDocument.Bookmarks("lamp1").Range.Font.Hidden = False
ActiveDocument.Bookmarks("lamp2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("beamanalytical").Range.Font.Hidden = False
ActiveDocument.Bookmarks("blank2").Range.Font.Hidden = False
ActiveDocument.Bookmarks("blank3").Range.Font.Hidden = False
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
Next
End Sub
Assuming that the content control Title is the same as the bookmark name you can try this simplified version of your code.
Private Sub Document_ContentControlOnExit(ByVal ContentControl As ContentControl, Cancel As Boolean)
Dim cc As ContentControl
For Each cc In ActiveDocument.ContentControls
If ActiveDocument.Bookmarks.Exists(cc.Title) Then
ActiveDocument.Bookmarks(cc.Title).Range.Font.Hidden = cc.Checked
End If
Next cc
End Sub
EDIT:
The issue you have with your original code is that it will only allow one row to be hidden.
To make your solution work you need to query the checked status of the corresponding content control for each bookmark. Your best option to achieve that is to ensure that the bookmark name matches either cc.Title or cc.Tag, otherwise you are back to complex and unwieldy code.
You actually don't need anything more complicated than:
Private Sub Document_ContentControlOnExit(ByVal CCtrl As ContentControl, Cancel As Boolean)
With CCtrl
If .Range.Information(wdWithInTable) = True Then
If .Checked = True Then
.Range.Tables(1).Range.Font.Hidden = True
.Range.Rows(1).Range.Font.Hidden = False
Else
.Range.Tables(1).Range.Font.Hidden = False
End If
End If
End With
End Sub
Looping through all the content controls is quite unnecessary. You don't even need any titles or bookmarks.

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

Filter ListView data with CheckBox inputs

I'm trying to provide a way to filter data within a ListView using CheckBox controls. Checking a box will add or remove the selected attribute from the ListView.
The current code:
For i = 0 To RecCount
Filter = True
'RecordDat array populated here
'filter the record by checkbox selection
If ckbComplete.Checked = True And Len(RecordDat(7)) = 0 Then
Filter = False
ElseIf ckbOpen.Checked = True And Len(RecordDat(7)) > 0 Then
Filter = false
ElseIf ckbApps.Checked = True And strings.Left(RecordDat(0).ToString, 4) <> "APPS" Then
Filter = False
ElseIf ckbContract.Checked = True And Strings.Left(RecordDat(0), 3) <> "SOP" Then
Filter = false
End If
If Filter = False Then GoTo SkipItem
' More code adding the item to the list view and formatting
SkipItem:
Next
At the moment I can filter only once per array index, i.e. I can filter ckbComplete and ckbApps but not ckbcomplete, ckbApps and ckbContract.
I managed to figure out a solution by trial and error and wish to share what I found.
In order to filter the data, each variable (in this case, the column value) required its own filter statements, each which must be encased in an If ELSE statement.
The resulting code was as follows:
Dim Filter As Boolean = True
For i = 0 To RecCount
Filter = False
'RecordDat initialisation
Filter = FilterPass(RecordDat)
If Filter = False Then Continue For
Dim itm As ListViewItem
itm = New ListViewItem(RecordDat)
Healthchart.ListView1.Items.Add(itm)
'Continue formatting and input of data into listview
Next
The FilterPass functions then follow:
Private Function FilterPass(ByVal RecordDat() As String)
Dim filter As Boolean = False
If Healthchart.ckbComplete.Checked = True Then
If Len(RecordDat(7)) > 0 Then
filter = FilterPass2(RecordDat)
If filter = True Then
filter = FilterPass3(RecordDat)
End If
End If
End If
If Healthchart.ckbOpen.Checked = True Then
If Len(RecordDat(7)) = 0 Then
filter = FilterPass2(RecordDat)
If filter = True Then
filter = FilterPass3(RecordDat)
End If
End If
End If
Return filter
End Function
Private Function FilterPass2(ByVal RecordDat() As String)
Dim filter As Boolean = False
If Healthchart.ckbApps.Checked = True Then
If Strings.Left(RecordDat(0).ToString, 4) = "APPS" Then
filter = True
End If
End If
If Healthchart.ckbContract.Checked = True Then
If Strings.Left(RecordDat(0), 3) = "SOP" Then
filter = True
End If
End If
If Healthchart.ckbTech.Checked = True Then
If Strings.Left(RecordDat(0), 3) = "ATS" Then
filter = True
End If
End If
If Healthchart.ckbDes.Checked = True Then
If Strings.Left(RecordDat(0), 3) = "DES" Then
filter = True
End If
End If
Return filter
End Function
Private Function FilterPass3(ByVal RecordDat() As String)
Dim filter As Boolean = True
Dim SubDate As Date = Date.ParseExact(RecordDat(1).ToString, "dd/MM/yy", System.Globalization.DateTimeFormatInfo.InvariantInfo)
If Healthchart.ckbLast30.Checked = True Then
If DateAdd(DateInterval.Day, -30, Now) > SubDate Then
filter = False
End If
End If
If Healthchart.ckb7Days.Checked = True Then
If DateAdd(DateInterval.Day, -7, Now) > SubDate Then
filter = False
End If
End If
Return filter
End Function
One can then add as many 'filters' as required by adding an additional If Filter = true then into the FilterPass function.

Textbox Enabled True False

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