Assign value to checkboxes - vba

Hye there, I new with vba here.
I want to use checkboxes to link with the series collections of a chart. I put the check boxes in a sheet which contain the chart altogether. I have a lot of checkboxes here to be assigned to "true" value.
Private Sub Controls_Initialize()
'Make default for checkboxes
CheckBox1.Value = True
CheckBox2.Value = True
CheckBox3.Value = True
CheckBox4.Value = True
CheckBox5.Value = True
CheckBox6.Value = True
CheckBox7.Value = True
CheckBox8.Value = True
CheckBox9.Value = True
CheckBox10.Value = True
CheckBox11.Value = True
CheckBox12.Value = True
CheckBox13.Value = True
CheckBox14.Value = True
CheckBox15.Value = True
CheckBox16.Value = True
CheckBox17.Value = True
CheckBox18.Value = True
CheckBox19.Value = True
CheckBox20.Value = True
CheckBox21.Value = True
CheckBox22.Value = True
CheckBox23.Value = True
CheckBox24.Value = True
End Sub
I have tried this code but can't
For i = 1 to 24
Controls("CheckBox" & i).Value = True
Next i
The questions are
1. Is there any other code that can make it simple?
2. How to link the check boxes with the series collection in the activechart? Example, if the checkbox return value false, the series collection will be deleted/hide(perhaps?). And when it returns value true, the series collection of the same data will be added back in the chart. I would like to make the chart interactive.
If there is any reference that I can reviewed, do tell me.
Thanks in advance.
Regards.

Alright, so assuming from what you've given, I'd think the problem is that the interpreter doesn't know i is an integer.
To fix this, we can implement something along the lines of Dim i As Integer to implement i as an integer.
We could try this:
Dim i As Integer
For i = 1 to 24
Controls("CheckBox" & i).Value = True
Next i

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.

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.

Button to change boolean to true or false; not always false?

I have code for a button that sets a boolean named ProjectCompleteOrgBtn.
I am trying to make this button set the boolean to true if you click it, then if you click it again after it makes it false, then again true, repeat.
The true and false sort the form below differently. Though at the moment everytime you click the button, it is always true. How do I fix this?
Private Sub ProjectCompleteOrgBtn_Click()
Dim ProjectCompeleteOrgB As Boolean
If ProjectCompleteOrgB = True Then
Set ProjectCompleteOrgB = False
Else
Set ProjectCompleteOrgB = True
If ProjectCompleteOrgB = False Then
MsgBox (False)
Forms!DatabaseF.ProjectQSubF.Form.RecordSource = "Select * from ProjectsQ ORDER BY ProjectComplete ASC"
Forms!DatabaseF.ProjectQSubF.Form.Refresh
Else
MsgBox (True)
Forms!DatabaseF.ProjectQSubF.Form.RecordSource = "Select * from ProjectsQ ORDER BY ProjectComplete DESC "
Forms!DatabaseF.ProjectQSubF.Form.Refresh
ProjectCompleteOrgB = False
End If
End Sub
First, you do
Dim ProjectCompeleteOrgB As Boolean
but notice it is spelled wrong (CompElete).
Then you write
If ProjectCompleteOrgB = False Then
but your code only changes that property or variable's value in the True block of your If. It doesn't get changed anywhere else.
Furthermore, you can make this:
If ProjectComplete = True Then
Set ProjectComplete = False
Else
Set ProjectComplete = True
much shorter by just writing
ProjectComplete = Not ProjectComplete

Gridview Show and Hide a Specific Column

I have a gridview in which a specific column Date. I have set the Visible property of column to false because I want to show on different conditions of page. Please tell me how can I do it using vb.net that my Date column should show or hide at runtime
Update
My current code is
If Not Page.User.Identity.Name = "bilal" Then
GridView1.AutoGenerateEditButton = False
GridView2.AutoGenerateEditButton = False
GridView3.AutoGenerateEditButton = False
Else
GridView1.AutoGenerateEditButton = True
GridView1.AutoGenerateColumns = True
GridView1.DataBind()
If GridView1.Columns.Count > 0 Then
'assuming your date-column is the first '
GridView1.Columns(3).Visible = True
Else
GridView1.HeaderRow.Cells(0).Visible = False
For Each gvr As GridViewRow In GridView1.Rows
gvr.Cells(0).Visible = True
Next
End If
GridView2.AutoGenerateEditButton = True
GridView3.AutoGenerateEditButton = True
End If
If you've set AutoGenerateColumns to True, the Column-Count will be 0, then you need to loop the rows and show/hide the appropriate cells. Otherwise you can use the Visible property.
GridView1.DataBind()
If GridView1.Columns.Count > 0 Then
'assuming your date-column is the 4.'
GridView1.Columns(3).Visible = True
Else
GridView1.HeaderRow.Cells(3).Visible = False
For Each gvr As GridViewRow In GridView1.Rows
gvr.Cells(3).Visible = True
Next
End If