Solved

URGENT DATABINDING QUESTION !!!   1000 pts

Posted on 2004-09-15
24
226 Views
Last Modified: 2010-04-23
I have binded a textbox to an object using (textbox1.databindings.add... etc)
     my object throws a custom exception (made by me) in order to tell the user that certain data can't be assigned to a property.
      but the only thing that I'm getting rigth now is that when I enter the "invalid data" the application didn't let me change the focus to another control, but no exception message is displayed and no runtime exception is generated.


public class dog

   private pName as string

    public property Name() as string
     get
         return pname
     end get
      set (byval value as string)
               if value="hello" then
                    dim ex as new exception("value not permitted")
                     throw ex
                else
                    pname=value
                 end if
     end set
end class


now lets supose that I need to bind an instance of this class to a textbox
I do this using the following

dim x as new dog
textbox1.databindings.add ("text",x,"name")

but when i change the value of the textbox in run time, I've got NO EXCEPTION
how can I do to catch an exception that is being ignored.

or maybe I'm doing something wrong.

best regards
Elias.
0
Comment
Question by:eliaslopezgtz
  • 11
  • 7
  • 5
  • +1
24 Comments
 
LVL 5

Expert Comment

by:tzxie2000
ID: 12065006
please try code below

write this in form declarations
    Private newdog As dog = New dog
we must have an instance
as you define it will missing out of the function

write binding as

        Me.TextBox1.DataBindings.Add("text", newdog, "name")

about class:

Public Class dog

    Private pName As String = ""

    Public Property [Name]() As String
        Get
            Return pname
        End Get
        Set(ByVal value As String)
            If value = "hello" Then
                Dim ex As New Exception("value not permitted")
                MessageBox.Show("aa")
                Throw ex
            Else
                pname = value
            End If
        End Set
    End Property
End Class


 pName should assign a value or be nothing
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 12065019
why don't you just do

set (byval value as string)
               if value="hello" then
                     msgbox "Invalid"
                else
                    pname=value
                 end if
     end set
0
 
LVL 5

Expert Comment

by:tzxie2000
ID: 12065105
I am just modify code on the author
yes
the code I did not use exception
and I suggest not use exception as this is an interface to user
throw out exception may make user anxiety
0
 
LVL 5

Expert Comment

by:LindzK
ID: 12066507
Hi,

  could you post your full code - so that we can see exactly how you are binding the data ?

I created a simple from with two text boxes - the binding seems to happen either when I close the form, or when the text box in question looses focus.  You say it didn't let you change the focus - what exactly did you mean by that ? :)

If you have more than one item on your form, you can just tab to it, to loose focus - but If you really cant , then maybe you should initiate something in the textchanged event for the text box - that will either loose the focus from the text box or rebind the textbox to the class again.

The code that binds the text box, should be encompassed wihtin a try / catch statement - that way you can deal with the exception that you throw however you want - if it's been 'caught' you don't have to throw it at the user, you can do something else if you want, or you can forumlate a more friendly user message, and show the user that.

Anyway - below is the code I produced ( bare in mind, the binding only happens when you tab to a different text box )

It uses a try/catch to catch the error you generated.  I'd still like to see your code though, so we can look a little further into where your problem is :)

Public Class Form2
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        Me.TextBox2 = New System.Windows.Forms.TextBox()
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(104, 96)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.TabIndex = 0
        Me.TextBox1.Text = "TextBox1"
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(208, 168)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.TabIndex = 1
        Me.TextBox2.Text = "TextBox2"
        '
        'Form2
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(440, 266)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox2, Me.TextBox1})
        Me.Name = "Form2"
        Me.Text = "Form2"
        Me.ResumeLayout(False)

    End Sub

#End Region




    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim newdog As New dog()
            newdog.Name = "goodbye"
            TextBox1.DataBindings.Add(New Binding("Text", newdog, "Name"))
        Catch ex As Exception
            'perform any code here that you wish because the error
            'was bound
            ' You can either report it to the user
            ' log it somewhere
            ' or do nothing about it
            ' but placeing your main code inside a try/catch
            '
            MsgBox(ex.ToString)
        End Try

    End Sub

   
End Class

