I am trying to pull information from a database into a form.
the database stores checkbox values as a single (-1 for true 0 for false)
However i am unable to set the .checked state of the checkbox with this.
frmTool.chkMeterFake.CheckState = dr("VALIDATE")
i have also tried
frmTool.chkMeterFake.CheckState = Convert.ToBoolean(dr("VALIDATE"))
Try using Checked property instead of CheckState:
frmTool.chkMeterFake.Checked = Convert.ToBoolean(dr("VALIDATE"))
The CheckState property is useful when you want your checkbox to have three possible states: checked, unchecked, or not set.
Why don't you use a bit column for booleans?
However, you can use this:
Dim validateIndex = dr.GetOrdinal("Validate")
frmTool.chkMeterFake.Checked = dr.GetFloat(validateIndex) = 0
Related
I have programmed a long time but I’m relatively new to vb.net. And I’ve avoided subroutines and functions in which I passed parameters because I always get stuck. I’m trying to write a subroutine to pass information that will fill a TextBox or a checkbox with either the value from a table or clear the field or set to false. The first code below is an example of what I’ve been doing and this works. I trying to write a subroutine to pass 1.the name of the textbox or checkbox control on my form,2.the data row value, and 3.the column name in the table. The problem is when I passed a checkbox I can’t get the checked event to show on my control(CoreCol) that I passed. It knows it’s a checkbox and it will set the text of the checkbox too true or false but it won’t change the box checked.
This is an example of the old way that works. For a TextBox and a checkbox
' A Machine
If Not IsDBNull(r("A Machine")) Or Not IsNothing(r("A Machine")) Then
TbXMachA.Text = r("A Machine")
Else
TbXMachA.Text = ""
End If
If Not IsDBNull(r("A CO2 Box?")) Or Not IsNothing(r("A CO2 Box?")) Then
CkbxCO2BoxA.Checked = r("A CO2 Box?")
Else
CkbxCO2BoxA.Checked = False
End If
This works
LoadData2TextBox(Me. TbXMachA, r, "A Machine ")
This doesn’t
LoadData2TextBox(Me.CkbxCO2BoxA, r, "A CO2 Box?")
this is the sub routine I'm writing
Private Sub LoadData2TextBox(ByRef CoreCol As Control, CoreRow As DataRow, BoxStage As String)
If Not IsDBNull(CoreRow(BoxStage)) Then
If TypeOf CoreCol Is TextBox Then
CoreCol.Text = CoreRow(BoxStage)
End If
If TypeOf CoreCol Is CheckBox Then
CoreCol.??? = CoreRow(BoxStage)
End If
Else
CoreCol.Text = ""
End If
You know that CoreCol is a CheckBox so you can cast it as one then use it as a CheckBox.
If TypeOf CoreCol Is CheckBox Then
Dim myCheckBox = DirectCast(CoreCol, CheckBox)
myCheckBox.Checked = DirectCast(CoreRow(BoxStage), Boolean)
End If
Another cast in getting the boolean value out of CoreRow(BoxStage). The above code assumes this will work, but I am not sure what is in CoreRow(BoxStage). You may need to add some logic based on the value depending on what it is. For example:
myCheckBox.Checked = CoreRow(BoxStage) = "somevalue"
I'm working with VBA, and I'm struggling to get a combobox with two options:
First option: the textbox next to it must appear one "-", like if is supposed to be empty or disabled.
Second option: the same texbox must must be able to receive an input, like numbers.
Like: "Do you have an ID?" if no, don't fill the texbox. If yes, fill it with your number.
Thanks!
I'm assuming C# implementation but this would work essentially for any .net or WINFORMS project.
if(cbo.selectedindex = 0)
tbFoo.text = "-";
else if(cbo.selectedindex == 1)
tbFoo.text = "filltextwithID";
Check if the selectedindex of your combobox is the first or second options in the list and do your first option with the first selectedindex, else if it is the second, fill your textbox with whatever you need to fill it with(textwise).
Use your conditional if statement(naturally, I know) to check your conditions that you want your combobox to do to your textbox.
Otherwise another way to do this is with a selectedindexchanged event and do your switch statement or if statement based on which selectedindex you are talking about. 0 is the first item....all the way up to n items.
I got it! Here's the code:
Private Sub ComboBox_Change()
If ComboBox = "I don't have an ID" Then
IdTextBox.Visible = False 'Hidden
IdLabel.Visible = False
Else
IdTextBox.Visible = True 'Unhidden
IdLabel.Visible = True
End If
End Sub
I know there must be a simple solution to this question, but I seem to be missing the answer.
I have a Visual Studio 2008 Winforms application where I created a datagridview to display data to users. In the datagridview I allow the users to edit information. One of the cells is set to read only and the users want to be able to edit this information now. When I go into the datagridview designer to set the cell to Readonly = False the change does not get saved. Does not matter what I do the change WILL not get saved. I am now looking into changing this at run time using the code below:
When I use that code I get the error mentioned in the title. It seems to me that I am stuck now... I can't change the cell to readonly = false using the Visual Studio designer. I can't set the cell to readonly = false at runtime.
Question: What am I doing wrong? Is there something else I can do? This is quite a sizable application for multiple users and this is a request made by most of the users.
Any help would be appreciated.
Dim oDL As New MTN.BusinessLayer.MasterTables()
Dim dt As DataTable = oDL.GetTheItems() DataGridView1.DataSource = dt
You can actually set the ReadOnly property for the DataTable column!
Dim dt As DataTable = oDL.GetTheItems()
dt.Columns("Selected").ReadOnly = false
DataGridView1.DataSource = dt
Have you tried? (setting properties) :
AllowUserToAddRows = False
AllowUserToDeleteRows = False
ReadOnly = True
First Set DataTable ReadOnly Property False
Then Set Gridview ReadOnly Property false
EX-
DataTable dt=("Select * prom priceList");
dt.Columns["Rate"].ReadOnly = false;
DataGrid View dgv=.dataSource=dt;
dgv.Columns["Rate"].ReadOnly = false;
I am trying to programmatically check a checkbox of a ListView (using VB & .NET 4).
The ListView lvVorschlag has been created in the designer, along with three elements. I then do the following:
Dim lviOptimal As New ListViewItem("Optimal")
lviOptimal.SubItems.Add(...) 'several SubItems are added
lvVorschlag.Items.Add(lviOptimal)
lvVorschlag.Items(0).Selected = True
All the SubItems are correctly added and the line lvVorschlag.Items(0).Selected = True does not give me an error. But nothing is checked. Any idea why?
Note: I also tried with lvVorschlag.Items("Optimal").Selected = True but it gives me an error saying that this object is Nothing. Too bad, referring by name would have been easier.
You should use the Checked property to check the item(s) you'd like:
lvVorschlag.Items(0).Checked = True
Set the focus on the item
Dim lviOptimal As New ListViewItem("Optimal")
lviOptimal.SubItems.Add(...) 'several SubItems are added
lvVorschlag.Items.Add(lviOptimal)
lvVorschlag.Items(0).focus()
lvVorschlag.Items(0).Selected = True
http://msdn.microsoft.com/en-us/library/y4x56c0b%28v=vs.100%29.aspx
This line of code references a checkbox in a Gridview, how can I assign the value of the check box 1 or -1, I think it is for checked or unchecked, to a variable so that I can run an if statement against it, and change it to True or False?
dt.Rows(row.DataItemIndex)("DisplayString") = (CType((row.Cells(3).Controls(0)), CheckBox)).Checked
Try this:
Dim checkBox1 as CheckBox = CType((row.Cells(3).Controls(0)), CheckBox)
checkBox1.Checked = True
Edit
I'm not sure I completely understand what it is you are trying to do?
Are are you are attempting to check/uncheck the CheckBox based on the values 1 or -1?
Could you elaborate please?
Here is the solution that worked for me. I need to read the condition/status of the check box and use an if statement on the result....
dt.Rows(row.DataItemIndex)("DisplayString") = (CType((row.Cells(1).Controls(0)), CheckBox)).Checked
Dim CB As CheckBox = CType((row.Cells(1).Controls(0)), CheckBox)
value = CB.Checked
Now I can use 'value' in my if statements.