FindByValue equivalent for forms

  • Thread starter Thread starter Xlar54
  • Start date Start date
X

Xlar54

Im trying to come up with a means to simulate the web version of
FindByValue. given:

private void SelectValueInComboBox(System.Windows.Forms.ComboBox cbo,
int id)
{
}

How would I do the equivalent to the web version:

cboText.FindByValue(12).Selected = true;

(not sure about exact syntax, but thats what Im trying to get. Im
using the DataValueMember and DataTextMember propertied to bind)
 
Hi Scott,

You may take a look at the ListControl.SelectedValue property.
When you set a value to this property it will look up in the value member
of the items and select the item whose value member matches the given value.
If no item mathes the SelectedValue will be resetted to -1.

Does it resolve your problem?

Good luck!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
Actually what I normally do is set the datavaluemember to an id, such
as 12, and the datatextmember to the code description as in "USA".
This is how we do it on the web and then use FindByValue(id).Selected
= true to set the combobox to the value in the database (effectively
setting the combobox to "USA"). This is what Im trying to duplicate
for windows forms. If you have any other thoughts, I appreciate your
input.
 
Hi Scott,

Do you mean you would like to set the selected item to "USA" in the
ComboBox when you look up for id (12) in the value member?

If so, I think my understanding is correct, you may set the value you want
to select into ComboBox.SelectedValue property, if the value exists in the
value member column the match record will be selected as current record, so
that the text in the ComboBox will be changed accordingly.

Here is a simple sample for demonstrate this behavior.
You may create a new Winform application and insert the code snippet below
into the Form1's code, then Call InitTest method in Form1's constructor
(after InitializeComponent).

When you input a different value (1,2,3) in the Textbox and click "GoTo"
button, the SelectedItem in the ComboBox will be changed to Name value
accordingly.

Is this the behavior you are looking for?
Please feel free to reply this thread to let me know if this is not your
expected behavior.

Thanks!

<code>
ComboBox cmb = new ComboBox();
TextBox tb = new TextBox();
Button btn = new Button();

private void InitTest()
{

//Init some test data
ArrayList al = new ArrayList();
al.Add ( new MyObject ( 1, "aaa" ) );
al.Add ( new MyObject ( 2, "bbb" ) );
al.Add ( new MyObject ( 3, "ccc" ) );

//Set DataBinding
cmb.DataSource = al;
cmb.DisplayMember = "Name";
cmb.ValueMember = "ID";
cmb.Location = new Point ( 10, 10 );


//Setup a TextBox to show the Selected Value
tb.Location = new Point(cmb.Location.X + cmb.Width + 10, 10);
tb.Size = new Size ( 50, 10);

//setup a Button to set values into SelectedValue
btn.Text = "GoTo";
btn.Location = new Point( tb.Location.X + tb.Width + 10, 10 );
btn.Size = new Size ( 50, cmb.Height );
btn.Click +=new EventHandler(btn_Click);

this.Controls.Add ( cmb );
this.Controls.Add ( tb );
this.Controls.Add ( btn );
this.Size = new Size ( 275, 100 );
}

private void btn_Click(object sender, EventArgs e)
{
cmb.SelectedValue = Int32.Parse (tb.Text);
}

//Test Data Object
class MyObject
{
public MyObject( int id, string name)
{
_name = name;
_id = id;
}
public String Name
{
get { return _name;}
set { _name = value;}
}string _name ;

public int ID
{
get { return _id;}
set { _id = value;}
}int _id;
}
</code>


Best regards,

Ying-Shen Yu [MSFT]
Microsoft Community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
Back
Top