1.11M Members

Write class function in class file

 
0
 

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

 
0
 

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

 
0
 

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

Dim objclsKYC as new clsKYC

is it in the form page?

 
0
 

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.

Attachments CheckBoxCheckerDemoProject.zip (67.51KB)
 
0
 

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
 
0
 

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

 
0
 

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.

 
0
 

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
 
0
 

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?

 
0
 

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
 
0
 

That code is much cleaner to read - you're getting there!

 
0
 

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.

 
0
 

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
 
0
 

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.
 
0
 

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
 
0
 

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

 
0
 

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.

 
0
 

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 !

 
0
 

Your check box and radio buttons are in group box or in panel?

 
0
 

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" />
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article