Edit item or subitem values of a selected listview item - vb.net

Ok, so I have a listview on one form, and when a button is pressed it opens up a new form with the contents of the selected listview item and it's subitems in a series of textboxes. The user can then change the data in the textboxes and either press save to make the changes or cancel to close the window.
What command would I use to change the selected listview item and subitems to whatever is in the boxes?
this is the code that populates the boxes:
Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim appeditcontents As String = main.passlist.SelectedItems(0).ToString
Dim appstrlen As Integer = appeditcontents.Length()
Dim apptotal As Integer = appstrlen - 16
Dim usereditcontents As String = main.passlist.SelectedItems(0).SubItems(1).ToString
Dim userstrlen As Integer = usereditcontents.Length()
Dim usertotal As Integer = userstrlen - 19
Dim passeditcontents As String = main.passlist.SelectedItems(0).SubItems(2).ToString
Dim passstrlen As Integer = passeditcontents.Length()
Dim passtotal As Integer = passstrlen - 19
appedit.Enabled = False
appedit.Text = main.passlist.SelectedItems(0).ToString.Substring(15, apptotal)
useredit.Text = main.passlist.SelectedItems(0).SubItems(1).ToString.Substring(18, usertotal)
passedit.Text = main.passlist.SelectedItems(0).SubItems(2).ToString.Substring(18, passtotal)
End Sub
Any pointers on cleaning up this code would probably help too.

This page looks like it would help you.

Related

Adding item into listview1 and remove if the item has already in the listview1 or replace

I have listview1 with 2 columns. I already have a code for adding items but my problem is when I add again the same item in listview1, it shows duplicates and when I continue to add the same item, the items in listview are increasing with same data.
Example for what I need:
When listview1 has already a data in 1st column = 1 and in 2nd column = A, and I want to add again with a data like these 1st column = 1 and in 2nd column = B. I have 2 solutions and I try to code it but with no luck. My 2 solutions is these:
I just want to update only the data in 2nd column, from "A" to "B" but with the same 1st column data and no additional item will be added into listview like nothing just update/replace it.
OR
Remove the item that will cause duplication and add the same item so there will be no duplicates.
Here's my code for adding item:
Private Sub rbChoiceA_Checked(ByVal sender As Object, ByVal e As EventArgs)
Dim rbA As RadioButton = TryCast(sender, RadioButton)
Dim str As String = rbA.Parent.Name
str = str.Remove(0, 6)
lab1.Text = str
Dim item As ListViewItem
Dim row As String() = New String(2) {}
row(0) = str
row(1) = rbA.Text
item = New ListViewItem(row)
ListView1.Items.Add(item)
End Sub
I just need help to add the code for my problem. Your answers is highly appreciated. Thanks in advance!
Find the item before add it
If (ListView1.FindItemWithText(row(0)) Is Nothing) Then
ListView1.Items.Add(item)
End If
I just figured it out because of others help also.I created a function as Dictionary.This code is updating the subitem without adding or deleting item.
Dim thisDict As New Dictionary(Of String, String)
Public Sub listviewupdate(ByVal D As Dictionary(Of String, String))
ListView1.Items.Clear()
For Each KVP As KeyValuePair(Of String, String) In D
Dim LVI As New ListViewItem(KVP.Key)
LVI.SubItems.Add(KVP.Value)
ListView1.Items.Add(LVI)
Next
End Sub
Private Sub rbChoiceA_Checked(ByVal sender As Object, ByVal e As EventArgs)
Dim rbA As RadioButton = TryCast(sender, RadioButton)
Dim str As String = rbA.Parent.Name.Remove(0, 6)
lblItemNo.Text = str
If thisDict.ContainsKey(str) Then thisDict.Remove(str)
thisDict.Add(str, rbA.Text)
listviewupdate(thisDict)
End Sub

Datagridview with combobox cannot fill in