Public Class dog

    Private pName As String


    Public Property Name() As String
        Get
            Return pName
        End Get
        Set(ByVal value As String)
            If value = "hello" Then
                Dim NotPermitted As New Exception("This value is not permitted")
                Throw NotPermitted
            Else
                pName = value
            End If
        End Set
    End Property
End Class
0
 

Author Comment

by:eliaslopezgtz
ID: 12068309
Dear  LindzK:

I 've tried your code in a blank new solution but I only get the same... I can't get out from textbox1 while the value is "hello" but no exception message box is displayed please help me.

best regards elias
0
 
LVL 5

Expert Comment

by:tzxie2000
ID: 12068339
how about the code


Public Class Form1
    Inherits System.Windows.Forms.Form
    Private newdog As dog = New dog


#Region " Windows &#31383;&#20307;&#35774;&#35745;&#22120;&#29983;&#25104;&#30340;&#20195;&#30721; "

    Public Sub New()
        MyBase.New()

        '&#35813;&#35843;&#29992;&#26159; Windows &#31383;&#20307;&#35774;&#35745;&#22120;&#25152;&#24517;&#38656;&#30340;&#12290;
        InitializeComponent()

        '&#22312; InitializeComponent() &#35843;&#29992;&#20043;&#21518;&#28155;&#21152;&#20219;&#20309;&#21021;&#22987;&#21270;

    End Sub

    '&#31383;&#20307;&#37325;&#20889; dispose &#20197;&#28165;&#29702;&#32452;&#20214;&#21015;&#34920;&#12290;
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Windows &#31383;&#20307;&#35774;&#35745;&#22120;&#25152;&#24517;&#38656;&#30340;
    Private components As System.ComponentModel.IContainer

    '&#27880;&#24847;: &#20197;&#19979;&#36807;&#31243;&#26159; Windows &#31383;&#20307;&#35774;&#35745;&#22120;&#25152;&#24517;&#38656;&#30340;
    '&#21487;&#20197;&#20351;&#29992; Windows &#31383;&#20307;&#35774;&#35745;&#22120;&#20462;&#25913;&#27492;&#36807;&#31243;&#12290;
    '&#19981;&#35201;&#20351;&#29992;&#20195;&#30721;&#32534;&#36753;&#22120;&#20462;&#25913;&#23427;&#12290;
    Public WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents SqlConnection1 As System.Data.SqlClient.SqlConnection
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(176, 8)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 0
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(104, 224)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.TabIndex = 3
        Me.TextBox1.Text = "TextBox1"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    End Sub


    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.TextBox1.DataBindings.Add("text", newdog, "name")

    End Sub

    Private Sub Button1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Button1.Paint
        Dim g As Graphics = e.Graphics
        Dim f As Font = New Font("Arial", 8)

        g.DrawString("run", f, System.Drawing.Brushes.Blue, 0, 0)
        g.DrawString("stop", f, System.Drawing.Brushes.Red, 0, 8)

    End Sub
End Class

Public Class dog

    Private pName As String = ""

    Public Property [Name]() As String
        Get
            Return pname
        End Get
        Set(ByVal value As String)
            If value = "hello" Then
                MessageBox.Show("value not permitted")
                Throw New GardenException("value not permitted")
            Else
                pname = value
            End If
        End Set
    End Property
End Class


Public Class GardenException
    Inherits System.ApplicationException
    Public Sub New()
    End Sub
    ' Creates a Sub New for the exception that allows you to set the
    ' message property when thrown.
    Public Sub New(ByVal Message As String)
        MyBase.New(Message)
    End Sub
    ' Creates a Sub New that can be used when you also want to include
    ' the inner exception.
    Public Sub New(ByVal Message As String, ByVal Inner As Exception)
        MyBase.New(Message)
    End Sub
End Class
0
 
LVL 5

Expert Comment

by:tzxie2000
ID: 12068368
sorry for different language let the code be fuse

new code paste


Public Class Form1
    Inherits System.Windows.Forms.Form
    Private newdog As dog = New dog


