Set properties on labels using for statement in VB.NET - vb.net

I am trying something that I don't know if it is possible to do.
When I press a button, the property .visible of many labels must change, and I am doing that using this:
Private Sub AlakranPositionsButton_Click(sender As Object, e As EventArgs) Handles AlakranPositionsButton.Click
If AlakranLabel.Visible = False Then
Label4.Visible = True
Label5.Visible = True
AlakranLabel.Visible = True
Alakran1Label.Visible = True
Alakran2Label.Visible = True
Alakran3Label.Visible = True
Alakran4Label.Visible = True
Alakran5Label.Visible = True
Alakran6Label.Visible = True
Alakran7Label.Visible = True
Alakran8Label.Visible = True
Alakran9Label.Visible = True
Alakran10Label.Visible = True
Alakran1YTextBox.Visible = True
Alakran2YTextBox.Visible = True
Alakran3YTextBox.Visible = True
Alakran4YTextBox.Visible = True
Alakran5YTextBox.Visible = True
Alakran6YTextBox.Visible = True
Alakran7YTextBox.Visible = True
Alakran8YTextBox.Visible = True
Alakran9YTextBox.Visible = True
Alakran10YTextBox.Visible = True
Alakran1XTextBox.Visible = True
Alakran2XTextBox.Visible = True
Alakran3XTextBox.Visible = True
Alakran4XTextBox.Visible = True
Alakran5XTextBox.Visible = True
Alakran6XTextBox.Visible = True
Alakran7XTextBox.Visible = True
Alakran8XTextBox.Visible = True
Alakran9XTextBox.Visible = True
Alakran10XTextBox.Visible = True
Else
Label4.Visible = False
Label5.Visible = False
AlakranLabel.Visible = False
Alakran1Label.Visible = False
Alakran2Label.Visible = False
Alakran3Label.Visible = False
Alakran4Label.Visible = False
Alakran5Label.Visible = False
Alakran6Label.Visible = False
Alakran7Label.Visible = False
Alakran8Label.Visible = False
Alakran9Label.Visible = False
Alakran10Label.Visible = False
Alakran1YTextBox.Visible = False
Alakran2YTextBox.Visible = False
Alakran3YTextBox.Visible = False
Alakran4YTextBox.Visible = False
Alakran5YTextBox.Visible = False
Alakran6YTextBox.Visible = False
Alakran7YTextBox.Visible = False
Alakran8YTextBox.Visible = False
Alakran9YTextBox.Visible = False
Alakran10YTextBox.Visible = False
Alakran1XTextBox.Visible = False
Alakran2XTextBox.Visible = False
Alakran3XTextBox.Visible = False
Alakran4XTextBox.Visible = False
Alakran5XTextBox.Visible = False
Alakran6XTextBox.Visible = False
Alakran7XTextBox.Visible = False
Alakran8XTextBox.Visible = False
Alakran9XTextBox.Visible = False
Alakran10XTextBox.Visible = False
End If
I think that there must be a way to do something like this:
If AlakranLabel.Visible = False Then
For i As Integer = 0 To 20
Label(i).visible = False
Next
Else
For i As Integer = 0 To 20
Label(i).visible = True
Next
End If
Someone knows how to do this? I am not able to see how to do it..
Thanks!
Solution
Declaration of the lists:
Dim listObjectiveLabels As List(Of Label) = New List(Of Label)
Dim listObjectiveTextBox As List(Of TextBox) = New List(Of TextBox)
Add each label and textbox in a list:
listAlakranLabels.Add(AlakranLabel)
listAlakranLabels.Add(Alakran1Label)
listAlakranLabels.Add(Alakran2Label)
listAlakranLabels.Add(Alakran3Label)
listAlakranLabels.Add(Alakran4Label)
listAlakranLabels.Add(Alakran5Label)
listAlakranLabels.Add(Alakran6Label)
listAlakranLabels.Add(Alakran7Label)
listAlakranLabels.Add(Alakran8Label)
listAlakranLabels.Add(Alakran9Label)
listAlakranLabels.Add(Alakran10Label)
listAlakranTextBox.Add(Alakran1YTextBox)
listAlakranTextBox.Add(Alakran2YTextBox)
listAlakranTextBox.Add(Alakran3YTextBox)
listAlakranTextBox.Add(Alakran4YTextBox)
listAlakranTextBox.Add(Alakran5YTextBox)
listAlakranTextBox.Add(Alakran6YTextBox)
listAlakranTextBox.Add(Alakran7YTextBox)
listAlakranTextBox.Add(Alakran8YTextBox)
listAlakranTextBox.Add(Alakran9YTextBox)
listAlakranTextBox.Add(Alakran10YTextBox)
listAlakranTextBox.Add(Alakran1XTextBox)
listAlakranTextBox.Add(Alakran2XTextBox)
listAlakranTextBox.Add(Alakran3XTextBox)
listAlakranTextBox.Add(Alakran4XTextBox)
listAlakranTextBox.Add(Alakran5XTextBox)
listAlakranTextBox.Add(Alakran6XTextBox)
listAlakranTextBox.Add(Alakran7XTextBox)
listAlakranTextBox.Add(Alakran8XTextBox)
listAlakranTextBox.Add(Alakran9XTextBox)
listAlakranTextBox.Add(Alakran10XTextBox)
Use for each sentence:
For Each l As Label In listAlakranLabels
l.Visible = True
Next
For Each l As TextBox In listAlakranTextBox
l.Visible = True
Next

