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 / January 2007

Tip: Looking for answers? Try searching our database.

Textbox bound to custom property shows 0 when value is nothing

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
segecko@gmail.com - 05 Jan 2007 13:46 GMT
Hi

I have this rather simple problem. I have created a class which
implements INotifyPropertyChanged. This class has a public property (of
type long) which is bound to a standard textbox using a BindingSource
object as broker. This works fine, except when the underlying value is
Nothing, the textbox displays a 0. In this case I wan't the textbox to
be empty, ie. not showing 0. I would prefer not to put code directly on
the form to clear the textbox (fi. in the textbox_change event), since
this would ruin one of the ideas of databinding.
Suggetions please...

Regards
...Seth Gecko
RobinS - 05 Jan 2007 22:05 GMT
What if you make the property nullable? You didn't
specify a language, so here's some VB2005.

Private _myData As Nullable(Of Long)
Public Property myData() As Nullable(Of Long)

When you reference it in code, you have to refer to it this way:

If myData.HasValue Then
 myData += 10
End If

And you can set it to Nothing:  myData = Nothing.

Maybe this would work.

Robin S.
---------------------------------

> Hi
>
[quoted text clipped - 12 lines]
> Regards
> ...Seth Gecko
segecko@gmail.com - 08 Jan 2007 08:00 GMT
Hi again

No, this doesn't work. The textbox still displays a 0 instead of an
empty string.
Would it perhaps be possible to create a new textbox which overrides
the function who actually sets textbox.text = property.value?
Or is it possible to bind different get and set to one textbox? Meaning
bind a set property of type long and a get property of type text?

I cannot believe that this, rather simple problem, will require a very
advanced solution... Databinding is otherwise sooo easy (at least at
the surface).

Am I the only one who needs this kind of thing?

> What if you make the property nullable? You didn't
> specify a language, so here's some VB2005.
[quoted text clipped - 31 lines]
> > Regards
> > ...Seth Gecko
RobinS - 08 Jan 2007 19:49 GMT
Try this:

myTextBox.DataBindings.Add("Text", myBindingSource, _
 "fieldName", True, DataSourceUpdateMode.Validate, "", "#")

The second-to-last argument is what you want displayed
if the value is DBNull.

Last is the format string for the displayed data.
If you set this to #, if the value is 0, it should
display "".

I got this from Brian Noyes' DataBinding book.

Robin S.
----------------------------------------

> Hi again
>
[quoted text clipped - 52 lines]
>> > Regards
>> > ...Seth Gecko
Seth Gecko - 26 Jan 2007 17:28 GMT
Hi

This works like a charm. Thank you.
How about if I want to display the 0 (when the value has it), but ""
when the value is fi. -999?
Basicly I want to user to be able to enter 0 and this should be
displayed. I init my properties to a default of -999 and if it is this
value, then the textbox should be empty, ie. "". Can this be done using
the formatstring argument? I haven't been able to find much info on
this parameter in MSDN.

Otherwise, thank you.

> Try this:
>
[quoted text clipped - 73 lines]
> >> > Regards
> >> > ...Seth Gecko- Skjul tekst i anførselstegn -- Vis tekst i anførselstegn -
RobinS - 26 Jan 2007 20:48 GMT
If you want to go to those lengths, you can use the Format
and Parse methods of the bound control. Try this:

in Form_Load:

   'specifically bind the textbox for your field
   'you have to do this, because you need the name of
   '  the databinding in order to capture the
   '  format and parse events
   Dim myTextBoxBinding As Binding = _
     New Binding("Text", myBindingSource, "myField", True)

   'add event handlers for the format and parse events
   AddHandler myTextBoxBinding.Format, AddressOf OnMyFieldFormat
   AddHandler myTextBoxBinding.Parse, AddressOf OnMyFieldParse

   myTextBox.DataBindings.Add(myTextBoxBinding)

Private Sub OnMyFieldFormat(ByVal sender As Object, _
 ByVal e As ConvertEventArgs)
   'ConvertEventArgs contains the [Value] that is to
   '  be used for setting the bound control property
   '  unless you decide to change it.

   If e.Value Is Nothing OrElse e.Value.Equals(DBNull.Value) _
     OrElse e.Value = -999 Then
       'Show the textbox with nothing in it.
       e.Value = String.Empty
   End If

End Sub

Private Sub OnMyFieldParse(ByVal sender As Object, _
 ByVal e As ConvertEventArgs)
   'For the parse event, the Value property of the event argument
   '  contains the value of the bound control property. When
   '  the parsing process is complete, the value set on the [Value]
   '  property will be used to set the content of the bound column
   '  in the data source.

   If stringValue = String.Empty Then
       e.Value = -999
   End If
End Sub

This is *also* from Brian Noyes' Data Binding book.

Robin S.
----------------------------------------------------------
Hi

This works like a charm. Thank you.
How about if I want to display the 0 (when the value has it), but ""
when the value is fi. -999?
Basicly I want to user to be able to enter 0 and this should be
displayed. I init my properties to a default of -999 and if it is this
value, then the textbox should be empty, ie. "". Can this be done using
the formatstring argument? I haven't been able to find much info on
this parameter in MSDN.

Otherwise, thank you.

> Try this:
>
[quoted text clipped - 81 lines]
> >> > ...Seth Gecko- Skjul tekst i anførselstegn -- Vis tekst i
> >> > anførselstegn -

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.