#Region
    Public Sub New()
        MyBase.New()

        InitializeComponent()

    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    Private components As System.ComponentModel.IContainer

    Public WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents SqlConnection1 As System.Data.SqlClient.SqlConnection
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(176, 8)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 0
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(104, 224)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.TabIndex = 3
        Me.TextBox1.Text = "TextBox1"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    End Sub


    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.TextBox1.DataBindings.Add("text", newdog, "name")

    End Sub

    Private Sub Button1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Button1.Paint
        Dim g As Graphics = e.Graphics
        Dim f As Font = New Font("Arial", 8)

        g.DrawString("run", f, System.Drawing.Brushes.Blue, 0, 0)
        g.DrawString("stop", f, System.Drawing.Brushes.Red, 0, 8)

    End Sub
End Class

Public Class dog

    Private pName As String = ""

    Public Property [Name]() As String
        Get
            Return pname
        End Get
        Set(ByVal value As String)
            If value = "hello" Then
                Throw New GardenException("value not permitted")
            Else
                pname = value
            End If
        End Set
    End Property
End Class


Public Class GardenException
    Inherits System.ApplicationException
    Public Sub New()
    End Sub
    ' Creates a Sub New for the exception that allows you to set the
    ' message property when thrown.
    Public Sub New(ByVal Message As String)
        MyBase.New(Message)
    End Sub
    ' Creates a Sub New that can be used when you also want to include
    ' the inner exception.
    Public Sub New(ByVal Message As String, ByVal Inner As Exception)
        MyBase.New(Message)
    End Sub
End Class
0
 

Author Comment

by:eliaslopezgtz
ID: 12069855
Still getting the same... ignored exception,
 I want to catch that exception somewhere in the code and depending on that do some stuff.

0
 
LVL 5

Expert Comment

by:LindzK
ID: 12072770
I don't think it's ignoring the exception - I think it's not binding the data, If you put a debug line inside your class around the point that you throw the exception, you will probably find that it only goes in there - when it's initialy bound ( with the right information ) and when you close the page.

 - You have to think of a point you want the data to bind.  It will bind when the text box looses focus - when you say you can't tab - what exactly do you mean by that ? your whole form wont let you tab around ?  I'm not sure why that's not happening, may be easier if we see your code - can you post your code ? :).

Otherwise, if you place the code that does the binding inside the textchanged event of the text box - that will force it to go through, then it will catch the exception ( providing that you put a try/catch around the databinding, see my code earlier ), you could also have an extra button maybe that does the binding - button name soemthing like 'Change name' ?

It sounds like there's a simple reason the binding may not be happening - put the debug in your code to find out if it's not binding, or just not finding the error - and if you can, post your code here, and we can try to give it a run through to find the problem.

0
 
LVL 5

Expert Comment

by:tzxie2000
ID: 12072791
I meet the same problem as the author
It's realy binding with the dog object
I set a breakpoint and it goes there

I also try to write throw exception in binding.parse and binding.format and the result is the same

the program will have a little slow when enter hello and then lose focus

it seems the controls doing something in controls and skip the exception

I also try to throw NULLReference... Exception it also be the same
0
 
LVL 5

Expert Comment

by:LindzK
ID: 12072798
By posting code, I mean the code that uses your class dog  - not just the class itself )
0
 
LVL 5

Expert Comment

by:LindzK
ID: 12072829
Please post the code - would b easier if i can look at an exact replicar of the problem :)
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:eliaslopezgtz
ID: 12074602
ok first of all I'm using visual studio 2003 enterprise


Public Class Form1
    Inherits System.Windows.Forms.Form
    Private newdog As dog = New dog


#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.TextBox2 = New System.Windows.Forms.TextBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(125, 111)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(120, 22)
        Me.TextBox1.TabIndex = 0
        Me.TextBox1.Text = "TextBox1"
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(250, 194)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.Size = New System.Drawing.Size(120, 22)
        Me.TextBox2.TabIndex = 1
        Me.TextBox2.Text = "TextBox2"
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(328, 72)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 2
        Me.Button1.Text = "Button1"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15)
        Me.ClientSize = New System.Drawing.Size(528, 306)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.TextBox2)
        Me.Controls.Add(Me.TextBox1)
        Me.Name = "Form1"
        Me.Text = "Form2"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
         
            newdog.Name = "goodbye"
            TextBox1.DataBindings.Add("Text", newdog, "Name")
        Catch ex As Exception
            'perform any code here that you wish because the error
            'was bound
            ' You can either report it to the user
            ' log it somewhere
            ' or do nothing about it
            ' but placeing your main code inside a try/catch
            '
            MsgBox(ex.ToString)
        End Try
    End Sub