You've got 2 options:
1.- Adding your labels to a List<Label> and the use a For Each to set their Visible property:
Dim allLabels As List(Of Label) = New List(Of Label);
allLabels.Add(Label4)
allLabels.Add(Label5)
allLabels.Add(AlakranLabel)
...
For Each l As Label In allLabels
l.Visible = True
Next
2.- If you want to show/hide all the labels inside a container, you can For Each the container Controls Collection, like this:
For Each l As Label In Me.Controls.OfType(Of Label)
l.Visible = True
Next
EDIT
You've got another option, using the Find method of the Controls collection:
For i As Integer = 1 To 10
Me.Controls.Find("Alakran" & i & "Label", False).FirstOrDefault().Visible = True
Next
This would set all labels from Alakran1Label to Alakran10Label visibility to True

You can iterate on all instances of the Label or Textbox in your form (or container) by :
First declaring a control variable to handle each object in your Container collection
Determine if the current control is a label
setting their visibility to false.
Dim lbl_holder as Control
For each lbl_holder in Me.Controls
If typeof lbl_holder is Label or Typeof lbl_holder is Textbox then
lbl_holder.Visible = False
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.

Microsoft Excel VBA - Run-Time Error 438

I appear to be having an error which I am struggling to figure out the reason. I have tried the help sections and also tried researching it online but have not come up with any results. I am hoping someone may be able to assist me in the matter.
Issue
I have created multiple forms for different sheets on my spreadsheet. I have made forms which can be used to hide/show select column(s) by user discretion. I have two forms which work perfectly fine, but on the third.
I get
Run-Time Error 438 "Object doesn't support this property or method"
What does this mean? The code is the exact same as the other forms. The only difference in them is that the names of the sheets are different.
I will paste the code below for the sheets. Hopefully you can distinguish which is which. I will try and do my best to explain.
Code below
Main sheet - contains button to form open form
Private Sub openUserForm_Click()
chkFormCooms.Show
End Sub
Userform
Option Explicit
Sub hideCol(C As Integer)
If Controls("CheckBox" & C) = True Then
Columns(C).Hidden = True
Else
Columns(C).Hidden = False
End If
ActiveWindow.ScrollColumn = 1
End Sub
Private Sub chkP1_Click()
If Me.chkP1.Value = True Then
Sheets("Cooms").Columns("T:W").Hidden = True
Sheets("chkCooms").chk1.Value = True
ElseIf Me.chkP1.Value = False Then
Sheets("Cooms").Columns("T:W").Hidden = False
Sheets("chkCooms").chk1.Value = False
End If
End Sub
Private Sub chkP2_Click()
If Me.chkP2.Value = True Then
Sheets("Cooms").Columns("X:AA").Hidden = True
Sheets("chkCooms").chk2.Value = True
ElseIf Me.chkP2.Value = False Then
Sheets("Cooms").Columns("X:AA").Hidden = False
Sheets("chkCooms").chk2.Value = False
End If
End Sub
Private Sub chkP3_Click()
If Me.chkP3.Value = True Then
Sheets("Cooms").Columns("AB:AE").Hidden = True
Sheets("chkCooms").chk3.Value = True
ElseIf Me.chkP3.Value = False Then
Sheets("Cooms").Columns("AB:AE").Hidden = False
Sheets("chkCooms").chk3.Value = False
End If
End Sub
Private Sub chkP4_Click()
If Me.chkP4.Value = True Then
Sheets("Cooms").Columns("AF:AI").Hidden = True
Sheets("chkCooms").chk4.Value = True
ElseIf Me.chkP4.Value = False Then
Sheets("Cooms").Columns("AF:AI").Hidden = False
Sheets("chkCooms").chk4.Value = False
End If
End Sub
Private Sub chkP5_Click()
If Me.chkP5.Value = True Then
Sheets("Cooms").Columns("AJ:AM").Hidden = True
Sheets("chkCooms").chk5.Value = True
ElseIf Me.chkP5.Value = False Then
Sheets("Cooms").Columns("AJ:AM").Hidden = False
Sheets("chkCooms").chk5.Value = False
End If
End Sub
Private Sub chkP6_Click()
If Me.chkP6.Value = True Then
Sheets("Cooms").Columns("AN:AQ").Hidden = True
Sheets("chkCooms").chk6.Value = True
ElseIf Me.chkP6.Value = False Then
Sheets("Cooms").Columns("AN:AQ").Hidden = False
Sheets("chkCooms").chk6.Value = False
End If
End Sub
Private Sub chkP7_Click()
If Me.chkP7.Value = True Then
Sheets("Cooms").Columns("AR:AU").Hidden = True
Sheets("chkCooms").chk7.Value = True
ElseIf Me.chkP7.Value = False Then
Sheets("Cooms").Columns("AR:AU").Hidden = False
Sheets("chkCooms").chk7.Value = False
End If
End Sub
Private Sub chkP8_Click()
If Me.chkP8.Value = True Then
Sheets("Coomst").Columns("AV:AY").Hidden = True
Sheets("chkCooms").chk8.Value = True
ElseIf Me.chkP8.Value = False Then
Sheets("Cooms").Columns("AV:AY").Hidden = False
Sheets("chkCooms").chk8.Value = False
End If
End Sub
Private Sub chkP9_Click()
If Me.chkP9.Value = True Then
Sheets("Cooms").Columns("AZ:BC").Hidden = True
Sheets("chkCooms").chk9.Value = True
ElseIf Me.chkP9.Value = False Then
Sheets("Cooms").Columns("AZ:BC").Hidden = False
Sheets("chkCooms").chk9.Value = False
End If
End Sub
Private Sub chkP10_Click()
If Me.chkP10.Value = True Then
Sheets("Cooms").Columns("BD:BG").Hidden = True
Sheets("chkCooms").chk10.Value = True
ElseIf Me.chkP10.Value = False Then
Sheets("Cooms").Columns("BD:BG").Hidden = False
Sheets("chkCooms").chk10.Value = False
End If
End Sub
Private Sub chkP11_Click()
If Me.chkP11.Value = True Then
Sheets("Cooms").Columns("BH:BK").Hidden = True
Sheets("chkCooms").chk11.Value = True
ElseIf Me.chkP11.Value = False Then
Sheets("Cooms").Columns("BH:BK").Hidden = False
Sheets("chkCooms").chk11.Value = False
End If
End Sub
Private Sub chkP12_Click()
If Me.chkP12.Value = True Then
Sheets("Cooms").Columns("BL:BO").Hidden = True
Sheets("chkCooms").chk12.Value = True
ElseIf Me.chkP12.Value = False Then
Sheets("Cooms").Columns("BL:BO").Hidden = False
Sheets("chkCooms").chk12.Value = False
End If
End Sub
Private Sub chkP13_Click()
If Me.chkP13.Value = True Then
Sheets("Cooms").Columns("BP:BS").Hidden = True
Sheets("chkCooms").chk13.Value = True
ElseIf Me.chkP13.Value = False Then
Sheets("Cooms").Columns("BP:BS").Hidden = False
Sheets("chkCooms").chk13.Value = False
End If
End Sub
Private Sub UserForm_Initialize()
Me.chkP1.Value = Sheets("chkCooms").chk1.Value
Me.chkP2.Value = Sheets("chkCooms").chk2.Value
Me.chkP3.Value = Sheets("chkCooms").chk3.Value
Me.chkP4.Value = Sheets("chkCooms").chk4.Value
Me.chkP5.Value = Sheets("chkCooms").chk5.Value
Me.chkP6.Value = Sheets("chkCooms").chk6.Value
Me.chkP7.Value = Sheets("chkCooms").chk7.Value
Me.chkP8.Value = Sheets("chkCooms").chk8.Value
Me.chkP9.Value = Sheets("chkCooms").chk9.Value
Me.chkP10.Value = Sheets("chkCooms").chk10.Value
Me.chkP11.Value = Sheets("chkCooms").chk11.Value
Me.chkP12.Value = Sheets("chkCooms").chk12.Value
Me.chkP13.Value = Sheets("chkCooms").chk13.Value
End Sub
I hope this all makes sense and that someone is able to assist me in this matter. If you need further explanation then please do not hesitate to ask. Thank you very much for your assistance.
Check the name of your userform its probably spelt incorrectly
For information about the error check this amazing description