Have datagridview which contains 4 columns created manually in datagridview creator. 1st, 2nd and 3rd columns are just textbox columns but the last one is marked as comboboxcolumn. On my form i have button, when user clicks it new row is add to datagridview. For first three columns data is filled up from some variables, and the last combobox column should be filled up for user from datasource so he could select his value out of it. The problem is i have problems with this combobox column and so far couldn't fill it in. This is my actual code
Private Sub myButton_Click(sender As Object, e As EventArgs) Handles btnAddMatType.Click
Dim dt as DataTable
dt = New Variation().GetAll() 'returning Ids and Names
Dim cbo = CType(dgvMaterials.Columns(3), DataGridViewComboBoxColumn)
cbo.Items.AddRange(dt.AsEnumerable().Select(Function(s) s.Field(Of String)("Name")).ToArray())
try
Dim rodzajID as String = TreeMaterials.SelectedValue
Dim rodzajName as string = TreeMaterials.SelectedNode.Text
Dim material as string = TreeMaterials.SelectedNode.Parent.Text
dgvMaterials.Rows.Add(material, rodzajName, rodzajID)
End Sub
Tried also this:
Private Sub myButton_Click(sender As Object, e As EventArgs) Handles btnAddMatType.Click
Dim dt as DataTable
dt = New Variation().GetAll() 'returning Ids and Names
Dim cbo = CType(dgvMaterials.Columns(3), DataGridViewComboBoxColumn)
cbo.DataSource = dt
cbo.ValueMember = "Id"
cbo.DisplayMember = "Name"
Dim rodzajID as String = TreeMaterials.SelectedValue
Dim rodzajName as string = TreeMaterials.SelectedNode.Text
Dim material as string = TreeMaterials.SelectedNode.Parent.Text
dgvMaterials.Rows.Add(material, rodzajName, rodzajID)
End Sub
In both cases every time user clicks button row is added to datagrid but last column's combobox is empty. How to solve that?
Try to add this line for asociate your comboBox to the wanted column :
dgvMaterials.Columns.Insert(3, cbo)

How would I use a for loop to edit multiple labels in Visual basic for visual studios

