I've run into a problem with binding form properties to data in an xml
file (through a dataset). As it turns out, I can easily bind if the
data type of the property on the control is a normal datatype such as
a string or integer. If the datatype is not one of the built-in types
however, things go horribly wrong. Consider this xml file:

<?xml version="1.0" standalone="yes"?>
<SettingsData>
<one>
<Text>Center</Text>
</one>
</SettingsData>

If I create a normal winforms application and add the following code
to the form's constructor, it works. I get "Center" in the text box.
Here's the code that works:

DataSet dataset;
dataset = new DataSet();
dataset.ReadXml(@"d:\development\bindtest\form.xml");
TextBox textbox = new TextBox();
Binding binding = new Binding("Text", dataset, "one.Text");
textbox.DataBindings.Add(binding);
textbox.Width = 150;
textbox.Height = 50;
textbox.Top = 50;
textbox.Left = 50;
textbox.Parent = this;
this.Controls.Add(textbox);

I hardcoded the size and placement of the text box, but I can easily
get that from the xml as well. I encountered a problem when I tried
to pull TextAlign from the xml file though. I know that this would
require the use of Format, so I implemented that. I changed the xml
from above to look like this:

<?xml version="1.0" standalone="yes"?>
<SettingsData>
<one>
<TextAlign>Center</TextAlign>
</one>
</SettingsData>


And I changed the code from above to look like this (changed from Text
to TextAlign, and added the convert event handler):


DataSet dataset;
dataset = new DataSet();
dataset.ReadXml(@"d:\development\bindtest\form.xml");
TextBox textbox = new TextBox();
Binding binding = new Binding("TextAlign", dataset, "one.TextAlign");
binding.Format += new ConvertEventHandler(FormatHorizontalAlignment);
textbox.DataBindings.Add(binding);
textbox.Width = 150;
textbox.Height = 50;
textbox.Top = 50;
textbox.Left = 50;
textbox.Parent = this;
this.Controls.Add(textbox);


I also added the Convert Event Handler:


public void FormatHorizontalAlignment(object sender, ConvertEventArgs
cevent)
{
switch((string)cevent.Value)
{
case "Center":
cevent.Value = HorizontalAlignment.Center;
break;
case "Left":
cevent.Value = HorizontalAlignment.Left;
break;
case "Right":
cevent.Value = HorizontalAlignment.Right;
break;
}
}


This event handler looks right to me. I followed the example from the
help files. I've made similar code work with other data types such as
booleans being coverted from a T or F in the xml file. DesiredType
shows HorizontalAlignment.

When I run this code, oddly, the event handler is executed three
times. Each time seems to execute correctly. Then I get an unhandled
exception from the Application.Run(new Form1()); Error creating
window handle.

Can anyone tell me what's happening and how to fix it? I foresee I'll
have the same problem trying to read the font name and size from the
xml file. I have a workaround, but it's very unelegant.


Here's the stack trace:

System.ComponentModel.Win32Exception: Error creating window handle.
at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
at System.Windows.Forms.Control.CreateHandle()
at System.Windows.Forms.TextBoxBase.CreateHandle()
at System.Windows.Forms.Control.CreateControl(Boolean
fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl(Boolean
fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr
hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef
hWnd, Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at bindtest.Form1.Main() in d:\development\bindtest\form1.cs:line
109



tia, brian