Hi,
I know exactly what you mean re the options available. Oddly enough it's the low level language capablities that I sometimes don't consider (e.g. operator overloading and implicit type conversions). Given:
public struct YN
{
bool b;
public static implicit operator string(YN x)
{
return x.b ? "Y" : "N";
}
public static implicit operator YN(string s)
{
YN x = new YN();
x.b = s == "Y";
return x;
}
public static implicit operator bool(YN x)
{
return x.b;
}
public static implicit operator YN(bool b)
{
YN x = new YN();
x.b = b;
return x;
}
public static implicit operator int(YN x)
{
return x.b ? 1 : 0;
}
public static implicit operator YN(int i)
{
YN x = new YN();
x.b = i != 0;
return x;
}
}
you could use some weird and wonderful syntax. E.g
YN test = true;
string s = test;
if (test)
textBox1.Text = test;
YN test2 = "Y";
bool b = test2;
test2 = 0;
s = test2;
But probably not a good idea!
Best,
Viv
>Thanks for the idea! I haven't looked at the format and parse event handlers in a while (I used them previously only to format decimals) so I totally neglected those as options. Those are now my 5th way of doing this :o) I'm playing around with it. I actually like this option better so far....
>
>I love the many options .net has, but I must admit, it gets frustrating at the same time that in .net, the options don't immediately come to mind yet. That may take a while. Of course, I used VFP since the early 80s so of course different ways of accomplishing would immediately come to my mind for it...
>
>
>Tracy
>
>
>>Hi,
>>
>>Did you look at using the Binding Parse and Format events to handle the conversion ?
>>If you're using the Binding class that should be a much cleaner implementation.
>>Regards,
>>Viv
>>
>>>I was overlooking the obvious that day. :o) The problem was with our default classes in the other layers. I had to create a special base textbox to control the saving. We have base classes but the base textbox classes already created only passed 'TRUE' and 'FALSE' based on a value of 'TRUE' or 'FALSE.' I had to create a new special basetextbox class to look for 'Y' and 'N,' but pass 'TRUE' or 'FALSE' to the business layer and then on to the other layers respectively...
>>>
>>>
>>>I was testing about 4 different ways of accomplishing the same thing. One way was using the passwordchr and setting maxlength to 1, another was the example below, another used a bool value and converted it to string before sending it to the business layer, and the other was a different way of doing what is done below.
>>>
>>>Fun, fun, fun! :o)
>>>
>>>
>>>>
>DISREGARD: Resolved. :)>>>>
>>>>So, how did you end up solving this Tracy? I'm sure others will benefit from your experience. =0)
>>>>
>>>>~~Bonnie
>>>>
>>>>
>>>>
>>>>
>>>>>
>>>>>First, a checkbox is a LAST RESORT. I am trying to emulate the foxpro toggling of values in .net
>>>>>
>>>>>Ok, I have a textbox control that is used for a bit field in SQL Server (1 or 0) TRUE or FALSE. By default, the value is TRUE or FALSE. I want it to toggle between Y and N. I have that working using the keypress event. I am also changing the value in the _TextChanged() so that when data is first loaded, it displays Y instead of TRUE and N instead of FALSE. Both of those work fine.
>>>>>
>>>>>Now, my problem is in saving the value. Obviously it raises an exception. How do I save TRUE instead of Y and FALSE instead of N? Where is the best place to put that code? For this form, values are saved when the control is exited, not using a save button.
>>>>>
>>>>>Also, is the way I have done it so far ok? Is there a better way? Should the code below be some place else? I'm am not comfortable yet with which event runs when and where I would put code...
>>>>>
>>>>>
>>>>> private void txbisactive_KeyPress(object sender, KeyPressEventArgs e)
>>>>> {
>>>>> if (e.KeyChar == (char)32)
>>>>> {
>>>>> if (((TextBox)sender).Text == "TRUE")
>>>>> {
>>>>> ((TextBox)sender).Text = "N";
>>>>> }
>>>>> else
>>>>> {
>>>>> if (((TextBox)sender).Text == "FALSE")
>>>>> {
>>>>> ((TextBox)sender).Text = "Y";
>>>>> }
>>>>> else
>>>>> {
>>>>> if (((TextBox)sender).Text == "Y")
>>>>> {
>>>>> ((TextBox)sender).Text = "N";
>>>>> }
>>>>> else
>>>>> {
>>>>> ((TextBox)sender).Text = "Y";
>>>>> }
>>>>> }
>>>>> }
>>>>> }
>>>>> else if (e.KeyChar == 'N' || e.KeyChar == 'n')
>>>>> {
>>>>> ((TextBox)sender).Text = "N";
>>>>> }
>>>>> else if (e.KeyChar == 'Y' || e.KeyChar == 'y')
>>>>> {
>>>>> ((TextBox)sender).Text = "Y";
>>>>> }
>>>>> e.Handled = true;
>>>>> }
>>>>>
>>>>> private void txbisactive_TextChanged(object sender, EventArgs e)
>>>>> {
>>>>> if (((TextBox)sender).Text == "TRUE")
>>>>> {
>>>>> ((TextBox)sender).Text = "Y";
>>>>> }
>>>>> else
>>>>> {
>>>>> if (((TextBox)sender).Text == "FALSE")
>>>>> {
>>>>> ((TextBox)sender).Text = "N";
>>>>> }
>>>>> }
>>>>> }
>>>>>