Results 1 to 21 of 21

Thread: [RESOLVED] List Box

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2012
    Posts
    209

    Resolved [RESOLVED] List Box

    Does anyone know how to set up a list of items in a List Box and then write a code so a user double-clicking an item in that List Box would have the word he double clicked appear in a text box on a different form?

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: List Box

    of course

    Code:
    Form2.Text1.Text=Form1.List1.Text

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2012
    Posts
    209

    Re: List Box

    Is there a way to add to the text box instead of changing the entire text of the textbox?

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: List Box

    Code:
    Form2.Text1.Text=form2.Text1.Text & Form1.List1.Text

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2012
    Posts
    209

    Re: List Box

    I was wondering how to do it where the person clicks. Like if he wants to Insert a phrase in the middle of the sentence.

  6. #6
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: List Box

    Have a look at some of the textbox methods specificly SelStart, SelLength and SelText
    Also have a look at the Mid$() Right$() and Left$() functions

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2012
    Posts
    209

    Re: List Box

    I've been researching and can't find anything. Mind pointing me in the right direction or teaching me?

  8. #8
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: List Box

    It's all covered in the online help for VB

  9. #9
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    Re: List Box

    Quote Originally Posted by johndmingione View Post
    I was wondering how to do it where the person clicks. Like if he wants to Insert a phrase in the middle of the sentence.
    Your first request is someone double clicking a listbox in Form A, and the text being inserted into a Textbox in Form B

    You last question (quoted above) is asking that it be inserted where the person clicks.
    How can the user be double clicking a listbox in Form A, and AT THE SAME TIME, be clicking somewhere in the middle of the textbox in Form B ?

    Also I could be wrong, but are some bits of information (questions) being trickled out over a period of time, that could have been specified all at the beginning ?

  10. #10
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,272

    Re: List Box

    Quote Originally Posted by Bobbles View Post
    Also I could be wrong, but are some bits of information (questions) being trickled out over a period of time, that could have been specified all at the beginning ?

    Nah, standard vbforums spec-creep....

  11. #11
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: List Box

    add 1 timer (Timer1)
    add 1 textbox (Text1, set multiline = true)

    add this code to Form code
    Code:
    Private Declare Function GetCursorPos Lib "user32" _
    (lpPoint As POINTAPI) As Long
    
    Private Type POINTAPI
        X As Long
        Y As Long
    End Type
    
    Dim Mouse As POINTAPI
    Dim MoveLabel As Boolean
    Dim ListText As TextBox
    
    Private WithEvents List1 As ListBox
    
    
    Private Sub Form_Load()
    Set ListText = Me.Controls.Add("VB.Textbox", "ListText")
       With ListText
          .ZOrder 0
          .Move 0, 0, 100, 200
          .BackColor = &HC0C0FF
          .BorderStyle = 0
          .Visible = False
          .Alignment = 2
       End With
    
    Set List1 = Me.Controls.Add("VB.Listbox", "List1")
       With List1
          .Move 0, 0, 2000, 2000
          .Visible = True
          .AddItem "his keys"
          .AddItem "his phone"
          .AddItem "ice cream"
          .AddItem "a car"
          .AddItem "something"
          .AddItem "a word"
       End With
       
    
    With Timer1
       .Interval = 10
       .Enabled = False
    End With
    
    With Text1
       .Move 0, 2500, 4000, 4000
       .Visible = True
       .Text = "Jake lost " & vbNewLine & _
               "Sophie wants " & vbNewLine & _
               "I will add '' here"
    End With
    End Sub
    
    Private Sub List1_DblClick()
       With ListText
          .Width = (100 * Len(List1.Text))
          .Text = List1.Text
          .Visible = True
       End With
    
    MoveLabel = True
    Timer1.Enabled = True
    End Sub
    
    Private Sub Text1_Click()
    If MoveLabel Then
       Text1.SelText = ListText.Text
       ListText.Visible = False
       Timer1.Enabled = False
       MoveLabel = False
    End If
    End Sub
    
    Private Sub Timer1_Timer()
    GetCursorPos Mouse
       
       If MoveLabel Then
          ListText.Move (Mouse.X * 15) - Me.Left + 50, (Mouse.Y * 15) - Me.Top - 400
       End If
    End Sub

  12. #12
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: List Box

    Timer interval of 10 is not a good idea. A program that would do nothing but insert text would be hitting the CPU 100 times a second if of course the timer actually worked that fast which it doesn't. In fact I can't think of any need to use a timer here at all.

    Also the question was about having the list on one form and the textbox on another so that code would not do the job anyway.

  13. #13
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: List Box

    This is just an example of what he wants but on same form.. i think you can take the code you need and modify to your needs if your smart enough.. and the timer is only running after double click on listbox.. i dont think the software will stop working because of that amount of time the timer is off... you could put 100 interval if you want

  14. #14
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: List Box

    Quote Originally Posted by johndmingione View Post
    I was wondering how to do it where the person clicks. Like if he wants to Insert a phrase in the middle of the sentence.
    Try this:

    Form1 code:
    vb Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     Form2.Show  '~~~ make sure the second form is als shown
    5. End Sub
    6.  
    7. Private Sub List1_DblClick()    '~~~ when ListBox item is double clicked
    8.     If List1.ListIndex > -1 Then    '~~~ if there's a selected item in ListBox
    9.         Clipboard.SetText List1.List(List1.ListIndex)   '~~~ Copy the selected item to Clipboard
    10.         Form2.Text1.SetFocus        '~~~ set focus back to the TextBox in the second form
    11.         SendKeys "^V"               '~~~ paste it. This will overwrite any selection made in the textbox witht the content from Clipboard
    12.     End If
    13. End Sub

    For testing:
    There should be two forms: Form1 and Form2. Form1 should contain a listbox(List1) with some items in it. Form2 should contain a textbox(Text1) with some text in it.

    Copy-paste the above code in Form1 and test it. If you want to place the text somewhere in the textbox, make sure that you place the cursor at that position or you could make a selection on the text also.
    Then double click the item from the Listbox in Form1.

    Note: Form2 should be loaded and shown before you double click the item in Listbox.


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  15. #15
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: List Box

    Quote Originally Posted by Max187Boucher View Post
    This is just an example of what he wants but on same form.. i think you can take the code you need and modify to your needs if your smart enough.. and the timer is only running after double click on listbox.. i dont think the software will stop working because of that amount of time the timer is off... you could put 100 interval if you want
    I think it is a bad idea to use timers where they are not needed. I think it is a bad idea to ever set an interval to 10 or as I have saw some people say to set it to 1 there is no way this type of operation could be so critical as to need that short an interval and if it is needed then the timer would not suit the purpose as it is not acurate at those speeds anyway.

    Most of all I think it is a bad idea to show this type of example to someone who is just trying to get a handle on how to place text from one form to another or at a specific point in a text box. It would be confusing and they may very well think that a timer is needed for such things and even that 10 is the number you should use.

    The fact is that the goal could be accomplished without the use of the API, without any timers and with less code.

    All that needs to be done is determine which text to use and where to insert based on a mouse click of each control.

  16. #16
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    Re: List Box

    Yes this could achieved very easy with a few lines of code i thought it could give him an idea of what he could do or change (if he wants)
    Can't you see he asked 2 different things people(you) told him the easy way and he still ask to point him in the right direction because he couldnt find anything so i gave another small example that i think makes sense but your right it might be too much for him
    ok timer set it from 100 to 300 millisecond, its still going to do the same thing
    You can make the same function in 100 different ways

    Anyway. if he likes it or not he can tell me, i dont need somebody else that is not asking the question to tell me my code does not work when it does work, i do not see whats wrong with that code except for "maybe" timer interval

  17. #17
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: List Box

    Quote Originally Posted by johndmingione View Post
    I was wondering how to do it where the person clicks. Like if he wants to Insert a phrase in the middle of the sentence.
    Code:
    If List1.ListIndex > -1 Then Form2.Text1.SelText = List1.Text

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    May 2012
    Posts
    209

    Re: List Box

    Dear Bobbles,

    I apologize for being unclear in my first post. Originally I wanted to have them double click an item in the list but I felt it would be easier for me, code-wise, to have them apply it instead. I apologize for not specifying that I was changing my request.

    I didn't go with Max187Bouchers code. I followed it and picked it apart piece by piece but ultimately for what I was looking for it was a giant waste of my time if I was going to change everything and use it. All I needed was one line provided by jcis and akhileshbc.

    Thank you everyone.

  19. #19
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    Re: List Box

    Quote Originally Posted by johndmingione View Post
    Dear Bobbles,

    I apologize for being unclear in my first post. Originally I wanted to have them double click an item in the list but I felt it would be easier for me, code-wise, to have them apply it instead. I apologize for not specifying that I was changing my request.

    I didn't go with Max187Bouchers code. I followed it and picked it apart piece by piece but ultimately for what I was looking for it was a giant waste of my time if I was going to change everything and use it. All I needed was one line provided by jcis and akhileshbc.

    Thank you everyone.
    There is a way to mark a thread as resolved.
    I raise very few questions (can't remember when I last did), so I don't know how to do that.

    Glad you got it solved,
    Rob

  20. #20
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: List Box

    At the top of the first post on the page there is a menu item called Thread Tools, the option to mark as resolved lives in that little drop down menu.

  21. #21

    Thread Starter
    Addicted Member
    Join Date
    May 2012
    Posts
    209

    Re: [RESOLVED] List Box

    Forgot to do it yesterday. Thank you for the reminder. Cheers everyone

Tags for this Thread

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