Results 1 to 12 of 12

Thread: [RESOLVED] Help with list box items and Textbox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    27

    Resolved [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:
    1. Private Sub listBoxCompInfo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles listBoxCompInfo.SelectedIndexChanged
    2.  
    3.         listBoxCompInfo.SelectedItem(0) = rtbCompName.Text
    4.         listBoxCompInfo.SelectedItem(1) = rtbCompAddr1.Text
    5.         listBoxCompInfo.SelectedItem(1) = rtbCompAddr2.Text
    6.         listBoxCompInfo.SelectedItem(2) = rtbProgrammer.Text
    7.         listBoxCompInfo.SelectedItem(3) = rtbPartNumer.Text
    8.         listBoxCompInfo.SelectedItem(4) = rtbMachineName.Text
    9.         listBoxCompInfo.SelectedItem(5) = rtbComments.Text
    10. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveSettngs.Click
    11.         Dim ItemArray(Me.listBoxCompInfo.Items.Count - 1) As Object
    12.         Me.listBoxCompInfo.Items.CopyTo(ItemArray, 0)
    13.         Dim Data As String = Join(ItemArray, Environment.NewLine)
    14.         My.Computer.FileSystem.WriteAllText("c:\test.txt", Data, False)
    15.     End Sub

    Any Help would be greatly appreciated!

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Help with list box items and Textbox

    listBoxCompInfo.Items(0) = rtbCompName.Text

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    27

    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?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Help with list box items and Textbox

    use custom listItems:

    vb Code:
    1. Public Class listItem
    2.  
    3.     Public displayText As String
    4.     Public associatedText As String
    5.  
    6.     Public Overrides Function ToString() As String
    7.         Return Me.displayText
    8.     End Function
    9.  
    10.     Public Sub New(ByVal display As String, ByVal hidden As String)
    11.         Me.displayText = display
    12.         Me.associatedText = hidden
    13.     End Sub
    14.  
    15. End Class

    then to add to your listbox:

    vb Code:
    1. listbox1.items.add(new listItem("text to show in listbox", "associated text"))

    to retrieve your associated text for a listbox entry:

    vb Code:
    1. MsgBox(DirectCast(ListBox1.Items(0), listItem).associatedText)

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    27

    Re: Help with list box items and Textbox

    To make it a little more clear below see below.

    Name:  pic351.bmp
Views: 197
Size:  272.8 KB

    When the user re-arranges the order the text file is written in that order.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Help with list box items and Textbox

    ok. how are you changing the order of the listbox items?

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    27

    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:
    1. Private Sub listBoxCompInfo_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles listBoxCompInfo.DragDrop
    2.         listBoxCompInfo.Items.Insert(listBoxCompInfo.IndexFromPoint(listBoxCompInfo.PointToClient(New Point(e.X, e.Y))), e.Data.GetData(listBoxCompInfo.Text))
    3.         listBoxCompInfo.Items.RemoveAt(listBoxCompInfo.SelectedIndex)
    4.     End Sub
    5.  
    6.     Private Sub listBoxCompInfo_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles listBoxCompInfo.DragOver
    7.         e.Effect = DragDropEffects.Move
    8.     End Sub
    9.  
    10.     Private Sub listBoxCompInfo_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles listBoxCompInfo.MouseDown
    11.         listBoxCompInfo.DoDragDrop(listBoxCompInfo.Text, DragDropEffects.All)

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Help with list box items and Textbox

    try this:
    Attached Files Attached Files

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    27

    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

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    27

    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.

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Help with list box items and Textbox

    i'd go with the usercontrol

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    27

    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:
    1. Public Class TabPageEx
    2.     Inherits System.Windows.Forms.TabPage
    3.  
    4.     Private WithEvents uc As New UserControl1
    5.     Private WithEvents uc2 As New UserControl2
    6.  
    7.  
    8.     Public Sub New()
    9.         Me.uc.Margin = New Padding(0)
    10.         Me.uc.Dock = DockStyle.Fill
    11.         Me.Controls.Add(Me.uc)
    12.         Me.UseVisualStyleBackColor = True
    13.         Me.uc2.Margin = New Padding(0)
    14.         Me.uc2.Dock = DockStyle.Fill
    15.         Me.Controls.Add(Me.uc2)
    16.         Me.UseVisualStyleBackColor = True
    17.     End Sub
    18.  
    19.  
    20.     Private Sub InitializeComponent()
    21.  
    22.     End Sub
    23.    
    24. End Class

    Here is the button click event that adds the tabpage.

    Code Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.  
    3.         Dim Mytab As New TabPageEx
    4.         frmPrefsDialog.TabControl1.TabPages.Add(Mytab)
    5.         Mytab.Text = RichTextBox1.Text
    6.         frmPrefsDialog.TabControl1.SelectedTab = Mytab
    7.  
    8.         Me.Close()
    9.  
    10.     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
    Last edited by RCA20; Dec 24th, 2009 at 07:58 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width