473,568 Members | 2,762 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2846
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.ReadAllLin es 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.ReadAllLin es(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.Select edIndexChanged 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.BeginsWi th("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(st ring 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(it ems[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
6981
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 it return the associated records to a listbox. Once the listbox has the records, I want to select a record, which will open a form associated with...
3
3147
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 populate a combo box with these names. (this way, I can display all the EMPLOYEE_NAME values) (2) In general, can I do additional processing on...
6
2863
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 a denormalized mirror of the database, but I'm not having much luck getting the selection logic down (I haven't found a 'hook' where I can access...
7
11170
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 ListBox.SelectedItem. And it returns null, even when I select an item. Even when I try to read the value when I get the SelectedIndexChanged event, it still...
0
2637
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 I am having is that when I have a value in the propertygird and edit that, I want the listbox to have the selectvalue equal to the value that is...
2
5251
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 and all works well: the user dbl clicks an item in the list box, I use the valuemember to retrieve some values from the db. However, I would like a...
0
1905
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 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...
2
1203
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 value="3">Item 3</option> <option value="4">Item 4</option>
2
3821
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. I've tried wrapping "Listbox.SuspendLayout()" and "Listbox.ResumeLayout()" around the for loop (that does the adding) but it still takes about 2...
0
7693
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8117
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5498
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
932
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.