hi,

i write a code uisng VB.net.
i have design few checkbox and would like to write class function in class file.
can anyone guide me how to create and call the function from the class to the page.
i have create a class file as below

Public Class clsKYC

    Public Function InfoCheking(ByVal SignVerified As String, ByVal CTOSChkd As String, ByVal BursaChckd As String, ByVal AMLAChkd As String, ByVal FRCChkd As String, ByVal DFCChckd As String, ByVal BenChkd As String)

        If SignVerified = False Then
            Return "please clicked"
            Exit Function
        ElseIf CTSChkd = False Then
            Return "please clicked"
            Exit Function
        ElseIf rsaChckd = False Then
            Return "please clicked"
            Exit Function
        ElseIf AMChkd = False Then
            Return "please clicked"
            Exit Function
        ElseIf FRCChkd = False Then
            Return "please clicked"
            Exit Function
        ElseIf FCChckd = False Then
            Return "Invalid source directory"
            Exit Function
        ElseIf BnChkd = False Then
            Return "please clicked"
            Exit Function
        Else
            Return "please clicked"
            Exit Function
        End If

    End Function

End Class

then from the page i call like as below :

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


        If clsKYC.InfoCheking(checkbox1.Checked) Then
            textbox1.Enabled = True
        Else
            textbox1.Enabled = False
        End If


    End Sub

but i think there is wrong some where but i have no idea. can anyone guide me.plz

you need to instantiate the class clsKYC like
Dim objclsKYC as new clsKYC
And then call the function objclsKYC.InfoCheking

Hi tq for for prompt answer, where do i need to instantiate ?

Dim objclsKYC as new clsKYC

is it in the form page?

It looks to me like you need to start over...

  1. Item One: Only one of your arguments to the class method match a variable in the class
  2. Item Two: That's an endless If..ElseIf..Else..End If statement, use a loop
  3. Item Three: I would pass an array of checkboxes to the class method, and check from there

See demo project attached for better methodology.

Hi,
tq so much, its real helpful
but i got an error when i paste it to my app as below because i dont use group box.
what should i change to instead of using the group box.please help me !

For Each myCheckBox In myGroupBox.Controls

The demo was not for me to do your work for you, and not for you to just plug it right into your application. You're supposed to look at it, step throught it, and learn how to code properly.

I put the checkboxes in a groupbox so that I could access the GroupBox.Controls collection property. I suggest using a GroupBox in your own project for logical grouping of controls as well as layout. If you cannot use a GroupBox, try using the Form.Controls collection instead.

For example:

For each myCheckBox in Me.Controls

You also need to add this piece of code to frmMain.vb on Line 28

Erase myCheckBoxArray

You have to erase the Array or you end up leaking memory within the application.

hi,
i'm finding difficult to understand your codes even though you try to help me!
can you help me to solve base on the code i write down here.
i write this code on the button click. is this correct ?

If objclsKYC.InfoCheking(checkbox1.Checked, checkbox2.Checked, checkbox3.Checked, checkbox4.Checked, checkbox5.Checked, checkbox6.Checked, checkbox7.Checked) Then

            textbox1.Enabled = "True"
             textbox2.Enabled = "True"
              textbox3.Enabled = "True"
               textbox4.Enabled = "True"
                textbox5.Enabled = "True"
                 textbox6.Enabled = "True"
                  textbox7.Enabled = "True"
        Else

            textbox1.Enabled = "False"
             textbox2.Enabled = "False"
              textbox3.Enabled = "False"
               textbox4.Enabled = "False"
                textbox5.Enabled = "False"
                 textbox6.Enabled = "False"
                  textbox7.Enabled = "False"
        End If

I don't think so.

Let's start from the beginning.
What is this project for, is it a school project?
What are the exact requirements?

it my college project actualy,
i a design page i have few checkboxes, few text boxes and few yes/no radio buttons. i need to write a validation function in class file to validate this controls are checked and filled. (some of the controls is optional can be filled/checked or leave it blank).

