Click here to Skip to main content
15,905,229 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am using class library in vb 2010 to make a custom textbox control.
i'm trying to add the list of control components which are in the main form
to the textbox property,
when i add a new textbox (my custom textbox) to the form i need the class to get the form component like (AxComms, Labels, ...etc) and deal with this component from my class (my custom textbox),
like get it's name as i tried below "CXComm = Comms.Name" before run the application
just when i drop the custom textbox in the form.

I trid the code below at sub new but the "
VB
Application.OpenForms.Item(0)
" just work and get the open Forms when the application is running.
and so on i can't access the form Control Components

What I have tried:

VB
Imports System.Windows.Forms
Imports System.IO
Imports System.ComponentModel
Imports System.Drawing
Imports CXSERVERCOMMUNICATIONSCONTROLLib

Imports System.Drawing.Drawing2D

Public Class Fit_TextBox

    Inherits System.Windows.Forms.TextBox

    Public Comms As New AxCXSERVERCOMMUNICATIONSCONTROLLib.AxComms

    Dim CommsInForm As AxCXSERVERCOMMUNICATIONSCONTROLLib.AxComms

    Dim curColor As Color
    Dim lastData As Single
    Dim MaxValue As Decimal
    Dim MinValue As Decimal
    Dim FactorVal As Decimal = 1
    Dim mainToolTip As String
    Dim txtval As String
    Dim FormName As String = "frmMain"
    Dim pnlName As String = "Panel1"
    Dim CXComm As String = "AxComms1"
    Dim tagname As String = "Tag"
    Dim plcName As String = "PLC"

    Dim ErrorProvider1 As New System.Windows.Forms.ErrorProvider

    Public x As TextBox

    Dim Error_Bit As Boolean = False

    Dim frm As Form
    Dim toolTip1 As New ToolTip()
    Dim lbl As Label
    Dim txt As TextBox
    Dim pnl, pnlC As Panel

    Dim Highlighter1 As New DevComponents.DotNetBar.Validator.Highlighter



    Public Sub New()

        '******************** Get Project Form and Comms Control Component
        frm = Application.OpenForms.Item(0)
        For Each c As Control In frm.Controls
            If InStr(c.Name, "AxComms1") Then
                Comms = c
                CXComm = Comms.Name
            End If
        Next
    End Sub

' ============================ Property ========================
Property Comms_In_Form As AxCXSERVERCOMMUNICATIONSCONTROLLib.AxComms
        Get
            'Comms.Name
            Return CommsInForm
        End Get
        Set(value As AxCXSERVERCOMMUNICATIONSCONTROLLib.AxComms)
            CommsInForm = value
        End Set
    End Property

    Property CX_Comms As String
        Get
            'Comms.Name
            Return CXComm
        End Get
        Set(value As String)
            CXComm = value
        End Set
    End Property

' ================================== Events ==============
  Private Sub Class1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

        Dim err As Boolean = False
        If (Me.Text = "") Then GoTo lblErr

        If e.KeyValue = 27 Then
            Me.Refresh()
            Me.Text = lastData
            pnl.Focus()
            Exit Sub
        End If

        If e.KeyValue = 13 Then


            ' ***************** Check if numeric value
            If Not IsNumeric(Me.Text) Then
                ErrorProvider1.SetError(Me, "Not Numeric Number")
                HighLighter_Sub(True)
                err = True
                GoTo lblErr
            End If

            ' ***************** Check minimum and maximum value
            If Max_Value <> 0 Or Min_Value <> 0 Then
                If Me.Text < Min_Value Then
                    ErrorProvider1.SetError(Me, "Exceeds Min Limit '" & MinValue & "'")
                    HighLighter_Sub(True)
                    err = True
                    GoTo lblErr
                End If

                If Me.Text > Max_Value Then
                    ErrorProvider1.SetError(Me, "Exceeds Max Limit '" & Max_Value & "'")
                    HighLighter_Sub(True)
                    err = True
                    GoTo lblErr
                End If
            End If

            ' ***************** Write data to PLC 
            frm.Comms.Write("CJ1M", tagname, Me.Text * Factor)
            lastData = Me.Text
            pnl.Focus()
        End If
lblErr:
        If Not err Then
            toolTip1.SetToolTip(Me, mainToolTip)
            HighLighter_Sub(False)
            ErrorProvider1.SetError(Me, "")
        Else
            Error_Bit = True
        End If

    End Sub
Posted
Updated 13-Aug-16 2:05am
v2
Comments
OriginalGriff 1-Aug-16 4:54am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
And at the moment this doesn't make a lot of sense without teh context you have have and we don't!
Perhaps if you try explaining in more detail what you are doing and what problem it's causing you it would help?
Use the "Improve question" widget to edit your question and provide better information.

Hi, did you tried passing the required controls to the calling function in class file as parameter?
 
Share this answer
 
I have tried this and its work well

VB
Public Sub New()
     frm = Application.OpenForms.Item(0)
     For Each c As Control In frm.Controls
          If (c.GetType.ToString = "TextBox") Then
              txtControl = c
          End If
     Next
End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900