Is there an easier/more efficient way to make Visible = False for multiple buttons in VB.NET?

not sure if there is an easier way to do this but I have a LOT of buttons on a form. Different ones are visible for different functions.
Is there a way to have something like this have an easier way to change their visibility without having each button coded to go False/True?
For simplicity sake, I created a quick app to handle the visibility but I want to hide the others when one set of buttons is visible. So if I select Row 1, it will make Visibility FALSE on Row 2 and 3.
Am I stuck with this or is there an easier way/more efficient way? THANKS IN ADVANCE!
Public Class Form1
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedItem.ToString = "Button Row 1" Then
Button1.Visible = True
Button2.Visible = True
Button3.Visible = True
Button4.Visible = True
Button5.Visible = True
Button6.Visible = True
Button7.Visible = True
Button8.Visible = True
Button9.Visible = True
Button10.Visible = True
Button11.Visible = False
Button12.Visible = False
Button13.Visible = False
Button14.Visible = False
Button15.Visible = False
Button16.Visible = False
Button17.Visible = False
Button18.Visible = False
Button19.Visible = False
Button20.Visible = False
Button21.Visible = False
Button22.Visible = False
Button23.Visible = False
Button24.Visible = False
Button25.Visible = False
Button26.Visible = False
Button27.Visible = False
Button28.Visible = False
Button29.Visible = False
Button30.Visible = False
ElseIf ComboBox1.SelectedItem.ToString = "Button Row 2" Then
Button1.Visible = False
Button2.Visible = False
Button3.Visible = False
Button4.Visible = False
Button5.Visible = False
Button6.Visible = False
Button7.Visible = False
Button8.Visible = False
Button9.Visible = False
Button10.Visible = False
Button11.Visible = True
Button12.Visible = True
Button13.Visible = True
Button14.Visible = True
Button15.Visible = True
Button16.Visible = True
Button17.Visible = True
Button18.Visible = True
Button19.Visible = True
Button20.Visible = True
Button21.Visible = False
Button22.Visible = False
Button23.Visible = False
Button24.Visible = False
Button25.Visible = False
Button26.Visible = False
Button27.Visible = False
Button28.Visible = False
Button29.Visible = False
Button30.Visible = False
ElseIf ComboBox1.SelectedItem.ToString = "Button Row 3" Then
Button1.Visible = False
Button2.Visible = False
Button3.Visible = False
Button4.Visible = False
Button5.Visible = False
Button6.Visible = False
Button7.Visible = False
Button8.Visible = False
Button9.Visible = False
Button10.Visible = False
Button11.Visible = False
Button12.Visible = False
Button13.Visible = False
Button14.Visible = False
Button15.Visible = False
Button16.Visible = False
Button17.Visible = False
Button18.Visible = False
Button19.Visible = False
Button20.Visible = False
Button21.Visible = True
Button22.Visible = True
Button23.Visible = True
Button24.Visible = True
Button25.Visible = True
Button26.Visible = True
Button27.Visible = True
Button28.Visible = True
Button29.Visible = True
Button30.Visible = True
End If
End Sub
End Class
Set the tag property of the buttons. You can do it in the properties window. I just show it in code to illustrate the point
Button1.Tag = "Button Row 1"
Then you can do
Dim selectedRow = ComboBox1.SelectedItem.ToString()
For Each c As Control In Controls
If (TypeOf c Is Button) Then
c.Visible = selectedRow.Equals(c.Tag)
End If
Next
Note that this automatically shows the buttons of the selected row and hides the others.
If this affects too many buttons, you can also check if the Tag is not Nothing instead of testing if it is a button.
You could use a for loop and keep track of which textbox is in which row, could put the row in the name, tag, use a custom property, etc.
You could put each row in a groupbox and just change the visibility of the group.
You could make a list of Buttons for each row, add the buttons, and loop through those lists.
Winforms support data binding.
' In form constructor
public Sub New()
InitializeComponent()
cmbEnableButtons.DataSource = New List(Of string) From
{
"Nothing",
"Button Row 1",
"Button row 2"
}
button1.Tag = "Button Row 1"
button2.Tag = "Button Row 1"
button3.Tag = "Button Row 2"
button4.Tag = "Button Row 2"
button1.DataBindings.Add(CreateBindingForVisible())
button2.DataBindings.Add(CreateBindingForVisible())
button3.DataBindings.Add(CreateBindingForVisible())
button4.DataBindings.Add(CreateBindingForVisible())
}
Private Function CreateBindingForVisible() As Binding
Dim buttonBinding =
New Binding("Visible",
cmbEnableButtons,
"SelectedValue",
true,
DataSourceUpdateMode.OnPropertyChanged)
' Every time selected value of combobox changed
' this event handler convert string to "visible" boolean
AddHandler buttonBinding.Format, AddressOf ButtonBinding_Format
return buttonBinding;
End Sub
Private Sub ButtonBinding_Format(object sender, ConvertEventArgs e)
Dim binding = DirectCast(sender, Binding)
Dim button = DirectCast(binding.Control, Button)
e.Value = Equals(button.Tag, e.Value)
End Sub
With data binding you can configure every button separately from each other, while having common logic in one place.

