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?
Printable View
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?
of course
Code:Form2.Text1.Text=Form1.List1.Text
Is there a way to add to the text box instead of changing the entire text of the textbox?
Code:Form2.Text1.Text=form2.Text1.Text & Form1.List1.Text
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.
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
I've been researching and can't find anything. Mind pointing me in the right direction or teaching me?
It's all covered in the online help for VB
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 ?
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
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.
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
Try this:
Form1 code:
vb Code:
Option Explicit Private Sub Form_Load() Form2.Show '~~~ make sure the second form is als shown End Sub Private Sub List1_DblClick() '~~~ when ListBox item is double clicked If List1.ListIndex > -1 Then '~~~ if there's a selected item in ListBox Clipboard.SetText List1.List(List1.ListIndex) '~~~ Copy the selected item to Clipboard Form2.Text1.SetFocus '~~~ set focus back to the TextBox in the second form SendKeys "^V" '~~~ paste it. This will overwrite any selection made in the textbox witht the content from Clipboard End If 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.
:wave:
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.
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
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.
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.
Forgot to do it yesterday. Thank you for the reminder. Cheers everyone :)