How to use Y/N values with winforms checkbox

  • Thread starter Thread starter GG
  • Start date Start date
Sure, but be warned, it's complicated... :)

myCheck.Checked = (IsInterested.equals("yes")) ? true : false;

Then, on the CheckChanged Event:
IsInterested = (myCheck.Checked) ? "yes" : "no";
 
I'm assuming you want to do this in a data grid? Is the underlying data
source a DataTable? If it is then I would create a column on the DataTable
which has an Expression property set to:

iif(%column%, 'Y', 'N')

Then, bind to that column.

You could also create a new DataGridViewTextBoxColumn which writes the
appropriate text, but that seems like way too much work.
 
If the underlying data isn't a datatable one could use a TypeConverter
and the TypeConverterAttribute to good effect.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Chris Mullins said:
Sure, but be warned, it's complicated... :)

myCheck.Checked = (IsInterested.equals("yes")) ? true : false;

myCheck.Checked = IsInterested.equals("yes");

Sorry, just a pet peeve of the "if (x) y=true; else; y=false;" variant
(instead of just "y=x").

Hilton
 
Well, if we're going to pick nits related to pet peeves, the comparision
should really be right:

myCheck.Checked = IsInterested.Equals("yes",
StringComparison.OrginalIgnoreCase);

Ya never know when some dev down the line will use "Yes" instead of "yes".
(I know, this is a strange pet peeve, but....)

We could keep going, and be really silly...

public Struct YesNo
{
private bool _yesOrNo;
...
public bool IsYes{ get { ... } set {...}}
public bool IsNo{ get { ... } set{...}}
}
 
Chris said:
Well, if we're going to pick nits related to pet peeves, the comparision
should really be right:

myCheck.Checked = IsInterested.Equals("yes",
StringComparison.OrginalIgnoreCase);

Ya never know when some dev down the line will use "Yes" instead of "yes".
(I know, this is a strange pet peeve, but....)

I totally agree with you. I use string.Compare (x, y, true) since CF 1.1
does not have the method you used. I wonder which is faster on .NET
(non-CF).

Hilton
 
Here is a derived class that includes properties for setting valid yes/no
values and setting/getting the checked text.

public class CheckboxEx: CheckBox
{
private string _YesValue = "Y";
private string _NoValue = "N";

public string YesValue
{
get { return _YesValue; }
set
{
_YesValue = value;
}
}

public string NoValue
{
get { return _NoValue; }
set
{
_NoValue = value;
}
}

public string CheckedText
{
get
{
return this.Checked ? _YesValue: _NoValue ;
}
set
{
this.Checked = string.Compare(value,_YesValue,true)==0;
}
}


}

Kind Regards,
Mark Dykun, VP Mobile and Integration services, Castle CRM
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top