473,387 Members | 1,897 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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

Nov 7 '06 #1
5 2830
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...

Nov 7 '06 #2
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[i]);
}
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.
On Tue, 07 Nov 2006 11:24:07 +0100, CCLeasing <ga**@ccleasing.co.ukwrote:
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...


--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 7 '06 #3
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

Nov 7 '06 #4

I have no edit facilty please find repost with corrected example
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 ; "sometext for the text box here"
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
Nov 7 '06 #5
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[i], text));
}
It will be easier to come up with code ideas if you have a clearer idea of
what you are trying to achieve.

On Tue, 07 Nov 2006 12:57:54 +0100, CCLeasing <ga**@ccleasing.co.ukwrote:
>
I have no edit facilty please find repost with corrected example
>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 ; "sometext for the text box here"
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


--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 7 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Megan | last post by:
Hi- I'm creating a database of music bands with their cds and songs. I'm trying to program an SQL statement so that I can enter a string of text in a textbox, press the 'Enter' key, and have...
3
by: ssb | last post by:
Hello, This may be very elementary, but, need help because I am new to access programming. (1) Say, I have a column EMPLOYEE_NAME. How do I fetch (maybe, cursor ?) the values one by one and...
6
by: Chris Leuty | last post by:
I am populating a multiselect Listbox from a dataset, with the content of the listbox filled by one table, and the selections determined from another table. So far, I have been keeping the dataset...
7
by: Ohad Asor | last post by:
Hello all, I have an ASP.NET page I've written using VS.NET2003, which have a ListBox in it. When I press a button in the form, I try to get the selected item in the list by calling...
0
by: David J | last post by:
Hi, I am strugling with the propertygrid and a listbox. I am using the universaldropdowneditor from the codeproject (code below). However I am populating the listbox via a datasource. The problem...
2
by: frank | last post by:
Hello All, I know this is a re-post, but I didn't get any bites on my last post. So here goes again: I have a listbox which is bound to a datatable. I have set the displaymember and valuemember...
0
by: CCLeasing | last post by:
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...
2
by: glbdev | last post by:
Hi. I am trying to loop thru a listbox and retreive the value for all selected items. Example of listbox items: <option value="1">Item 1</option> <option value="2">Item 2</option> <option...
2
by: NvrBst | last post by:
I populate a ListBox with a LogFile that has about (~1000 lines). The ListBox's datasource is a BindingList<string>. Whenever I add the elements, with the datasource set, it takes about 2 mins. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.