once the class function done i need to call/use the class in the form page in a button click.
below is what i have done so far but i just able to control checkboxes, i do not know how to include other controls in the codes.
Please help me to solve this.Thank You !

 Public Function InfoCheking(ByVal CheckBoxArray() As Control) As String

        For Each Me.chk In CheckBoxArray
            If Not (Me.chk.Checked) Then
                Me.strReturn = "Please check all the boxes"
                Me.chk.ForeColor = Color.Red
            Else
                Me.chk.ForeColor = Color.Black
            End If
        Next
        Return Me.strReturn
    End Function





 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
    Dim chkbox As CheckBox() = {checkbox1, checkbox2, checkbox3, checkbox4, _
                              checkbox5, checkbox6, checkbox7}
        Dim txtmsg = objclsKYC.InfoCheking(chkbox)

        If txtmsg = "" Then
            'to update the info to database
            MsgBox("Your information has been updated")
        Else
            'to prompt err msg
            MsgBox(txtmsg)
            Button1.Enabled = False

        End If
 End Sub

So, if you're checking controls other than just checkbox, you need to change your class definition. What do you have for a class definition right now? I will also post a sample class for you to look at.

Update - if you're going to have to validate all of the controls, you are better off using the System.Windows.Forms.Form.ControlCollection collection object. This will already have all of your controls... :)

Suggestion for your checkbox array:
When you harcode the names of the controls, you will break the code if you change the names...

Dim chkbox As CheckBox() = {checkbox1, checkbox2, ..., checkbox5, checkbox6, checkbox7}

It is better to add the controls at runtime like the following:

For each myControl in Me.Controls
    If (TypeOf(myControl) Is CheckBox) Then
        ' checkbox specific code
    ElseIf
        (TypeOf(myControl) Is TextBox) Then
        ' textbox specific code
    Else
        ' not a checkbox or textbox
        ' handle accordingly
    End If
Next

' if you have a lot of different types to check
' a Select Case block may be cleaner
' readability == maintainability

Some thoughts:

  1. Windows Forms Controls have a built-in CausesValidation property that fires when you leave a control - this can be used to validate the user input, This event will not fire if the user never enters the control though, so if they skip that control completely, nothing happens. You should probably use this to validate the user input as the user interacts with the form & controls, and have your class check to see whether all of the required controls have actually had some input/interaction.
  2. The Windows Forms ErrorProvider (Toolbox, All Windows Forms) can provide a visual cue to the user when the control action is not correct. The documentation for the ErrorProvider Class is here on MSDN.

Edited 4 Years Ago by john.knapp

actualy i got no idea on how to modify the code but i did as below in the class function.
but i dont think so its write...can you help me to modify my code.
Thank you!

at class function

Public Function InfoCheking(ByVal CheckBoxArray() As Control) As String
 For Each myControl In Me.Controls
            If (TypeOf (myControl) Is CheckBox) Then
                If Not (Me.chk.Checked) Then
                    Me.strReturn = "Please check all the boxes"
                    Me.chk.ForeColor = Color.Red
                ElseIf (TypeOf (myControl) Is RadioButton) Then
                    If Not (Me.chk.Checked) Then
                        Me.strReturn = "Please check all the boxes"
                        Me.chk.ForeColor = Color.Red
                    Else
                        Me.strReturn = "Your information has been updated "
                        'Me.chk.ForeColor = Color.Black
                    End If
                End If
            End If
        Next
end function

Button Click

 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

       Dim chkbox As CheckBox() = {checkbox1, checkbox2, checkbox3, checkbox4, _
                                    checkbox5, checkbox6, checkbox7}

        Dim txtmsg = objclsKYC.InfoCheking(chkbox)

        If txtmsg = "" Then
            'to update the info to database
            MsgBox("Your information has been updated")

        Else
            'to prompt err msg
            MsgBox(txtmsg)
            Button1.Enabled = False
        End If

    End Sub

