help - populating a combox box from a text file and updating a listbox automatically

C

CCLeasing

Hello, I have searched google but can not find a straight forward
answer to my problem. Hopefuly someone will be kind enough to offer
their expertise. Please forgive if this seems a bit convoluted but this
is probabally a reflection of my not clear understanding of the
mechanics behind what i'm trying to achieve. Please bear with it I
really could do with your help.

I have a simple windows form. There are two controls on the form that I
am struggling with. One is a combo box and one is a mutli line text
box.

What I would like to.
------------------------------
I would like to populate the combo box control from a text file.
The data that I would like to populate the combo box with is
hierarchical. It consists of units and subinits of data. I would like
to store the data in a text file in the following kind of format: -

1 Gold ; 1.2 Golden Blue ; 1.3 Golden Green
2 Red
3 Purple ; 3.1 Purple Grey ; 3.2 Purple yellow

When the combo box is clicked i would like the drop down box to display
something like this .

1. Gold
1.2 Golden Blue
1.3 Golden Green
2. Red
3. Purple
3.1 Purple Grey
3.2 Purple Yellow

When a selection is made in the combo box I would like the textbox on
the form to update itself with the data that matches the selection in
the combobox. Many options from the combo box would share the same
associated data for the textbox control. So my thought was that maybe I
could add another bit of data to my text file to indicate what textbox
data is linked with the combobox item, maybe like this: -

1 (a) Gold ; 1.2 (a) Golden Blue ; 1.3 (a) Gold Green
2 (a) Red
3 (c) Purple ; (b) 3.1 Purple Grey ; (e) 3.2 Purple yellow

So basically what I want to do is: -

a. know how to populate the combo box with data from a text file in
roughly the format i've proposed.
b. know how to check for the bracketed character associated with the
combo box selection when a selection is made.
c. populate the textbox control with data from another text file when a
selection on the combo box is made. Depending on the bracketed value
picked up from step c. The text file for the textbox control would look
something like this : -

a. some text here
b. some text here
c. some text here

I really appreciate your help, if you might be able to offer advice
please do.

Thankyou
 
C

CCLeasing

n.b the data i've supplied is a sample data, the actual data is
different in nature it is to do with some contact manager called act,
but it's format is identicial. Just in case the colour's led anyone to
beleive i was working with pixels or colours etc...
 
M

Morten Wennevik

Hi,

It is not entire clear to me what you are trying to do, but a few pointers
are to use ReadLine or File.ReadAllLines on the file and then
String.Split(';') per line to get any sub items. To simulate indentation
you can just add a space or several to any items in position 1 and above
in the split string[].

[C# 2.0]
string[] lines = File.ReadAllLines(path);

foreach(string s in lines)
{
string[] items = s.Split(';');
comboBox1.Items.Add(items[0]);

if(items.Length > 1)
for(int i = 1; i < items.Length; i++)
comboBox.Items.Add(" " + items);
}


As for the updating of the TextBox, you can bind it to the ComboBox.Text
field or do something in ComboBox.SelectedIndexChanged event or similar,
but I'm not sure what you actually want to do here.
 
C

CCLeasing

Thankyou very much that's exactly what i wanted.

Now i just need to get part two the updating of the textbox working.

Can you give me an example of some code that would find the line that
starts with the value of the combo box, and then store the text of that
line that occurs after a semicolon to a string, and set this string as
the value of the textbox control.

e.g.


combo box value: AAAA

on update check the text file textbox.txt for a line begininning with
AAAA e.g.

CCCC; some other text
AAAA ;
BBBB ; some other text

and then set the textbox controls text equal to "sometext for the text
box here" because that is the text that is after the semicolon on the
AAAA line.

Thankyou
 
M

Morten Wennevik

Ok,

The item in the ComboBox have some text that should be displayed in the
TextBox. This text is taken from another file and can be shared among
several items in the ComboBox.

How to find the text in the 'text'-file depends on how it is formatted.
You could compare with combobox, value, using a similar method of
ReadAllLines and search with String.BeginsWith("AAAA"), or do a string[]
items = String.Split(';') and compare the comboBox value with items[0]..
If the text is limited, you could also put it in the same file as the
ComboBox values and store the text along with the display value. You can
create a separate class for this

public class ComboBoxItem
{
private string display = "";
private string value = "";

public string Display
{
get{ return display; }
}

public string Value
{
get{ return value; }
}

public ComboBoxItem(string display, string value)
{
this.display = display;
this.value = value;
}
}

The ComboBox should then be filled like this


foreach(string s in lines)
{
string text = ...
string[] items = s.Split(';');
comboBox1.Items.Add(new ComboBoxItem(items[0], text));

if(items.Length > 1)
for(int i = 1; i < items.Length; i++)
comboBox.Items.Add(new ComboBoxItem(" " + items, text));
}


It will be easier to come up with code ideas if you have a clearer idea of
what you are trying to achieve.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top