Programmatically check a checkbox in ListView - vb.net

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

Related

DataGridView: programmatically select/highlight entire column on button click

I want to programmatically select/highlight entire column on button click to let users know what they searched.
This is what currently happens on button click ("GO" button)
but this is what I need to happen.
I have tried these so far to no avail:
DataGridView2.SelectionMode = DataGridViewSelectionMode.FullColumnSelect
DataGridView2.Columns(2).Selected = True
Doing so gave me this error: DataGridView control's SelectionMode cannot be set to FullColumnSelect while it has a column with SortMode set to DataGridViewColumnSortMode.Automatic.
I also tried to simply select the whole column. No error, but it didn't work.
DataGridView2.Columns(2).Selected = True
Just set the Selected property of each cell in the column.
Found this thread. I modified the code like so:
Dim row As DataGridViewRow
For Each row In DataGridView2.Rows
row.Cells(1).Selected = True
Next
and placed it inside Private Sub Button1_Click
I noticed that the first cell is automatically selected. This solved that problem.

set Checkbox value from Datareader

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

IsChecked' is not a member of 'System.Windows.Forms.CheckBox'

I have a form with tab page containing several checkboxes.
I need to loop through these checkboxes to see which ones are checked. From there, I need to grab the .Tag property so that I can in turn add a property with that name from another object to t list.
I have a problem with my code though. For the line -
DirectCast(objCtrl, CheckBox).IsChecked
I'm getting the error
IsChecked' is not a member of 'System.Windows.Forms.CheckBox'
This seems odd, as I obviously need to use that method to check if the checkbox is actually checked, and the docs seem to suggest that method exists.
What am I doing wrong? Thanks.
Dim objCtrl As Control
For Each objCtrl In Me.objConfigForm.tabPageGeneral.Controls
If TypeOf objCtrl Is CheckBox AndAlso DirectCast(objCtrl, CheckBox).IsChecked Then
Dim strProp As String = DirectCast(objCtrl, CheckBox).Tag
arrGeneral.Add(objUser.strProp)
End If
Next
It seems that you are looking at the documentation for WPF CheckBox not to the docs for WinForms Checkbox
In the latter the property is named Checked
The Property you want is Checked not IsChecked
See the help for more info: http://msdn.microsoft.com/en-us/library/system.windows.forms.checkbox.checked(v=vs.110).aspx
The property is Checked, not IsChecked.

show/hide textbox by change value of combobox

i'm having problem regarding combobox in vb.net,
im trying to show & hide specific text boxes, on the base of an item when it is selected from combobox,
here is the code i'm writing, whats mistake in it?
Dim PatSearchID, PatSearchName, PatSearchCat As String
PatSearchID = txtSearchID.Text
PatSearchCat = cmbSearchPat.SelectedItem.ToString()
If PatSearchCat = "RegNo" Then
txtSearchID.Show()
txtSearchName.Hide()
End if
kindly correct what the problem is?
txtSearchName.Visible = False
30chars
instead of this (txtSearchID.Show() txtSearchName.Hide()) try to use the visible property of textbox

How to reference controls located on different Tabs (VB.NET)

I have an application written in VB.NET that reads data from a file and displays the data on the screen.
Depending on the data in the file, the program has a TabControl with up to 3 tabs and each tab in turn has a DataGridView for displaying data. For example I have a TabControl that has a tab called "Saturday" and a tab called "Sunday".
The problem I am having is that when I read data from a file, the program displays all the data on the Saturday's tab grid because I am not sure how to reference the Grid on the Sunday tab.
To add the DataGridView I am using the following code:
Grid = New DataGridView
Grid.Dock = DockStyle.Fill
Grid.Name = "Grid" & TabControl.SelectedIndex
Grid.Tag = "Grid" & TabControl.SelectedIndex
And this is how I am reading the data in:
If reader.GetAttribute("controltype") = "Tab" Then
SelectedTab = reader.Name
End If
If reader.Name = "cell" Then
y = y + 1
Grid.Rows(i).Cells(y).Style.BackColor = Color.FromName(reader.ReadElementString("cell"))
End If
What I almost want to do is something like (pseudocode):
SelectedTab.Grid.Rows(i).Cells(y).Style.BackColor = Color.FromName(reader.ReadElementString("cell"))
However when I use the above code it complains:
'Grid' is not a member of 'String'
I hope you understand the issue. Let me know if you need clarification
Your code is a little unclear. However, it appears to me that the following line:
If reader.GetAttribute("controltype") = "Tab" Then
SelectedTab = reader.Name
End If
is creating at least one problem. It looks like you are attempting to refer to a Tabpage control by the string representation of its name, but unless I missed something, what that line is actually doing is trying to make a tabpage control type("SelectedTab") refer to a string type. If that is the case, then you will want to try this instead:
If reader.GetAttribute("controltype") = "Tab" Then
TabControl1.SelectedTab = TabControl1.TabPages(reader.name)
End If
It is a little hard to tell from the code you have posted, but that might get you headed down the right path.
++++++++++++
UPDATE: It appears from your code that you are naming each DGV control by appending the index of the tab on which it is located to the string "grid." I am going to assume that you are using a class member variable named "SelectedTab" to represent the current tab selected in the control. I will assume that at the top of your class you have done something like this:
'Form-or-class scoped memebr variables:
Private SelectedTab As TabPage
Private SelectedGrid As DataGridView
You should be able to refer to the active grid control using something like this:
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
' Set SelectedTab member variable to refer to the new selected tab page:
SelectedTab = TabControl1.SelectedTab
' Set the SelectedGrid to refer to the grid control hosted on the selected tab page:
SelectedGrid = TabControl1.SelectedTab.Controls("Grid" & TabControl1.SelectedIndex.ToString())
End Sub
From here, you should be able to use the member variable for SelectedGrid to refer to the grid present on which ever tab page is selected in your tab control.
It is challenging to address your concerns with only fragments of your code. If you have additional difficulties, please post more of your code, so we can better see what else is going on.
Hope that helps!
Okay, I would go about something like this. Maybe you can simply use a DataSet to load the XML data in one line (if they have been saved with DataSet.WriteXML before).
Dim ds As New DataSet
Dim p As TabPage
Dim gv As DataGridView
ds.ReadXml("F:\testdata.xml")
For i As Integer = TabControl1.TabPages.Count - 1 To 0 Step -1
TabControl1.TabPages.RemoveAt(i)
Next
For Each dt As DataTable In ds.Tables
p = New TabPage(dt.TableName)
gv = New DataGridView
' ... configure the gv here...
gv.AutoGenerateColumns = True
gv.Dock = DockStyle.Fill
' ...
gv.DataSource = dt
TabControl1.TabPages.Add(p)
p.Controls.Add(gv)
Next