473,412 Members | 3,015 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,412 software developers and data experts.

ComboBox Default values

I want to set the Default value of a Combobox for any changeable record and have got this working but it is totaly unsatisfactory
see the code below
I loop through the items in the Combo looking for a match between cVal and the selectedValue then stop when I do have a match
the obvious problem is that each iteration fires the selectedIndexChanged but also for a large list this will slow everything down.

There must be a better way

int cVal=4;
for (int i=0;i<this.cmbCo.Items.Count+1;i++)
{
cmbCo.SelectedIndex=i;
if (int.Parse(cmbCo.SelectedValue.ToString())==cVal
{
cmbCo.SelectedIndex=i;
break;
}
}
Nov 16 '05 #1
7 23512
"NCrum" <NC***@discussions.microsoft.com> wrote:
[looking for a value in a combo box]


Do you really need the int.Parse(), or can you put your number into a
canonical format and then search for that? In the latter case, you can
use combo.FindString or combo.FindStringExact.

P.
Nov 16 '05 #2
Thanks for your reply
I am not sure either of us understands the other
I have Table "A" with a list of "CompayNames" and an Id No "CoId"
also
I have a Table "B" with a list of "Acitivites" this is related to "CompanyNames" by the id no "Coid"
When I change the activity in my winform I want the drop down to reflect this change and display the "CompanyName" using this "Activity" <<<

each company may have many activities but one activity can only have one company

Ta, N

"Paul E Collins" wrote:
"NCrum" <NC***@discussions.microsoft.com> wrote:
[looking for a value in a combo box]


Do you really need the int.Parse(), or can you put your number into a
canonical format and then search for that? In the latter case, you can
use combo.FindString or combo.FindStringExact.

P.

Nov 16 '05 #3
I'm sure there is a better way as well. But I need more information. How
are your dropdowns setup? I ask this because this control has the Display
value and a value that you can associate with this Display value.

I assume you have a Company dropdown that has CoID as the Value and the
CompanyName as the Display value. But what is in the Activity dropdown? Is
the Activity name the Display value and the CoID associated with this
activity as the Value item?

If this is the case then you should be able to use the SelectedValue
property on the Company dropdown to the SelectedValue property of the
Activity dropdown. Again assuming you have setup your dropdowns
appropriately. There would be no need to cycle through each item in the
Company dropdown. Be aware that when you set the Company dropdown the
SelectedIndexChanged event is going to fire for this dropdown.

If this doesn't help then describe what is in each dropdown as far as value
members and display members and I might be able to help a little more. I'm
having to assume a lot here because I don't know how you're populating your
dropdowns.

--
C Addison Ritchie, MCSD.NET
Ritch Consulting, Inc.

"NCrum" <NC***@discussions.microsoft.com> wrote in message
news:F2**********************************@microsof t.com...
Thanks for your reply
I am not sure either of us understands the other
I have Table "A" with a list of "CompayNames" and an Id No "CoId"
also
I have a Table "B" with a list of "Acitivites" this is related to "CompanyNames" by the id no "Coid"
When I change the activity in my winform I want the drop down to
reflect this change and display the "CompanyName" using this "Activity" <<<
each company may have many activities but one activity can only have one company
Ta, N

"Paul E Collins" wrote:
"NCrum" <NC***@discussions.microsoft.com> wrote:
[looking for a value in a combo box]


Do you really need the int.Parse(), or can you put your number into a
canonical format and then search for that? In the latter case, you can
use combo.FindString or combo.FindStringExact.

P.

Nov 16 '05 #4
Thanks

You are correct for the Company but the Activity box has the Activity has the Activity name for display and its Value is Actid
they were both populated by a reader into an Arraylist which is bound to the Combobox, does that help?
"C Addison Ritchie" wrote:
I'm sure there is a better way as well. But I need more information. How
are your dropdowns setup? I ask this because this control has the Display
value and a value that you can associate with this Display value.

I assume you have a Company dropdown that has CoID as the Value and the
CompanyName as the Display value. But what is in the Activity dropdown? Is
the Activity name the Display value and the CoID associated with this
activity as the Value item?

If this is the case then you should be able to use the SelectedValue
property on the Company dropdown to the SelectedValue property of the
Activity dropdown. Again assuming you have setup your dropdowns
appropriately. There would be no need to cycle through each item in the
Company dropdown. Be aware that when you set the Company dropdown the
SelectedIndexChanged event is going to fire for this dropdown.

If this doesn't help then describe what is in each dropdown as far as value
members and display members and I might be able to help a little more. I'm
having to assume a lot here because I don't know how you're populating your
dropdowns.

--
C Addison Ritchie, MCSD.NET
Ritch Consulting, Inc.

"NCrum" <NC***@discussions.microsoft.com> wrote in message
news:F2**********************************@microsof t.com...
Thanks for your reply
I am not sure either of us understands the other
I have Table "A" with a list of "CompayNames" and an Id No "CoId"
also
I have a Table "B" with a list of "Acitivites" this is related to

"CompanyNames" by the id no "Coid"
>>When I change the activity in my winform I want the drop down to

reflect this change and display the "CompanyName" using this "Activity" <<<

each company may have many activities but one activity can only have one

company

Ta, N

"Paul E Collins" wrote:
"NCrum" <NC***@discussions.microsoft.com> wrote:

> [looking for a value in a combo box]

Do you really need the int.Parse(), or can you put your number into a
canonical format and then search for that? In the latter case, you can
use combo.FindString or combo.FindStringExact.

P.


Nov 16 '05 #5
What I have done in situations like yours is to create a special combobox
item class. In your case you could try something like this:

Create a class for holding the activity information:
public class Activity
{
private int activityID;
private string activityName;
private int companyID;

public Activity(int activityID, string activityName, int companyID)
{
this.activityID = activityID;
// other assignments here
}

// get accessors here for activityID, actvityName, companyID
public int CompanyID
{
get { return companyID; }
}

// important part
// override the ToString method so that the combobox will know what to
display
public override string ToString()
{
return activityName;
}
}

Once this class is defined use your DataReader to construct instances of
this class. These instances of Activity are then added to the comboBox
using the comboBox1.Items.Add(myCurrentActivityInstance). The comboBox will
use the ToString() method of your class to determine what to display to the
user. You can ditch the ArrayList middleman here because you are creating
Activity instances and adding them directly to the combobox. There is no
binding used with the Activity combobox with this solution.

Now how to use all this. First for the Company dropdown I would data bind
to a table for loading this combo box. Like the following:

// code to fill your dataset with company info, namely company id and
company name
// then
companyCombo.DisplayMember = "CompanyName";
companyCombo.ValueMember = "CompanyID";
companyCombo.DataSource = myDataSet.Tables[0]; // bind to the company
info table

Now in the SelectedIndexChanged event of the Activity combobox you can do
this:
Activity activity = (Activity)activityCombo.SelectedItem();
companyCombo.SelectedValue = activity.CompanyID;

This should work for you. This is what I love about .NET and the new
controls. You no longer have to use the Tag property crap work arounds to
store extra information for a list item. You can create your own list item
class and simply add instances of it to the list. To get the information
back out just type cast the selected item to the appropriate class and all
your extra information is available to you.

Like everything in .NET there are many ways to accomplish something like
this. You may be able to use some sort of binding for the Activity combobox
but I have not look into any of that. Maybe someone could suggest a
possibly more robust way that uses binding all around.

Questions welcomed.

HTH

--
C Addison Ritchie, MCSD.NET
Ritch Consulting, Inc.

"NCrum" <NC***@discussions.microsoft.com> wrote in message
news:29**********************************@microsof t.com...
Thanks

You are correct for the Company but the Activity box has the Activity has the Activity name for display and its Value is Actid they were both populated by a reader into an Arraylist which is bound to the Combobox, does that help?

"C Addison Ritchie" wrote:
I'm sure there is a better way as well. But I need more information. How
are your dropdowns setup? I ask this because this control has the Display value and a value that you can associate with this Display value.

I assume you have a Company dropdown that has CoID as the Value and the
CompanyName as the Display value. But what is in the Activity dropdown? Is the Activity name the Display value and the CoID associated with this
activity as the Value item?

If this is the case then you should be able to use the SelectedValue
property on the Company dropdown to the SelectedValue property of the
Activity dropdown. Again assuming you have setup your dropdowns
appropriately. There would be no need to cycle through each item in the
Company dropdown. Be aware that when you set the Company dropdown the
SelectedIndexChanged event is going to fire for this dropdown.

If this doesn't help then describe what is in each dropdown as far as value members and display members and I might be able to help a little more. I'm having to assume a lot here because I don't know how you're populating your dropdowns.

--
C Addison Ritchie, MCSD.NET
Ritch Consulting, Inc.

"NCrum" <NC***@discussions.microsoft.com> wrote in message
news:F2**********************************@microsof t.com...
Thanks for your reply
I am not sure either of us understands the other
I have Table "A" with a list of "CompayNames" and an Id No "CoId"
also
I have a Table "B" with a list of "Acitivites" this is related to

"CompanyNames" by the id no "Coid"

>>>When I change the activity in my winform I want the drop down to

reflect this change and display the "CompanyName" using this "Activity" <<<

each company may have many activities but one activity can only have one company

Ta, N

"Paul E Collins" wrote:

> "NCrum" <NC***@discussions.microsoft.com> wrote:
>
> > [looking for a value in a combo box]
>
> Do you really need the int.Parse(), or can you put your number into

a > canonical format and then search for that? In the latter case, you can > use combo.FindString or combo.FindStringExact.
>
> P.
>
>
>


Nov 16 '05 #6
Thanks

I will give it a go later

"C Addison Ritchie" wrote:
What I have done in situations like yours is to create a special combobox
item class. In your case you could try something like this:

Create a class for holding the activity information:
public class Activity
{
private int activityID;
private string activityName;
private int companyID;

public Activity(int activityID, string activityName, int companyID)
{
this.activityID = activityID;
// other assignments here
}

// get accessors here for activityID, actvityName, companyID
public int CompanyID
{
get { return companyID; }
}

// important part
// override the ToString method so that the combobox will know what to
display
public override string ToString()
{
return activityName;
}
}

Once this class is defined use your DataReader to construct instances of
this class. These instances of Activity are then added to the comboBox
using the comboBox1.Items.Add(myCurrentActivityInstance). The comboBox will
use the ToString() method of your class to determine what to display to the
user. You can ditch the ArrayList middleman here because you are creating
Activity instances and adding them directly to the combobox. There is no
binding used with the Activity combobox with this solution.

Now how to use all this. First for the Company dropdown I would data bind
to a table for loading this combo box. Like the following:

// code to fill your dataset with company info, namely company id and
company name
// then
companyCombo.DisplayMember = "CompanyName";
companyCombo.ValueMember = "CompanyID";
companyCombo.DataSource = myDataSet.Tables[0]; // bind to the company
info table

Now in the SelectedIndexChanged event of the Activity combobox you can do
this:
Activity activity = (Activity)activityCombo.SelectedItem();
companyCombo.SelectedValue = activity.CompanyID;

This should work for you. This is what I love about .NET and the new
controls. You no longer have to use the Tag property crap work arounds to
store extra information for a list item. You can create your own list item
class and simply add instances of it to the list. To get the information
back out just type cast the selected item to the appropriate class and all
your extra information is available to you.

Like everything in .NET there are many ways to accomplish something like
this. You may be able to use some sort of binding for the Activity combobox
but I have not look into any of that. Maybe someone could suggest a
possibly more robust way that uses binding all around.

Questions welcomed.

HTH

--
C Addison Ritchie, MCSD.NET
Ritch Consulting, Inc.

"NCrum" <NC***@discussions.microsoft.com> wrote in message
news:29**********************************@microsof t.com...
Thanks

You are correct for the Company but the Activity box has the Activity has

the Activity name for display and its Value is Actid
they were both populated by a reader into an Arraylist which is bound to

the Combobox, does that help?


"C Addison Ritchie" wrote:
I'm sure there is a better way as well. But I need more information. How are your dropdowns setup? I ask this because this control has the Display value and a value that you can associate with this Display value.

I assume you have a Company dropdown that has CoID as the Value and the
CompanyName as the Display value. But what is in the Activity dropdown? Is the Activity name the Display value and the CoID associated with this
activity as the Value item?

If this is the case then you should be able to use the SelectedValue
property on the Company dropdown to the SelectedValue property of the
Activity dropdown. Again assuming you have setup your dropdowns
appropriately. There would be no need to cycle through each item in the
Company dropdown. Be aware that when you set the Company dropdown the
SelectedIndexChanged event is going to fire for this dropdown.

If this doesn't help then describe what is in each dropdown as far as value members and display members and I might be able to help a little more. I'm having to assume a lot here because I don't know how you're populating your dropdowns.

--
C Addison Ritchie, MCSD.NET
Ritch Consulting, Inc.

"NCrum" <NC***@discussions.microsoft.com> wrote in message
news:F2**********************************@microsof t.com...
> Thanks for your reply
> I am not sure either of us understands the other
> I have Table "A" with a list of "CompayNames" and an Id No "CoId"
> also
> I have a Table "B" with a list of "Acitivites" this is related to
"CompanyNames" by the id no "Coid"
>
> >>>When I change the activity in my winform I want the drop down to
reflect this change and display the "CompanyName" using this "Activity" <<< >
> each company may have many activities but one activity can only have one company
>
> Ta, N
>
> "Paul E Collins" wrote:
>
> > "NCrum" <NC***@discussions.microsoft.com> wrote:
> >
> > > [looking for a value in a combo box]
> >
> > Do you really need the int.Parse(), or can you put your number into a > > canonical format and then search for that? In the latter case, you can > > use combo.FindString or combo.FindStringExact.
> >
> > P.
> >
> >
> >


Nov 16 '05 #7
I was looking over the code I posted... this wasn't pasted from the actual
code so there may be errors. One I've already noticed is that SelectedItem
is a property so it doesn't need the () of course. :-)

There may be others... but the concept should be sound.

--
C Addison Ritchie, MCSD.NET
Ritch Consulting, Inc.

"NCrum" <NC***@discussions.microsoft.com> wrote in message
news:4F**********************************@microsof t.com...
Thanks

I will give it a go later

"C Addison Ritchie" wrote:
What I have done in situations like yours is to create a special combobox item class. In your case you could try something like this:

Create a class for holding the activity information:
public class Activity
{
private int activityID;
private string activityName;
private int companyID;

public Activity(int activityID, string activityName, int companyID)
{
this.activityID = activityID;
// other assignments here
}

// get accessors here for activityID, actvityName, companyID
public int CompanyID
{
get { return companyID; }
}

// important part
// override the ToString method so that the combobox will know what to display
public override string ToString()
{
return activityName;
}
}

Once this class is defined use your DataReader to construct instances of
this class. These instances of Activity are then added to the comboBox
using the comboBox1.Items.Add(myCurrentActivityInstance). The comboBox will use the ToString() method of your class to determine what to display to the user. You can ditch the ArrayList middleman here because you are creating Activity instances and adding them directly to the combobox. There is no binding used with the Activity combobox with this solution.

Now how to use all this. First for the Company dropdown I would data bind to a table for loading this combo box. Like the following:

// code to fill your dataset with company info, namely company id and company name
// then
companyCombo.DisplayMember = "CompanyName";
companyCombo.ValueMember = "CompanyID";
companyCombo.DataSource = myDataSet.Tables[0]; // bind to the company info table

Now in the SelectedIndexChanged event of the Activity combobox you can do this:
Activity activity = (Activity)activityCombo.SelectedItem();
companyCombo.SelectedValue = activity.CompanyID;

This should work for you. This is what I love about .NET and the new
controls. You no longer have to use the Tag property crap work arounds to store extra information for a list item. You can create your own list item class and simply add instances of it to the list. To get the information back out just type cast the selected item to the appropriate class and all your extra information is available to you.

Like everything in .NET there are many ways to accomplish something like
this. You may be able to use some sort of binding for the Activity combobox but I have not look into any of that. Maybe someone could suggest a
possibly more robust way that uses binding all around.

Questions welcomed.

HTH

--
C Addison Ritchie, MCSD.NET
Ritch Consulting, Inc.

"NCrum" <NC***@discussions.microsoft.com> wrote in message
news:29**********************************@microsof t.com...
Thanks

You are correct for the Company but the Activity box has the Activity has
the Activity name for display and its Value is Actid
they were both populated by a reader into an Arraylist which is bound
to the Combobox, does that help?


"C Addison Ritchie" wrote:

> I'm sure there is a better way as well. But I need more
information. How
> are your dropdowns setup? I ask this because this control has the

Display
> value and a value that you can associate with this Display value.
>
> I assume you have a Company dropdown that has CoID as the Value and
the > CompanyName as the Display value. But what is in the Activity dropdown? Is
> the Activity name the Display value and the CoID associated with
this > activity as the Value item?
>
> If this is the case then you should be able to use the SelectedValue
> property on the Company dropdown to the SelectedValue property of the > Activity dropdown. Again assuming you have setup your dropdowns
> appropriately. There would be no need to cycle through each item in the > Company dropdown. Be aware that when you set the Company dropdown the > SelectedIndexChanged event is going to fire for this dropdown.
>
> If this doesn't help then describe what is in each dropdown as far as value
> members and display members and I might be able to help a little
more. I'm
> having to assume a lot here because I don't know how you're
populating your
> dropdowns.
>
> --
> C Addison Ritchie, MCSD.NET
> Ritch Consulting, Inc.
>
> "NCrum" <NC***@discussions.microsoft.com> wrote in message
> news:F2**********************************@microsof t.com...
> > Thanks for your reply
> > I am not sure either of us understands the other
> > I have Table "A" with a list of "CompayNames" and an Id No "CoId"
> > also
> > I have a Table "B" with a list of "Acitivites" this is related to
> "CompanyNames" by the id no "Coid"
> >
> > >>>When I change the activity in my winform I want the drop down
to > reflect this change and display the "CompanyName" using this

"Activity" <<<
> >
> > each company may have many activities but one activity can only
have one
> company
> >
> > Ta, N
> >
> > "Paul E Collins" wrote:
> >
> > > "NCrum" <NC***@discussions.microsoft.com> wrote:
> > >
> > > > [looking for a value in a combo box]
> > >
> > > Do you really need the int.Parse(), or can you put your number
into a
> > > canonical format and then search for that? In the latter case,
you can
> > > use combo.FindString or combo.FindStringExact.
> > >
> > > P.
> > >
> > >
> > >
>
>
>


Nov 16 '05 #8

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

Similar topics

7
by: Malcolm Cook | last post by:
Hi, I've created and installed a custom UDF to populate my combobox, and have defined it per :...
3
by: Tamir Khason | last post by:
Why this does not work??? cb1 - combobox DataProvider.DataProvider.DS_BringRacks() - DataSet cb1.DataSource=DataProvider.DataProvider.DS_BringRacks().Tables; cb1.DisplayMember = "id"; ...
2
by: ross kerr | last post by:
Hi all, I have a control that extends the ComboBox object. It updates the selected item based on what the user enters in the text area. In the OnLeave event of the combobox, the selected...
7
by: charliewest | last post by:
Using .Net CF, i have created a 2 dimension ArrayList, and "binded" this list to a ComboBox control using the "DataSource" property. I have set the DisplaySource and ValueMember properties as well....
5
by: James P. | last post by:
Hello, For my new Windows application, all I want is to create an initial form to demo to the user to show them how it looks like with some data on it. So I figure the fastest way is to create...
1
by: genojoe | last post by:
I want to right-align numbers in a combobox. Since there is no TextAlign property for the ComboBox, I tried the following code in a form containing a default combobox. 'Declarations and two...
30
by: dbuchanan | last post by:
ComboBox databindng Problem == How the ComboBox is setup and used: My comboBox is populated by a lookup table. The ValueMember is the lookup table's Id and the DisplayMember is the text from a...
2
by: AMDRIT | last post by:
Hello Everyone, I would like to format the Display Members of a combobox's datasource. Is there a way to apply a format without subclassing the original datasource? For example, given a list of...
0
by: peter78 | last post by:
I wanted to implement an autocomplete feature on the combobox where you would type in partial text and it would try to match it for you. It doesn't exist in .Net yet, but I'm guessing it will in...
8
by: | last post by:
I am sure this has been asked and answered, but here goes anyway... VS.Net 2005, VB.Net How can you display more than one field in the displaymember property of a combobox inside the...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...
0
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...

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.