the above code is not working at all. can you please help me to modify the code accordingly.

You can't add Radio Buttons to that existing method, to start with - you only have checkboxes in the array - which is also named CheckBoxArray, if you add other controls it only adds confusion.

i need to a design page which have few checkboxes, few text boxes and few yes/no radio buttons. i need to write a validation function in class file to validate this controls are checked and filled. (some of the controls is optional can be filled/checked or leave it blank).

once the class function done i need to call/use the class in the form page in a button click.

i need to do this, can you help me !

in a form page. i didnt create group box or panel
i have design the control boxes using ASP.net and code using the vb.net
below is my design code using ASP.net

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="Checklist.ascx.vb"
    Inherits="Flow. Checklist" %>

<style type="text/css">
    .style1
    {
        width: 77px;
    }
    .style2
    {
        width: 240px;
    }
    .style3
    {
        height: 23px;
    }
</style>
<table cellpadding="0" cellspacing="0" class="tblContent1" border="1">
    <tr>
       <td class="style3">
            <asp:Label runat="server" ID="label1" 
                Width="194px">a. Signature verified</asp:Label>
        </td>

        <td class="style3">
            <asp:CheckBox runat="server"  ID="checkbox1" Width="100px"></asp:CheckBox>
        </td>

 <td class="style3">
            <asp:TextBox runat="server" ID="textbox1" 
                Width="765px"></asp:TextBox>
        </td>

    </tr>

    <tr>
      <td >
            <asp:Label runat="server" ID="label2"  >b. Bank Checked</asp:Label>
        </td>
        <td>
            <asp:CheckBox runat="server" ID="checkbox2" Width="100px"></asp:CheckBox>
        </td>
         <td>
            <asp:TextBox runat="server" ID="textbox2" Width="765px"></asp:TextBox>
        </td>
    </tr>
    <tr>
      <td >
            <asp:Label runat="server" ID="label3"  >c. Loan Defaulter Checked</asp:Label>
        </td>
        <td>
            <asp:CheckBox runat="server" ID="checkbox3" Width="100px"></asp:CheckBox>
        </td>
          <td>
            <asp:TextBox runat="server" ID="textbox3" 
                  Width="765px"></asp:TextBox>
        </td>
    </tr>
    <tr>
      <td >
            <asp:Label runat="server" ID="label4"  
                Width="350px">d. Personal List Checked</asp:Label>
        </td>
        <td>
            <asp:CheckBox runat="server" ID="checkbox4" Width="100px" style="height: 21px"></asp:CheckBox>
        </td>
        <td>
            <asp:TextBox runat="server" ID="textbox4" Width="765px"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td >
            <asp:Label runat="server" ID="label5" >e. Blacklist  Checked</asp:Label>
        </td>
        <td>
            <asp:CheckBox runat="server" ID="checkbox5" Width="100px"></asp:CheckBox>
        </td>
      <td>
            <asp:TextBox runat="server" ID="textbox5" Width="765px"></asp:TextBox>
        </td>
    </tr>
    <tr>
       <<td >
            <asp:Label runat="server" ID="label6" >f. ABB Updated</asp:Label>
        </td>
        <td>
            <asp:CheckBox runat="server" ID="checkbox6" Width="100px"></asp:CheckBox>
        </td>
 <td>
            <asp:TextBox runat="server" ID="textbox6" Width="765px"></asp:TextBox>
        </td>
    </tr>
    <tr>
 <td >
            <asp:Label runat="server" ID="label7" >g. Server Updated</asp:Label>
        </td>
        <td>
            <asp:CheckBox runat="server" ID="checkbox7" Width="100px"></asp:CheckBox>
        </td>
