By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 941 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

ComboBox Default values

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.