[RESOLVED] Help with list box items and Textbox
I have struggled to find a way to make this work. I have a listbox that has 5 items. These items are lets say Company Name, Address, User Name, Phone, Fax. On the form I also have the same labels with richtextboxes under each of the above names.
What I am looking to do is have each listbox item equal the text that is related to each textbox. Then write to file from the listbox.The file created should be the text from the related textboxes.
The goal is to allow the user to re-arrange the items in the listbox via drag and drop (which I think I have figured out) and write a new order of the file.
Here is my code Code:
Private Sub listBoxCompInfo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listBoxCompInfo.SelectedIndexChanged
listBoxCompInfo.SelectedItem(0) = rtbCompName.Text
listBoxCompInfo.SelectedItem(1) = rtbCompAddr1.Text
listBoxCompInfo.SelectedItem(1) = rtbCompAddr2.Text
listBoxCompInfo.SelectedItem(2) = rtbProgrammer.Text
listBoxCompInfo.SelectedItem(3) = rtbPartNumer.Text
listBoxCompInfo.SelectedItem(4) = rtbMachineName.Text
listBoxCompInfo.SelectedItem(5) = rtbComments.Text
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveSettngs.Click
Dim ItemArray(Me.listBoxCompInfo.Items.Count - 1) As Object
Me.listBoxCompInfo.Items.CopyTo(ItemArray, 0)
Dim Data As String = Join(ItemArray, Environment.NewLine)
My.Computer.FileSystem.WriteAllText("c:\test.txt", Data, False)
End Sub
Any Help would be greatly appreciated!
Re: Help with list box items and Textbox
listBoxCompInfo.Items(0) = rtbCompName.Text
Re: Help with list box items and Textbox
Paul,
Thank you for your response. I feel like such an idiot. I did make the change and it does change the listbox item to the text that was input from the textbox. What I was looking for is too not have the listbox item text change but rather make an association to the listbox item and the textbox. Maybe I am going about this all wrong or using the wrong controls. How can I make a listbox item retrieve the string in text box without changing either the listbox item text or the textbox text.
The textboxes are to be input fields for the user to enter information. The listbox items need to be connected to the correct textbox somehow in order for the user to re-arrange the entered information in the listbox. When the user clicks a button that information will be written to the file. As it sits right now only the contents are being written to the file and not the textbox information.
Any ideas?
Re: Help with list box items and Textbox
use custom listItems:
vb Code:
Public Class listItem
Public displayText As String
Public associatedText As String
Public Overrides Function ToString() As String
Return Me.displayText
End Function
Public Sub New(ByVal display As String, ByVal hidden As String)
Me.displayText = display
Me.associatedText = hidden
End Sub
End Class
then to add to your listbox:
vb Code:
listbox1.items.add(new listItem("text to show in listbox", "associated text"))
to retrieve your associated text for a listbox entry:
vb Code:
MsgBox(DirectCast(ListBox1.Items(0), listItem).associatedText)
1 Attachment(s)
Re: Help with list box items and Textbox
To make it a little more clear below see below.
Attachment 75222
When the user re-arranges the order the text file is written in that order.
Re: Help with list box items and Textbox
ok. how are you changing the order of the listbox items?
Re: Help with list box items and Textbox
Using drag and drop. The user can change the order at runtime. Here is the code I have. Seems to have a small bug as the listbox items move but on the drop event become invisible.
Code Code:
Private Sub listBoxCompInfo_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles listBoxCompInfo.DragDrop
listBoxCompInfo.Items.Insert(listBoxCompInfo.IndexFromPoint(listBoxCompInfo.PointToClient(New Point(e.X, e.Y))), e.Data.GetData(listBoxCompInfo.Text))
listBoxCompInfo.Items.RemoveAt(listBoxCompInfo.SelectedIndex)
End Sub
Private Sub listBoxCompInfo_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles listBoxCompInfo.DragOver
e.Effect = DragDropEffects.Move
End Sub
Private Sub listBoxCompInfo_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles listBoxCompInfo.MouseDown
listBoxCompInfo.DoDragDrop(listBoxCompInfo.Text, DragDropEffects.All)
1 Attachment(s)
Re: Help with list box items and Textbox
Re: Help with list box items and Textbox
Bingo!!!! Paul you are the man!!! This is exactly what I was looking for. Thank you so much. You are might be the listbox king! Thanks again man for all the help I REALLY appreciated it and the example you shared is going to push my knowledge base a ton.
RCA20
Re: Help with list box items and Textbox
I noticed that this is on the form load event. The controls are in tabpage so can I assume that I can use the tabpage click event instead of form1 load? Or would it be more efficient to create a usercontrol then initiate that component.
Re: Help with list box items and Textbox
i'd go with the usercontrol
Re: Help with list box items and Textbox
That is what I thought. But I am having difficulties initiated the 2 control. The first one works perfect but am not quite sure how to get the second one to initiate.
Code for Custom Tabpage Class Code:
Public Class TabPageEx
Inherits System.Windows.Forms.TabPage
Private WithEvents uc As New UserControl1
Private WithEvents uc2 As New UserControl2
Public Sub New()
Me.uc.Margin = New Padding(0)
Me.uc.Dock = DockStyle.Fill
Me.Controls.Add(Me.uc)
Me.UseVisualStyleBackColor = True
Me.uc2.Margin = New Padding(0)
Me.uc2.Dock = DockStyle.Fill
Me.Controls.Add(Me.uc2)
Me.UseVisualStyleBackColor = True
End Sub
Private Sub InitializeComponent()
End Sub
End Class
Here is the button click event that adds the tabpage.
Code Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Mytab As New TabPageEx
frmPrefsDialog.TabControl1.TabPages.Add(Mytab)
Mytab.Text = RichTextBox1.Text
frmPrefsDialog.TabControl1.SelectedTab = Mytab
Me.Close()
End Sub
Now I am struggle on how to inititiate the second usercontrol still using the TabPageEx Class. Do I need to make another seperate Class Such as TabpageEx2 too handle usercontrol2? The usercontrol2 needs to happen on form load so that the tab is always present. This may be a simply answer and apologize. I truly am trying to learn this at times gets so confusing.
I realized that this should be another topic. Mod please mark this as resolved
Thanks Again