<td>
            <asp:TextBox runat="server" ID="textbox7" Width="765px"></asp:TextBox>
        </td>
    </tr>
    </table>

    <table cellpadding="0" cellspacing="0" class="tblContent2" border="1">

    <tr>
       <td>
            <asp:Label runat="server" ID="label8"  Width="453px">h. Do you have other bank loan</asp:Label>
        </td>
      <td>
            <asp:RadioButton runat="server" ID="RadioButton1"  width="100px" 
                Text="Yes" style="height: 21px" ></asp:RadioButton>
        </td>
       <td>
            <asp:RadioButton runat="server" ID="RadioButton2" width="100px" 
                Text="No"></asp:RadioButton>
                        </td>
  <td>
            <asp:TextBox runat="server" ID="textbox8" 
                Width="560px" style="margin-left: 1px" ></asp:TextBox>
        </td>
    </tr>



    <tr>
     <td>
            <asp:Label runat="server" ID="label9" Enabled="False" >i. Risk classification</asp:Label>
        </td>
    <td>
            <asp:RadioButton runat="server" ID="RadioButton3"  width="100px" 
                Text="High"></asp:RadioButton>
        </td>
     <td>
            <asp:RadioButton runat="server" ID="RadioButton4" width="100px" 
                Text="Low"></asp:RadioButton>
        </td>
    </tr>
    </table>

    <table cellpadding="0" cellspacing="0" class="tblContent3" border="1">

    <tr>
   <td>
            <asp:Label runat="server" ID="label10" Width="1221px">Reason of Being Default :</asp:Label>
        </td>
        </tr>
    </table>

    <table cellpadding="0" cellspacing="0" class="tblContent4" border="1">
    <tr>
      <td >
            <asp:Label runat="server" ID="label11" Width="349px">i) geographic location</asp:Label>
        </td>
        <td class="style1">
            <asp:CheckBox runat="server" ID="checkbox8" Width="100px"></asp:CheckBox>
        </td>
          <td>
            <asp:TextBox runat="server" ID="textbox9" 
                  Width="768px"></asp:TextBox>
        </td>
    </tr>

    <tr>
      <td >
            <asp:Label runat="server" ID="label12">ii) clasification </asp:Label>
        </td>
        <td class="style1">
            <asp:CheckBox runat="server" ID="checkbox9" Width="100px"></asp:CheckBox>
        </td>
          <td>
            <asp:TextBox runat="server" ID="textbox10" Width="768px"></asp:TextBox>
        </td>
    </tr>
    </table>

    <table cellpadding="0" cellspacing="0" class="tblContent5" border="1">

    <tr>
       <td >
            <asp:Label runat="server" ID="label13" 
                Width="449px">iii) Others,pls state</asp:Label>
        </td>

 <td >
            <asp:TextBox runat="server" ID="textbox11" 
                Width="769px" Height="22px"></asp:TextBox>
        </td>
    </tr>
</table>

    <table cellpadding="0" cellspacing="0" class="tblContent6" border="1">

<tr>
 <td class="style2">
            <asp:Label runat="server" ID="label14" Width="448px" >j. Approved Interest Rate : </asp:Label>
        </td>
        <td>
            <asp:TextBox runat="server" ID="textbox13" 
                Width="169px" style="margin-left: 0px"></asp:TextBox>
        </td>
<td>
            <asp:Label runat="server" ID="Label15"   Width="598px" Height="19px">    &nbsp &nbsp &nbsp (to be approved by approving authority)</asp:Label>
        </td>
    </tr>
    </table>

     <table cellpadding="0" cellspacing="0" class="tblContent7" border="1">

    <tr>
      <td>
            <asp:Label runat="server" ID="label16" Width="448px">Remarks : </asp:Label>
        </td>

          <td>
            <asp:TextBox runat="server" ID="textbox12" 
                  Width="768px"></asp:TextBox>
        </td>
    </tr>
</table>

    <table cellpadding="0" cellspacing="0" class="tblContent8" border="1">

