Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / October 2007

Tip: Looking for answers? Try searching our database.

How to use Y/N values with winforms checkbox

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
GG - 09 Oct 2007 20:25 GMT
Is there a way to use Y/N values instead of true/false values?

Thanks
Chris Mullins [MVP - C#] - 09 Oct 2007 20:33 GMT
Sure, but be warned, it's complicated... :)

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

Then, on the CheckChanged Event:
IsInterested = (myCheck.Checked) ? "yes" : "no";

--
Chris Mullins

> Is there a way to use Y/N values instead of true/false values?
>
> Thanks
Hilton - 09 Oct 2007 23:31 GMT
> 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
Chris Mullins [MVP - C#] - 09 Oct 2007 23:43 GMT
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 Mullins

>> Sure, but be warned, it's complicated... :)
>>
[quoted text clipped - 6 lines]
>
> Hilton
Hilton - 10 Oct 2007 20:26 GMT
Chris wrote:
> Well, if we're going to pick nits related to pet peeves, the comparision
> should really be right:
[quoted text clipped - 4 lines]
> 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
Nicholas Paldino [.NET/C# MVP] - 09 Oct 2007 20:35 GMT
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.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Is there a way to use Y/N values instead of true/false values?
>
> Thanks
>
> *** Sent via Developersdex http://www.developersdex.com ***
Bob Powell [MVP] - 09 Oct 2007 21:42 GMT
If the underlying data isn't a datatable one could use a TypeConverter
and the TypeConverterAttribute to good effect.

Signature

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.

>     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
[quoted text clipped - 6 lines]
>     You could also create a new DataGridViewTextBoxColumn which writes the
> appropriate text, but that seems like way too much work.
Mark Dykun - 17 Oct 2007 20:36 GMT
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

> Is there a way to use Y/N values instead of true/false values?
>
> Thanks
>
> *** Sent via Developersdex http://www.developersdex.com ***

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.