Here is my code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim randVal As Integer
Dim label As String
Dim val As Integer
Dim stringVal As String
For i As Integer = 1 To 256 Step 1
val = i
stringVal = CStr(val)
label = "Label" + stringVal
randVal = CInt(Math.Floor((20 - 1 + 1) * Rnd())) + 1
label.BackColor = Color.Green
Next
End Sub
I get an error that string has no property BackColor.
How would I be able to edit all the strings without calling them individually?
The error message is correct: there isn't a property called BackColor on a string.
There is a BackColor property on Button, however, and it looks as if you're perhaps trying to set the background color of the Button object when it's clicked. If so, then you need to get hold of the Button object before you can set the color. The event handler has made this (moderately) easy, by passing the object into your handler as the parameter "sender". The only problem is that it's sent it as an object, not as a Button, so you first have to cast it to the type you want, like this:
Dim button As Button
button = DirectCast(sender, Button)
Then later on you can set the color:
button.BackColor = Color.Green
Also, if you want to set the text of the button, you have to set it using the button.Text property:
button.Text = "What I want to see on the button"
However, you're program is hard to follow. I can't see clearly from the code why you're executing a loop, or why you're setting values like randVal that aren't being used, and so it's hard to give concrete advice.
You are trying to access a property that does not exist.
The String type in Visual Basic has two properties. See here (https://msdn.microsoft.com/en-us/library/system.string_properties(v=vs.110).aspx)
If you are trying to change the background color of your label, then you need to reference your label name, which has that property.
For example: (I am assuming you want to change the text and color on your label)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim randVal As Integer
Dim labelText As String
Dim val As Integer
Dim stringVal As String
For i As Integer = 1 To 256 Step 1
val = i
stringVal = CStr(val)
labelText = "Label" + stringVal
randVal = CInt(Math.Floor((20 - 1 + 1) * Rnd())) + 1
labelName.BackColor = Color.Green
Next
End Sub
Having said that, you can also use your label properties to change the text of your label.
labelName.Text = "Label" + stringVal
Here are some references
(How to change the text color. Simple?)
Also, here is a link to Label Properties (https://msdn.microsoft.com/en-us/library/system.windows.forms.label_properties(v=vs.110).aspx)
and your Buttons properties (https://msdn.microsoft.com/en-us/library/system.windows.forms.button(v=vs.110).aspx)
You managed to set the variable label to the name of your Label control, but not to the object itself. Now you need to get that Control object from its name.
VB6:
Me.Controls(label).BackColor = vbGreen
VB.Net:
Me.Controls(label).BackColor = Color.Green
However, you had an easier way to span you label controls and update their backgrounds:
Dim c as Control
For Each c In Me.Controls
If (TypeOf c Is Label) Then c.BackColor = Color.Green
Next c
If you want to reference Label1 thru Label256 in a loop, no matter which container they are in, then use Controls.Find():
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i As Integer = 1 To 256
Dim lbl As Label = Me.Controls.Find("Label" & i, True).FirstOrDefault
If Not IsNothing(lbl) Then
lbl.BackColor = Color.Green
End If
Next
End Sub

How to copy selected item style to another item in Solid Edge?

I'm developing a macro for Solid Edge which is stores values of things like font size, width etc. and applies them to another object. Both functions are executable by button click. First button is saving the property values and second applies them to another object. The problem is that I have no clue which methods or functions should I use to store the values.
Public Class Form1
Dim solidedge As SolidEdge.Framework.Interop.Application
Dim line As SolidEdge.Framework.Interop.SelectSet
Dim item As SolidEdge.FrameworkSupport.Interop.Line2d
Dim style As SolidEdge.FrameworkSupport.Interop.GeometryStyle2d
Dim breite As Double
Dim dashname As String
Dim autophase As Boolean
Dim dashgapcount As Integer
Dim dashstrokepercent As Double
Dim color As Integer
Dim linearname As String
Dim units As Integer
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
solidedge = GetObject(, "SolidEdge.Application")
line = solidedge.ActiveSelectSet
item = line.Item(1)
style = item.Style
breite = style.Width
autophase = style.AutoPhase
dashgapcount = style.DashGapCount
dashstrokepercent = style.DashStrokePercent
color = style.LinearColor
linearname = style.LinearName
units = style.Units
dashname = style.DashName
End Sub
End Class
Example: I want make the black line look like the pink line by copying format:
I am not familiar with Solid Edge, but based on the code you posted, you could try something like this:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
solidedge = GetObject(, "SolidEdge.Application")
line = solidedge.ActiveSelectSet
item = line.Item(2)
item.Style = style
End Sub

How To Clear CheckedListBox From Previous Selection - VB.NET

Okay so I have a checkedlistbox that when a user selects a item it will print out that item into word. That works perfect. However, I want to give the user the option to not select anything at all, but when the user does not select an item in the checkboxlist, it still prints out the previous selected item into MS word.
Below is my code:
Private Sub ExportContactOkButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportContactOkButton.Click
Dim i As Integer
Dim array_Counter_Contacts As Integer
Dim array_size_Contacts As Integer
array_Counter_Contacts = 0
For i = 0 To ExportContactCheckedListBox.Items.Count() - 1
If ExportContactCheckedListBox.GetItemCheckState(i) = CheckState.Checked Then
array_size_Contacts = UBound(SelectedContacts)
ReDim Preserve SelectedContacts(array_size_Contacts + 1)
SelectedContacts(array_Counter_Contacts) = ExportContactCheckedListBox.Items(i)
If Search.debugging = True Then
MsgBox(ExportContactCheckedListBox.Items(i))
End If
array_Counter_Contacts += 1
ExportContactCheckedListBox.Items(i) = ExportContactCheckedListBox.Items(i).ToString.Replace("'", "''")
If array_Counter_Contacts = 1 Then
ContactNames = "" & ExportContactCheckedListBox.Items(i) & ""
Else
ContactNames = String.Concat(ContactNames & "', '" & ExportContactCheckedListBox.Items(i) & "")
End If
End If
Next
If Search.debugging = True Then
MsgBox(ContactNames)
End If
sConnection.Close()
Dispose()
Me.Close()
End Sub
I have tried remove, clear, and I even tried this
Dim i As Integer
For i = 0 To ExportContactCheckedListBox.CheckedIndices.Count - 1
ExportContactCheckedListBox.SetItemChecked(ExportContactCheckedListBox.CheckedIndices(0), False)
Next i
But nothing is working. Can anyone help me? All I want is to be able to have the checkedlistbox forget or clear the checked item after the "OK" button is pressed and the text has already been printed into word.
Use a List(Of String) to store the selection and, of course remember, to reinitialize the list when you hit the ExportContactOkButton
' Declared at the form level....
Dim SelectedContacts as List(Of String)
.....
Private Sub ExportContactOkButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExportContactOkButton.Click
Dim i As Integer
SelectedContacts = new List(Of String)()
For i = 0 To ExportContactCheckedListBox.Items.Count() - 1
If ExportContactCheckedListBox.GetItemCheckState(i) = CheckState.Checked Then
SelectedContacts.Add(ExportContactCheckedListBox.Items(i))
.....
End If
Next
End Sub
In this way, every time you hit the ExportContactOKButton you reinitialize your list of contacts, loop through the checked items, add the checked one to your list.
A List(Of String) is better because you don't need to know in advance how many items your user selects in the CheckedListBox and continuosly resize the array. You simply add new items to it. And you could always use it like an array
Dim Dim array_Counter_Contacts = SelectedContacts.Count
For i = 0 to array_Counter
Console.WriteLine(SelectedContacts(i))
Next