<tr>
 <td>
            <asp:Label runat="server" ID="label17" Width="447px" >j. Approved Loan Limits : </asp:Label>
        </td>
        <td>
            <asp:TextBox runat="server" ID="textbox14" 
                Width="169px">$ &nbsp </asp:TextBox>
        </td>
<td>
            <asp:Label runat="server" ID="Label18" Width="598px">    &nbsp &nbsp &nbsp (to be approved by approving authority)</asp:Label>
        </td>
    </tr>
    </table>

     <table cellpadding="0" cellspacing="0" class="tblContent9" border="1">

    <tr>
      <td>
            <asp:Label runat="server" ID="label19" Width="447px">Remarks : </asp:Label>
        </td>

          <td >
            <asp:TextBox runat="server" ID="textbox15" 
                  Width="770px"></asp:TextBox>
        </td>
    </tr>
</table>

<table cellpadding="0" cellspacing="0" class="tblContent10" border="1">

<tr>
<td> 
            <asp:Label runat="server" ID="label20" Width="447px" >l. Payment Mode : </asp:Label>
        </td>
<td>
           <asp:CheckBox runat="server" ID="checkbox10" Text="Cash" Width="100px"></asp:CheckBox>
        </td> 
<td >
        <asp:CheckBox runat="server" ID="checkbox11" Text="Cheque" Width="100px"></asp:CheckBox>
        </td> 
<td >
<asp:CheckBox runat="server" ID="checkbox12" Text="Debit" Width="567px"></asp:CheckBox>
</td> 
    </tr>
    </table>

<asp:Button ID="Button1" runat="server"  Text="Save" Width="281px" OnClick="Button1_Click" />

Edited 4 Years Ago by swathys

<asp:CheckBox runat="server" ID="checkbox1" Width="100px"></asp:CheckBox>

oh... my... gosh...

Protected Sub Page_Load

I should have noticed this and asked... <face palm>

Edited 4 Years Ago by john.knapp

@Swathys

Web Forms and Windows Forms have two completely different object models, and I have been coding for Windows Forms this entire time.

We need to start over, from scratch

Edited 4 Years Ago by john.knapp

Post source code for "Checklist.ascx.vb".
Checklist.ascx is also "hosted" by a WebForm page, isn't it? Post that page and any code behind also.

below is the code...Please help me !

Checklist.ascx.vb

Partial Public Class Checklist
    Inherits System.Web.UI.UserControl
    Dim objclsKYC As New clsKYC
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

        Dim chkbox As CheckBox() = {checkbox1, checkbox2, checkbox3, checkbox4, _
                                    checkbox5, checkbox6, checkbox7, checkbox8, checkbox9}

        Dim txtmsg = objclsKYC.InfoCheking(chkbox)

        If txtmsg = "" Then
            'to update the info to database code
            MsgBox(txtmsg)
        Else
            'to prompt err msg
            MsgBox(txtmsg)
            Button1.Enabled = False
        End If

    End Sub

Protected Sub RadioButton1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles RadioButton1.CheckedChanged
        If RadioButton1.Checked = True Then
            RadioButton2.Checked = False
        ElseIf RadioButton1.Checked = False Then
            RadioButton2.Checked = True
        End If
    End Sub

    Protected Sub RadioButton2_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles RadioButton2.CheckedChanged
        If RadioButton2.Checked = True Then
            RadioButton1.Checked = False
        ElseIf RadioButton2.Checked = False Then
            RadioButton1.Checked = True
        End If
    End Sub

    Protected Sub RadioButton3_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles RadioButton3.CheckedChanged
        If RadioButton3.Checked = True Then
            RadioButton4.Checked = False
        ElseIf RadioButton3.Checked = False Then
            RadioButton4.Checked = True
        End If
    End Sub

    Protected Sub RadioButton4_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles RadioButton4.CheckedChanged
        If RadioButton4.Checked = True Then
            RadioButton3.Checked = False
        ElseIf RadioButton4.Checked = False Then
            RadioButton3.Checked = True
        End If
    End Sub
