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 / Windows Forms / WinForm Data Binding / October 2006

Tip: Looking for answers? Try searching our database.

Binding a textbox to a nullable datatype.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
markk@nait.ca - 22 Oct 2006 18:41 GMT
I am wondering if there is a better solution, than the one I came up
with...

I am binding a textbox to a nullable float (float?) and want the
ability for the user to leave the textbox blank, indicating that it is
null. If you do not alter the behavior of the textbox, the user, when
attempting to leave the textbox would not be allowed because the empty
string is not a valid value of the binding property (float?).

In order to get around this I overloaded the OnValidating eventhandler.

      protected override void OnValidating(CancelEventArgs e)
       {
           if (this.Text == "" && this.BindingContext != null)
           {
               e.Cancel = false;
               base.Text = null;

               object o = ((this.DataBindings[0].DataSource as
BindingSource)).DataSource;
               PropertyInfo pi =
o.GetType().GetProperty(this.DataBindings[0].BindingMemberInfo.BindingMember);
               pi.SetValue(o, null, null);

           }
           else
               base.OnValidating(e);
       }

Is there a better way?
Bart Mermuys - 22 Oct 2006 20:49 GMT
Hi,

>I am wondering if there is a better solution, than the one I came up
> with...
[quoted text clipped - 26 lines]
>
> Is there a better way?

There are other ways, you decide what you like most:

1)  Since it looks like you are inheriting the TextBox, you could override
Text instead, eg. :

public override string Text
{
  get
 {
    return ((base.Text=="") && !DesignMode)? null : base.Text;
  }
  set
  {
     base.Text = value;
  }
}

2) Instead of inheriting from TextBox, you could attach an eventhandler to
the DataBinding Parse event, eg. :

// if you have setup the bindings using the designer then attach the
eventhandler
// after InitializeComponent
textBox1.DataBindings.Add("Text", yourBindingSource, "yourField");
textBox1.DataBindings["Text"].Parse += new ConvertEventHandler(
StringToFloatHandler );

private void StringToFloatHandler (object sender, ConvertEventArgs e)
{
   string str = e.Value as string;
   e.Value = (str=="")? (float?)null : float.Parse(str);
}

HTH,
Greetings
AnikSol - 26 Oct 2006 14:45 GMT
Hi Bart,

Any chance that you could post your suggestions 1 and 2 in VB?

thanks n regards

> Hi,
>
[quoted text clipped - 64 lines]
> HTH,
> Greetings
Bart Mermuys - 26 Oct 2006 21:24 GMT
Hi,

> Hi Bart,
>
> Any chance that you could post your suggestions 1 and 2 in VB?

I'm not very familar with nullable types in VB, but it here it goes:

1)
Public Overrides Property Text() As String
 Get
   Return IIf((MyBase.Text = "") And Not DesignMode, Nothing, MyBase.Text)
 End Get
 Set(ByVal value As String)
   MyBase.Text = value
 End Set
End Property

2)

AddHandler TextBox1.DataBindings("Text").Parse, AddressOf
StringToFloatHandler

Private Sub StringToFloatHandler( ByVal sender As Object, ByVal e As
ConvertEventArgs )

 Dim str As String = CStr( e.Value )
 e.Value = IIf(str = "", Nothing, float.Parse(str) )

End Sub

HTH,
Greetings

> thanks n regards
>
[quoted text clipped - 66 lines]
>> HTH,
>> Greetings
AnikSol - 27 Oct 2006 03:04 GMT
Bart

Thanks.

> Hi,
>
[quoted text clipped - 100 lines]
>>> HTH,
>>> Greetings

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.