Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Could someone assist me in coding a simple button to load txt to combo box?

vb.net combobox text file selection

This topic has been archived. This means that you cannot reply to this topic.
13 replies to this topic

#1 codegirl

codegirl

    CC Regular

  • Member
  • PipPipPip
  • 36 posts

Posted 26 March 2013 - 10:14 PM

Hello Everyone,

I've been trying to figure this out but can't find the right answer in google search.

 

I would like to load a text file by clicking on a button then the content of the txt file populate a combobox then once a selection is selected it will show as text (label).

Please see attach screenshot.

 

Alot of these samples I find are file location and no selection of the files it self.

Any help would be appreciated.

 

Thanks in advance :)

Attached Thumbnails

  • ggsdfgdf.jpg


#2 Ritwik I the programmer

Ritwik I the programmer

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 244 posts

Posted 26 March 2013 - 11:58 PM

I suppose you basically want to read the lines of a text file and use them as options in an awt Choice object? Then you should use the BufferedReader, FileInputStream and InputStreamReader classes, part of java.io:

 

BuffferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("[path\]<textfilename>.txt")));

 

Use the readLine() function of BufferedReader object to read lines from the file using a while loop, and add them as choices to the choice list, represented by the class Choice(part of java.awt).

 

You also need to know about ActionListener to respond to a button click, and about ItemListener to respond to selection of a choice in a choice list, and about WindowAdapter to close a window.

The complete code is attached below:

Attached File  ChoiceFrame.txt   1.85KB   206 downloads

 

P.S.:And I need to know a lot more about Layout Managers.


Edited by Ritwik I the programmer, 27 March 2013 - 12:08 AM.

I can believe, but why should I?


#3 codegirl

codegirl

    CC Regular

  • Member
  • PipPipPip
  • 36 posts

Posted 27 March 2013 - 12:27 AM

Hey Addict,

 Is this C# or C++ ? I believe it's not vb.net . Thanks though!



#4 zeroGiven

zeroGiven

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 61 posts

Posted 27 March 2013 - 04:11 AM

Use the StreamReader object to readline() of the text file, each readline will need to be added into the combobox. (of course it appears, you will want to skip the first readline where it reads Select). The streamreader is part of the system.io namespace.

 

Once you've populated the combobox, use the comboboxes selectionChanged event to set the label to the .selecteditem or .Text of the combobox.

 

if you google "site: MSDN StreamReader" there should be an example on reading in line by line. Just remember, every time .ReadLine() is executed the index moves to the next row in the stream.


Edited by zeroGiven, 27 March 2013 - 04:12 AM.


#5 Ritwik I the programmer

Ritwik I the programmer

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 244 posts

Posted 27 March 2013 - 04:12 AM

Silly mistake. It's java. I was feeling sleepy.


I can believe, but why should I?


#6 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 27 March 2013 - 01:26 PM

If we are talking VB.NET I go with Streamreader, open the file (with the constructor, like = new Streamredear( "your file name");

 

And the use the ReadToEnd() method to load the text fully into a string variable.

 

If you want an one liner for the combobox it would be like combobox.items = sr.readToEnd().Split( Environment.Newline) which will give you an string array, splitted by the enter, cool, isn't it ? :D



#7 codegirl

codegirl

    CC Regular

  • Member
  • PipPipPip
  • 36 posts

Posted 27 March 2013 - 10:46 PM

If we are talking VB.NET I go with Streamreader, open the file (with the constructor, like = new Streamredear( "your file name");

 

And the use the ReadToEnd() method to load the text fully into a string variable.

 

If you want an one liner for the combobox it would be like combobox.items = sr.readToEnd().Split( Environment.Newline) which will give you an string array, splitted by the enter, cool, isn't it ? :D

Hi blackrabbit,

 Was trying to search in youtube on tutorials but can't find the the exact one's with streamredear from upload txt file to combobox then to label if a selection in combobox is made.

I tried this

but his is that I have to type in the file name. I want something to have a popup window to select my file manually the load into combobox.

I know this sounds lazy but can someone give me an example?

 

Thanks



#8 codegirl

codegirl

    CC Regular

  • Member
  • PipPipPip
  • 36 posts

Posted 28 March 2013 - 12:02 AM

I tried his sample at: http://pastebin.com/WxRtb4x7

and replace my combobox name from hist textbox1 and nothing happened only errors. Now I'm stuck. Could someone correct me on this?



#9 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 28 March 2013 - 12:08 AM

Worry not CG, BlackRabbit has your back.

 

You need:

- One button

- One comboBox (in the example named cbList)

- Imports System.IO

- A text file ( in the example d:combo.txt ) with a couple of lines in it, each line being one item to put in the combo.

 

and this code (in the button click)

 

 
        Dim sr As StreamReader = New StreamReader("d:\combo.txt")

        For Each item As String In sr.ReadToEnd().Split(Environment.NewLine)

            cbList.Items.Add(item)

        Next
 

 

It tooks more to write the intro than the code :P

but basically that gets your file, reads it, splits it by enters and insert each line into your combobox, few lines, easier impossible.



#10 codegirl

codegirl

    CC Regular

  • Member
  • PipPipPip
  • 36 posts

Posted 28 March 2013 - 12:18 AM

Worry not CG, BlackRabbit has your back.

 

You need:

- One button

- One comboBox (in the example named cbList)

- Imports System.IO

- A text file ( in the example d:combo.txt ) with a couple of lines in it, each line being one item to put in the combo.

 

and this code (in the button click)

 

 

 
        Dim sr As StreamReader = New StreamReader("d:\combo.txt")

        For Each item As String In sr.ReadToEnd().Split(Environment.NewLine)

            cbList.Items.Add(item)

        Next
 

 

It tooks more to write the intro than the code :P

but basically that gets your file, reads it, splits it by enters and insert each line into your combobox, few lines, easier impossible.

Hi BlackRabbit,

 Thanks for I am looking for the steps I had mentioned. By click on a button then select a txt file, then the lines in the txt file will show in the combobox, then after I select something it will show as a text file next to the combobox which is (lable). This is shown on the screenshot.

Sorry to be a pain but thanks though.

Hmmmm.



#11 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 28 March 2013 - 12:20 AM

well, you put the code I gave you after you select the file, and replace the file name with the selected in the open file dialog, the rest, it makes what ou want ;)



#12 codegirl

codegirl

    CC Regular

  • Member
  • PipPipPip
  • 36 posts

Posted 29 March 2013 - 01:05 AM

Hi

 This is the complete code and screenshot. What it does is each time I click on the button it loads the content of the txt file without a prompt to choose my txt file. Also the label1 is not showing what I choose.

What do you think I should add or change?

 

Public Class Form1


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim sr As StreamReader = New StreamReader("C:\Users\vb\Desktop\test.txt")

        For Each item As String In sr.ReadToEnd().Split(Environment.NewLine)

            ComboBox1.Items.Add(item)

        Next
    End Sub
End Class

 

 

Attached Thumbnails

  • bbbb.jpg





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download