sturdy

Hi,

I need a checkbox that toggles on a char(1) field (Y/N)instead of a bit field. I can't seem to make the standard checkbox toggle unless bound to a bit field in the database. Since I can't change the database, is there a way to reconfigure or otherwise modify the existing checkbox Is a custom checkbox required and if so, how I'm still a newbie so need help here. Thanks in advance.

Sturdy



Re: Windows Forms General Char-toggled checkbox?

Bob zhu - MSFT

Hi , I am not quite sure your requiement , and if you want char 'Y' and 'N' I think you can do as follow

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace CS2

{

public partial class Form5 : Form

{

public Form5()

{

InitializeComponent();

}

private void Form5_Load(object sender, EventArgs e)

{

this.textBox2.TextChanged += new EventHandler(textBox2_TextChanged);

}

void textBox2_TextChanged(object sender, EventArgs e)

{

if (textBox2.Text.Trim().ToUpper() == "Y")

{ this.checkBox1.Checked = true; }

else if (textBox2.Text.Trim().ToUpper() == "N")

{ this.checkBox1.Checked = false; }

else { return; }

}

}

}






Re: Windows Forms General Char-toggled checkbox?

Zhi-Xin Ye - MSFT

You do not need to make a custom checkbox, just use a binding object, handle its Format event. I write a sample for your information:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace Sample4

{

public partial class CheckBoxBinding : Form

{

public CheckBoxBinding()

{

InitializeComponent();

}

private void CheckBoxBinding_Load(object sender, EventArgs e)

{

DataTable dt = new DataTable();

dt.Columns.Add("flag", typeof(char));

for (int j = 0; j < 10; j++)

{

char c = (j % 2 == 0) 'N' : 'Y';

dt.Rows.Add(c);

}

BindingSource bs = new BindingSource();

bs.DataSource = dt;

BindingNavigator bindingNavigator1 = new BindingNavigator();

bindingNavigator1.AddStandardItems();

bindingNavigator1.BindingSource = bs;

this.Controls.Add(bindingNavigator1);

Binding binding = new Binding("Checked", bs, "flag");

binding.Format += new ConvertEventHandler(binding_Format);

this.checkBox1.DataBindings.Add(binding);

}

void binding_Format(object sender, ConvertEventArgs e)

{

char c = (char)e.Value;

e.Value = (c == 'Y') true : false;

}

}

}


Hope it helps.
Best Regards.
Zhixin Ye




Re: Windows Forms General Char-toggled checkbox?

sturdy

Bob and Zhixin,

Great...thanks for the most helpful sample code. This is exactly what I need to do and it will help a lot.

Cheers,

Sturdy





Re: Windows Forms General Char-toggled checkbox?

sturdy

Zhixin Ye,

I said I am a newbie ;-) Your sample was a great help. But now...

I added the bindings into the form1_Main_Load block. I can see where this code will bind the checkbox as I wanted but something about the char cast is complaining. I think the problem is related to the SQLServer Express type char field which is ascii. The VC# type char field I discovered is unicode. Is there a conversion requirement here Please see below for the exception detail.

private void form1_Main_Load(object sender, EventArgs e)

{

...... fill table adapters, etc.

Binding binding = new Binding("Checked", bscCustomerTable, "CondDsgReimbCust");

binding.Format += new ConvertEventHandler(binding_Format);

this.chkReimbCust.DataBindings.Add(binding);

}

void binding_Format(object sender, ConvertEventArgs e)

{

char c = (char)e.Value; //looks okay, debug shows value of "e.Value" to be "N"

e.Value = (c == 'Y') true : false;

}

Exception detail: System.InvalidCastException was unhandled by user code. Message="Specified cast is not valid."

Thanks again and best regards,

Sturdy





Re: Windows Forms General Char-toggled checkbox?

timvw

You can always use e.Value.ToString() and then compare with "Y"...





Re: Windows Forms General Char-toggled checkbox?

Zhi-Xin Ye - MSFT

Change the codes in Format event to:

void binding_Format(object sender, ConvertEventArgs e)

{

string s = e.Value.ToString();

e.Value = (s == "Y") true : false;

}


Hope it helps.
Best Regards.
Zhixin Ye




Re: Windows Forms General Char-toggled checkbox?

sturdy

Zhixin Ye and Timvw,

Thanks. That should have been obvious... now I do feel stupid! Friday was a very long day.

Sturdy