locked
Custom validation on textboxes RRS feed

  • Question

  • Hi,

    I have a table with about 20 rows and each row has  2 textboxes that takes only integers txtMinValue and txtMaxValue

    (for each row obviously the names differ).

    My boss wants me to use custom validation (he said something about using sender to identify the correct textboxes for each row) to compare the 2 values and make sure that the value in txtMinValue  is smaller than the value in txtMaxValue.

    How can I do that?

    Thanks

    • Moved by Youjun Tang Tuesday, July 7, 2015 8:56 AM related to asp.net
    Wednesday, June 17, 2015 7:02 PM

Answers

  • Hi,

    I have a normal html table. When user clicks submit button I want the validation to work. But I also want to try and do it once he leaves the second (txtmaxvalue) textbox.

    Thanks


    I've no idea what an HTML Table is. But if it's an ASP .Net control then I would suggest asking the question in one of the ASP .Net forums.

    La vida loca

    • Proposed as answer by Cor Ligthert Thursday, June 18, 2015 6:14 AM
    • Marked as answer by Just Karl Wednesday, July 8, 2015 8:04 PM
    Thursday, June 18, 2015 4:22 AM

All replies

  • Hi,

    I have a table with about 20 rows and each row has  2 textboxes that takes only integers txtMinValue and txtMaxValue

    (for each row obviously the names differ).

    My boss wants me to use custom validation (he said something about using sender to identify the correct textboxes for each row) to compare the 2 values and make sure that the value in txtMinValue  is smaller than the value in txtMaxValue.

    How can I do that?

    Thanks

    A table?

    Do you mean a DataGridView bound to a DataTable?


    Still lost in code, just at a little higher level.

    :-)

    Wednesday, June 17, 2015 7:09 PM
  • How are you managing this table? Are you using a datagridview or some other control.

    At what point do you want to validate?

    It is difficult answering a question with such limited information.


    David M. Nichols software engineer

    Wednesday, June 17, 2015 7:09 PM
  • Hi,

    I have a table with about 20 rows and each row has  2 textboxes that takes only integers txtMinValue and txtMaxValue

    (for each row obviously the names differ).

    My boss wants me to use custom validation (he said something about using sender to identify the correct textboxes for each row) to compare the 2 values and make sure that the value in txtMinValue  is smaller than the value in txtMaxValue.

    How can I do that?

    Thanks

    Do you mean a DataTable or a TableLayoutPanel or what?

    I've never used a DataTable with TextBox's and don't know how that would or could work.

    BTW did you get the Pizza job?


    La vida loca


    Thursday, June 18, 2015 12:32 AM
  • I suppose if you are refering to the Table Class then perhaps the question should be asked in one of the Asp.Net forums perhaps.

    This example only displays using direct cast to access the sender though other methods are available most likely. I'm fairly certain this is not what you would use though.

    Option Strict On
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            For Each Ctl As Control In Me.Controls.OfType(Of TextBox)()
                AddHandler Ctl.TextChanged, AddressOf TextBoxs_TextChanged
            Next
        End Sub
    
        Private Sub TextBoxs_TextChanged(sender As Object, e As EventArgs)
            MessageBox.Show(DirectCast(sender, TextBox).Name)
        End Sub
    
    End Class


    La vida loca

    Thursday, June 18, 2015 12:59 AM
  • My boss wants me to use custom validation (he said something about using sender to identify the correct textboxes for each row) to compare the 2 values and make sure that the value in txtMinValue  is smaller than the value in txtMaxValue.

    By 'custom validation' you probably mean this:
    https://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating(v=vs.110).aspx

    You will need to identify the 'other' text box involved in the min/max testing.  Put that 'other' text box in the Tag property of each text box - that is, put the min text box in the tag of the max text box and the max text box in the tag of the min text box.

    You create one event handler method for the min text box and one event handler for the max text box.

    You then set the handler for the max text box validation event for every max text box to the same event handler code, probably using AddHandler
    https://msdn.microsoft.com/en-us/library/7taxzxka.aspx

    and the corresponding procedure for the min text boxes.

    In those event handler methods you cast the sender argument to a TextBox, get the other text box from the tag property, cast it to a text box, and do the comparison. 

    https://msdn.microsoft.com/en-us/library/7k6y2h6x.aspx

    Note that the the Handles clause in the event handler signature can take a list of events to be handled.

      It makes no difference what sort of container the text boxes are contained in.

    • Edited by Acamar Thursday, June 18, 2015 1:45 AM sp
    Thursday, June 18, 2015 1:37 AM
  • Hi,

    I have a normal html table. When user clicks submit button I want the validation to work. But I also want to try and do it once he leaves the second (txtmaxvalue) textbox.

    Thanks

    Thursday, June 18, 2015 4:16 AM
  • Hi,

    I have a normal html table. When user clicks submit button I want the validation to work. But I also want to try and do it once he leaves the second (txtmaxvalue) textbox.

    Thanks


    I've no idea what an HTML Table is. But if it's an ASP .Net control then I would suggest asking the question in one of the ASP .Net forums.

    La vida loca

    • Proposed as answer by Cor Ligthert Thursday, June 18, 2015 6:14 AM
    • Marked as answer by Just Karl Wednesday, July 8, 2015 8:04 PM
    Thursday, June 18, 2015 4:22 AM