End Class


Public Class dog

    Private pName As String


    Public Property Name() As String
        Get
            Return pName
        End Get
        Set(ByVal value As String)
            If value = "hello" Then
                Dim NotPermitted As New Exception("This value is not permitted")
                Throw NotPermitted
            Else
                pName = value
            End If
        End Set
    End Property
End Class



Now you run the code....
put hello in the textbox and see what happens...
you can't move away from that textbox until the value is <> "hello"
but no exception thrown in runtime is generated... so you can't catch it

best regards
Elias
0
 

Author Comment

by:eliaslopezgtz
ID: 12074928
I think it has something to be with the reflection process involved in databinding.
because if I set up the IDE to break when any exceptions are thrown... It breaks... with one...

System.Reflection.TargetInvocationException
0
 
LVL 5

Expert Comment

by:LindzK
ID: 12075117
The try/catch has to be around what you 'throw' otherwise, you throw the error - but confuse it, as it doesn't know what to do next - see code below

With the try/catch inside the set(ByVal value AS String)  it will display the alert box to teh user - you can then also reset or change the value of pName to something that won't break the rest of your code.

A text box, doesn't seem to have a databindingchanged event or anything - so this is the best I can come up with that allows you to still tab to a different control, but deal with the wrong input data when it happens

Public Property Name() As String
        Get
            Return pName
        End Get
        Set(ByVal value As String)
            Dim NotPermitted As New Exception("This value is not permitted")

            Try
                If value = "hello" Then

                    Throw NotPermitted
                Else
                    pName = value
                End If
            Catch ex As Exception
                MsgBox("This value is not permitted")
                ' set the value inside pname to something you need
                ' put any "fix it" code here
                pName = ""
            Finally
                ' put any code that must happen ( if you have any, here )
            End Try

        End Set
    End Property

Does this help you at all ? heh
0
 
LVL 5

Expert Comment

by:LindzK
ID: 12075135
What you were saying about the IDE - you told it to break when an error is thrown - that proved that the error was getting thrown, but without having the try/catch around the throw, without setting something up in the IDE - you are not catching the 'thrown' error.

The try/catch around teh throw will fix this

If your text box had better events, then you could've put the try/catch inside a databinding event - but unfortunatly there isn't one.
0
 
LVL 5

Expert Comment

by:tzxie2000
ID: 12075317
I am also be interesting with the question now as I can not understand this problem
surely LindzK's comment is OK but if we do with this format we will not need to use Exception

I think the exception may be help when

try
  newdog.name="hello"
Catch ex As Exception
  rem doing exception things
end try

but with binding the textbox.text seem not throw out the exception

if we write a code in a button click event

throw new exception("hi")

and run the program in EXE

the system will report the exception and terminated program

but by typing "hello" in textbox there will nothing happened only the textbox can not lose focus

I think It must be some surely where VB suggest us to do something with the exception

I suggest to binding.parse binding.format but not sure about it

or the binding just catch the exception and doing nothing

and then what the Author want to do is very important as the exception will terminated the program if we can not catch it



0
 

Author Comment

by:eliaslopezgtz
ID: 12075489
you are right tzxie2000 when I  assign manually the property value,  I get the exception.
but when I databind the textbox... happens that weird thing.
0
 
LVL 5

Expert Comment

by:LindzK
ID: 12075960
Have you tried the code I posted last ?

With the try/catch around the throw - it definatly throws ( and catches ) the error
0
 
LVL 5

Expert Comment

by:tzxie2000
ID: 12076012
please see
http://support.microsoft.com/default.aspx?scid=kb;en-us;317254
it said that

To work around this problem, handle the Parse event for the data-bound property, and then throw an exception. This exception is caught at a lower level and is not seen. The exception does not interfere with other data binding on the form.

as the binding call parse and format to assign from textbox.text to newdog.name and from newdog.name to textbox.text

it may mean that the .Net surely caught the exception at a lower level

so it may be surely  can not display exception
0
 
LVL 5

Expert Comment

by:LindzK
ID: 12077049
You could fire an event inside the dog class when the exception is thrown

Then you can call the event from your main class - and handle any code you need there - that would get round it.

So you would still 'catch' the problem inside the dog class - but fire an event which your main form can listen out for...

that sound good ?
0
 
LVL 5