End Class

clsKYC.vb

Imports System.Drawing

Public Class clsKYC
    Inherits System.Web.UI.UserControl
    Private chk As CheckBox
    Private strReturn As String

    Public Function InfoCheking(ByVal CheckBoxArray() As Control) As String

        For Each Me.chk In CheckBoxArray
            If Not (Me.chk.Checked) Then
                Me.strReturn = "Please check all the boxes"
                Me.chk.ForeColor = Color.Red
            Else
                Me.strReturn = "Information has been updated"
                'update to database code
                Me.chk.ForeColor = Color.Black
            End If
        Next
Return Me.strReturn
End Function
End Class

Edited 4 Years Ago by swathys

I did not know its web application. I think you can do this validation using javascript at client side rather than doing it in server side. I am not good to write code for you in asp.net. Just search in net how to validate checkboxes using javascript. U call that function in submitt button click in aspx code.

Edited 4 Years Ago by Pgmer

actualy i need to write the code in vb.net and its nothing to do in asp.ner...its just a design only but all the coding i did in vb.net...
i juz need a help to modify the souce code that i wrote in vb.net that i alredy send earlier...plz modify my code in this as below...

i need a vb.net code
i need to a design page which have few checkboxes, few text boxes and few yes/no radio buttons. i need to write a validation function in class file to validate this controls are checked and filled. (some of the controls is optional can be filled/checked or leave it blank).
once the class function done i need to call/use the class in the form page in a button click.

below is the souce code that i done so far and need modification as per above requirement

** at class function**

Public Function InfoCheking(ByVal CheckBoxArray() As Control) As String
 For Each myControl In Me.Controls
            If (TypeOf (myControl) Is CheckBox) Then
                If Not (Me.chk.Checked) Then
                    Me.strReturn = "Please check all the boxes"
                    Me.chk.ForeColor = Color.Red
                ElseIf (TypeOf (myControl) Is RadioButton) Then
                    If Not (Me.chk.Checked) Then
                        Me.strReturn = "Please check all the boxes"
                        Me.chk.ForeColor = Color.Red
                    Else
                        Me.strReturn = "Your information has been updated "
                        'Me.chk.ForeColor = Color.Black
                    End If
                End If
            End If
        Next
end function

** Button Click**

 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
       Dim chkbox As CheckBox() = {checkbox1, checkbox2, checkbox3, checkbox4, _
                                    checkbox5, checkbox6, checkbox7}
        Dim txtmsg = objclsKYC.InfoCheking(chkbox)
        If txtmsg = "" Then
            'to update the info to database
            MsgBox("Your information has been updated")
        Else
            'to prompt err msg
            MsgBox(txtmsg)
            Button1.Enabled = False
        End If
    End Sub

i desperately need a help on this...plz help !!
i need a vb.net code purely...

Edited 4 Years Ago by swathys

Best practice is to validate controls client-side and server-side.
Like pgmer said, client-side valiation is usually done with javascript, although it can be done with vbscript if that is your preference.

Regarding this statement:

actualy i need to write the code in vb.net and its nothing to do in asp.ner...its just a design only but all the coding i did in vb.net...

It has everything to do with ASP.Net. The ASP.Net object model governs how the controls are instantiated, accessed, loaded, etc.

For example, just copying and pasting your code doesn't work for me. See attached screenshot

Edited 4 Years Ago by john.knapp

Attachments asp_with_intellisense_errors.jpg 55.23 KB

At the very top of "checklist.ascx" you have this directive

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="Checklist.ascx.vb" Inherits="Flow. Checklist" %>

According to that line, Class Checklist is in Namespace Flow.
None of your other source files have the Namespace Flow directive.

In your solution, go to "Class View", expand the view on each object once, and take a screenshot. Crop, save as jpeg, and upload that screenshot

Edited 4 Years Ago by john.knapp

This article has been dead for over six months. Start a new discussion instead.