Results 1 to 12 of 12

Thread: Display Listbox item's description in a Textbox [Resolved]

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2004
    Posts
    29

    Display Listbox item's description in a Textbox [Resolved]

    Hi all, I'm new to VB and this is my question:

    I have a list box where the user select an item from the list box, and the description of the item will be display in a text box.
    Different item will display different description in the same text box. How can I do this?

    I've been trying by using this code:
    VB Code:
    1. Private Sub List1_Click()
    2.     Select Case List1.ListIndex
    3.        Case 0:
    4.            Text1.Text = "A Description"
    5.        Case 1:
    6.            Text1.Text = "B Description"
    7.        Case 2:
    8.            Text1.Text = "C Description"    
    9.     End Select
    10. End Sub

    But the description is very long (more than hundreds words), I tried to store it in an access database, but failed because text size too large. Is it possible to save the description in a .txt file and load the description from that .txt file into the text box?

    Thanks in advance!
    Last edited by hbin; Aug 25th, 2004 at 10:59 AM.

  2. #2
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877
    What did you define the field in Access? Text? If you define it as Memo then you should not have any problem with the text size.
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Aug 2004
    Posts
    29
    Yeah, I previously defined the field as Text, now the text size problem solved after I defined it as Memo, thanks

    I'm still having the problem to display the description of an item from a Listbox in the same Textbox. Since I stored the description in an access database, how do I let the user to randomly access to certain record (description) by selecting from the Listbox?

    Thanks in advance! I hope I didn't confuse anyone over here :P

  4. #4
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    You could always use a text file if you wanted. All you would have to do is seperate each "description" by a special character...
    VB Code:
    1. Dim desc() As String
    2.  
    3. Private Sub Form_Load()
    4. Dim FF As Long
    5. Dim myStr As String
    6. FF = FreeFile()
    7. Open "C:\test.txt" For Input As #FF
    8. myStr = Input(LOF(FF), FF)
    9. desc = Split(myStr, "{")
    10. Close #FF
    11. End Sub
    12.  
    13. Private Sub List1_Click()
    14. Text1.Text = Replace(desc(List1.ListIndex), vbNewLine, "")
    15. End Sub
    Code:
    //The TextFile
    This is the description of the very first item in the list (ListIndex 0).
    {
    This is the second description of the second item (ListIndex 1).
    {
    blah blah blah..
    {
    Phreak
    Last edited by «°°phReAk°°»; Aug 22nd, 2004 at 04:24 AM.

    Visual Studio 6, Visual Studio.NET 2005, MASM

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Aug 2004
    Posts
    29

    Display Listbox item's description in a Textbox

    Thanks Phreak, it works~! But the text shown up all in one paragraph even though it's originally divided in paragraphs in the text file. How can I make the text separated into paragraphs?

    By the way, I changed the code

    VB Code:
    1. Open "C:\test.txt" For Input As #FF
    to
    VB Code:
    1. Open "test.txt" For Input As #FF
    in order the program to locate the file in the same folder as the project, and it works. Will this code still works when I converted my project into .exe file?

  6. #6
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246
    AH, whoops, lol. This part:
    VB Code:
    1. Text1.Text = Replace(desc(List1.ListIndex), vbNewLine, "")
    ..removes the new lines. Which in turn, makes it all 1 line.
    You can remove the "Replace" and just make it this:
    VB Code:
    1. Text1.Text = desc(List1.ListIndex)
    I had the "Replace" because I didn't set the MutliLine property to true, and it showed squares for the Newline characters..

    Phreak

    Visual Studio 6, Visual Studio.NET 2005, MASM

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Aug 2004
    Posts
    29
    Yup, it now can separate into paragraphs.

    BTW, I don't know why before this code can works but now it couldn't works
    VB Code:
    1. Open "test.txt" For Input As #FF
    How should I write the code to make the program to locate the file in the same folder as the project, even when converted to .exe file?

    PS: Can a .txt file store a large amount of text?

  8. #8
    PowerPoster
    Join Date
    Jul 2001
    Location
    Tucson, AZ
    Posts
    2,166
    Suggest you do some reading on handling "sequential files"

    FF is a supposed to be a file number. As I recall it is defined as an integer, and/or long. FF is a hex value so MOST likely if you want to use hex, you need to convert it to an integer or long
    before using it as a file number. NEVER TRIED HEX AS A FILE NUMBER SO BEST GUESS RESPONSE.

    ---------------
    How should I write the code to make the program to locate the file in the same folder as the project, even when converted to .exe file?
    ----------------
    Put the folder/directory in front of your file name ----> "test.txt"


    -----------
    PS: Can a .txt file store a large amount of text?
    -----------

    Up to the size of the space available on your hard disk or
    as I recall 4GB, whichever is smaller.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Aug 2004
    Posts
    29
    Put the folder/directory in front of your file name ----> "test.txt"
    Yup, I did that before, eg:
    VB Code:
    1. C:\My Documents\test.txt
    , but I wanted to make the program automatically locate this text file as long as it's in the same folder as the project; so the user can place the project & file in any folder or directory.

    BTW, I was trying to let the user to print the description in the text box and it seems like can't print the text correctly and completly.

    Here is what I wrote:
    VB Code:
    1. Private Sub cmdPrint_Click()
    2.     'Print the Description
    3.     Printer.Font.Name = "Times New Roman"
    4.     Printer.Font.Size = 11
    5.     Printer.Print  Text1.Text
    6.     Printer.EndDoc
    7. End Sub

  10. #10
    Fanatic Member vbasicgirl's Avatar
    Join Date
    Jan 2004
    Location
    Manchester, UK
    Posts
    1,016
    if you want to find the path your project is in then use
    VB Code:
    1. Open App.Path & "\test.txt" For Input As #1
    is this what you are talking about ?

    casey.

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Aug 2004
    Posts
    29
    is this what you are talking about ?
    VB Code:
    1. Open App.Path & "\test.txt" For Input As #1
    casey.
    Yup, exactly Thanks.

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Aug 2004
    Posts
    29

    Printting description of the Textbox

    VB Code:
    1. Private Sub cmdPrint_Click()
    2.     'Print the Description
    3.     Printer.Font.Name = "Times New Roman"
    4.     Printer.Font.Size = 11
    5.     Printer.Print  Text1.Text
    6.     Printer.EndDoc
    7. End Sub
    I wrote these code to print the description in the text box and the output is like it can't print the text as what it shown in the text box or text file. Certain lines in a paragraph were skipped until a suppose to be long document (description) printed as short/uncompleted. Well, I'm not sure if it really "skipped" the lines in a paragraph or it can't wrap the lines to fit the page.

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