Expert Comment

by:LindzK
ID: 12083309
What I mean is, that it is throwing an error - but without catching the error - the program stalls, until the error is fixed.  That's why you need a try/catch around the throw inside the class.

The reason that the error is caught, if you pass in 'hello' on the initial bind, is because the bind is surrounded in a try/catch - which catches the thrown error.

Now, when you change the text in the text box, as there isn't a try/catch around that - it does not catch the thrown error.

You need to have a try/catch somewhere to catch it.

There are'nt any databinding changed events on a check box, so there's nowhere to put the try/catch - so , to stop it freezing at that point, you have to put teh try/catch around the place where it throws the exception.

this isn't great - as you don't want to handle the error there - but in your main form?

then what I suggest is that you fire an event when you set pname - and you can listen for that in your main form and do the necessary coding there - if you give me a sec, i'll produce some code for you, to explain what I mean :)
0
 
LVL 5

Expert Comment

by:LindzK
ID: 12084278
Ok -
here is some code.

It has a try/catch around the throw ( you have to do that to catch what you are throwing ), but rather than do any code needed at that point - it fires an event

You listen for the event in your main form, and if you find it, you know the error was found, and do the relevant code -

paste this into your code, and let me know how you get on :)


Public Class Form1
    Inherits System.Windows.Forms.Form
    Private WithEvents newdog As dog = New dog


#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.TextBox2 = New System.Windows.Forms.TextBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(125, 111)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(120, 22)
        Me.TextBox1.TabIndex = 0
        Me.TextBox1.Text = "TextBox1"
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(250, 194)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.Size = New System.Drawing.Size(120, 22)
        Me.TextBox2.TabIndex = 1
        Me.TextBox2.Text = "TextBox2"
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(328, 72)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 2
        Me.Button1.Text = "Button1"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15)
        Me.ClientSize = New System.Drawing.Size(528, 306)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.TextBox2)
        Me.Controls.Add(Me.TextBox1)
        Me.Name = "Form1"
        Me.Text = "Form2"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try

            newdog.Name = "goodbye"
            TextBox1.DataBindings.Add("Text", newdog, "Name")
        Catch ex As Exception
            'perform any code here that you wish because the error
            'was bound
            ' You can either report it to the user
            ' log it somewhere
            ' or do nothing about it
            ' but placeing your main code inside a try/catch
            '
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub newdog_Validation(ByVal sender As Object, ByVal e As ValidEventArgs) Handles newdog.Validation
        If Not e.CheckValidResult Then
            ' String bound to dog, was not valid -
            ' do any code you need to handle the invalid result here
            MsgBox("You entered an invalid name - Please re-enter")
        End If
    End Sub
End Class


' Class that contains the data for
' the validation in event. Derives from System.EventArgs.
'
Public Class ValidEventArgs
    Inherits EventArgs
    Private _Valid As Boolean

    'Constructor.
    '
    Public Sub New(ByVal IsValid As Boolean)
        Me._Valid = IsValid
    End Sub

    Public ReadOnly Property CheckValidResult() As Boolean
        Get
            Return _Valid
        End Get
    End Property

End Class

Public Class dog

    Private pName As String

    Public Delegate Sub ValidEventHandler(ByVal sender As Object, ByVal e As ValidEventArgs)
    Public Event Validation As ValidEventHandler


    Public Property Name() As String
        Get
            Return pName
        End Get
        Set(ByVal value As String)
            Dim NotPermitted As New Exception("This value is not permitted")

            Try
                If value = "hello" Then
                    Throw NotPermitted
                Else
                    pName = value
                    Dim vt As New ValidEventArgs(True)
                    RaiseEvent Validation(Me, vt)
                End If
            Catch ex As Exception
                Dim vf As New ValidEventArgs(False)
                RaiseEvent Validation(Me, vf)
                ' set the value inside pname to something you need
                ' put any "fix it" code here
                pName = ""
            Finally
                ' put any code that must happen ( if you have any, here )
            End Try

        End Set
    End Property
End Class
0
 
LVL 5

Accepted Solution

by:
LindzK earned 500 total points
ID: 12084303
The (hopefully) good thing about it for you is that - because you catch the thrown exception, you can still tab out of the box.  But because you fire an event when the exception is thrown - you can deal wtih the exception too.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now