Hide lables, textboxes and combo boxes on MS Access form

I am using MS Access 2007.
How can I hide labels, text boxes and combo boxes in a form when I click a button on another form?
Here is what I have already tried but getting an error: application-defined or object-defined error:
Private Sub cmdAdd_Click()
Form!frmDetails!Label105.Visible = False
Form!frmDetails!Combo110_Label.Visible = False
Form!frmDetails!Combo110.Visible = False
Form!frmDetails!Label27.Visible = False
Form!frmDetails![First Name].Visible = False
Form!frmDetails![Second Name].Visible = False
Form!frmDetails!LastName.Visible = False
Form!frmDetails![ID type_Label].Visible = False
Form!frmDetails!Combo43.Visible = False
Form!frmDetails![ID No].Visible = False
Form!frmDetails!Label33.Visible = False
Form!frmDetails!Address.Visible = False
Form!frmDetails!Address2.Visible = False
Form!frmDetails!Address3.Visible = False
Form!frmDetails!Label39.Visible = False
Form!frmDetails![Contact No].Visible = False
Form!frmDetails!Label112.Visible = False
Form!frmDetails!Combo113.Visible = False
Form!frmDetails!Label113.Visible = False
Form!frmDetails!cboStatus.Visible = False
Form!frmDetails!cmdSave.Visible = False
Form!frmDetails!cmdPrint.Visible = False
End Sub
regards.
joseph
Are all these controls on the same form on which the button you are using to hide? If so you can simply use Me.controlName.Visible
apart from that, you are using Form!formName!controlName.Visible where it should be, Forms!formName!controlName.Visible
Private Sub cmdAdd_Click()
Forms!frmDetails!Label105.Visible = False
Forms!frmDetails!Combo110_Label.Visible = False
Forms!frmDetails!Combo110.Visible = False
Forms!frmDetails!Label27.Visible = False
Forms!frmDetails![First Name].Visible = False
Forms!frmDetails![Second Name].Visible = False
Forms!frmDetails!LastName.Visible = False
Forms!frmDetails![ID type_Label].Visible = False
Forms!frmDetails!Combo43.Visible = False
Forms!frmDetails![ID No].Visible = False
Forms!frmDetails!Label33.Visible = False
Forms!frmDetails!Address.Visible = False
Forms!frmDetails!Address2.Visible = False
Forms!frmDetails!Address3.Visible = False
Forms!frmDetails!Label39.Visible = False
Forms!frmDetails![Contact No].Visible = False
Forms!frmDetails!Label112.Visible = False
Forms!frmDetails!Combo113.Visible = False
Forms!frmDetails!Label113.Visible = False
Forms!frmDetails!cboStatus.Visible = False
Forms!frmDetails!cmdSave.Visible = False
Forms!frmDetails!cmdPrint.Visible = False
End Sub