2010-01-12 5 views
2

form을 vb6의 class module으로 변환 할 수 있습니까?형식으로 클래스 모듈 만들기

+0

질문 –

+0

당신은 그것을 편집 할 수 ... 이해가되지 않습니다 : 다음은 다른 코드는 우리가 방금 만든 예를 들어 로그인 대화 상자를 사용하는 방법의 예입니다 제발 좀 더 분명히 해줘 요? –

+1

다른 프로젝트에서 다시 사용할 수 있도록 ActiveX DLL이나 OCX ("자체 포함")에 양식을 포함시킬 수 있기를 원하십니까? Common Dialog 컨트롤의 작동 방식과 비슷한 것일까? –

답변

7

이렇게하는 간단한 방법 중 하나는 VB6 IDE에서 새 ActiveX DLL 프로젝트를 만든 다음 새 Form을 프로젝트에 추가하는 것입니다. 또한 클래스가 필요하지만 프로젝트에 추가되는 기본 "Class1"의 이름을 바꿀 수 있습니다.

평소와 같이 양식을 만든 다음 양식을 표시하고 선택적으로 호출자에게 정보를 반환합니다 (반환 값, 이벤트 또는 클래스의 공용 속성을 통해). . 일단 DLL을 컴파일하면 다른 프로젝트에서 DLL에 대한 참조를 추가하고 공용 클래스를 인스턴스화하여 양식을 사용할 수 있습니다.

다음은 몇 가지 프로젝트에서 다시 사용할 수있는 일반 로그인 대화 상자를 만드는 방법을 보여주는 간단한 예제입니다. 로그인 대화 상자에는 사용자 이름 및 암호 필드와 확인 및 취소 버튼이있는 로그인 화면이 표시됩니다. 공용 클래스 인 LoginDialog은 다른 프로젝트에서 실제로 로그인 양식을 표시하고 해당 양식에서 데이터를 검색하는 데 사용될 수 있습니다 (사용자가 입력 한 실제 사용자 이름과 암호 및 사용자가 대화 상자를 취소했는지 여부). public 클래스는 폼에서 제공하는 기능을 둘러싼 래퍼입니다.

는이 개념

  • 새로운 양식을 작성하고 ActiveX DLL 프로젝트에 추가를 보여 그냥 빨리 예입니다 유의하시기 바랍니다. 그것은 frmLogin의 이름을 변경하고 여기에 다음 컨트롤을 추가

    • 텍스트 상자는 텍스트 상자가 txtPassword이름
    • txtUsername을 지명했다. PasswordChar에게 속성을 설정하려면 "*"
    "취소"로 설정 캡션과 cmdCancel 이름 (별표)
  • 있는 명령을 "OK"
  • 있는 명령 세트 캡션 에게 cmdOK 이름


  • 그런 다음 FOL을 추가 frmLogin.frm에 lowing 코드 : 프로젝트에 "LoginDialog"에서


'frmLogin.frm' 

Public Cancelled As Boolean 'Set if the user clicks Cancel or closes the form' 

Private Sub cmdCancel_Click() 
    'User cancelled the dialog by clicking Cancel...' 
    Me.Cancelled = True 
    Me.Hide 
End Sub 

Private Sub Form_QueryUnload(Cancel As Integer) 
    'User cancelled the dialog by closing the window...' 
    Me.Cancelled = True 
    Me.Hide 
End Sub 

Private Sub cmdOK_Click() 
    'Make sure the user filled in both fields.' 
    If Trim(txtUsername.Text) = "" And _ 
     Trim(txtPassword.Text) = "" Then 

     MsgBox "You must enter both a username and password." 
     Exit Sub 

    ElseIf Trim(txtUsername.Text) = "" Then 
     MsgBox "You must enter a username." 
     Exit Sub 
    ElseIf Trim(txtPassword.Text) = "" Then 
     MsgBox "You must enter a password." 
     Exit Sub 
    End If 

    'User filled in the necessary data - we can hide the form now' 
    Me.Cancelled = False 
    Me.Hide 

End Sub 


  • 이름 바꾸기 "클래스 1"과 다음 코드를 추가합니다.그래서


'LoginDialog.cls' 

'A public class that allows other projects to display a login  ' 
'dialog and retrieve the user`s login information and whether or not ' 
'they cancelled the dialog.           ' 
'This code assumes you have a form in the same project named frmLogin' 
'and that it contains 2 textboxes, txtUsername and txtPassword, and ' 
'2 command buttons, cmdOK and cmdCancel.        ' 
'                 ' 

Public Username As String   'The username entered by the user' 
Public Password As String   'The password entered by the user' 
Public CancelledByUser As Boolean 'True if the user cancels or closes the form' 

'Shows a new Login form with the specified defaults filled in, if provided.' 
'                   ' 
'                   ' 
Public Function Show() 

    'Create the login form and fill in the defaults' 
    Dim frm As frmLogin 
    Set frm = New frmLogin 

    frm.txtUsername = Me.Username 
    frm.txtPassword = Me.Password 

    'Shows the form until it is hidden or closed' 
    frm.Show vbModal 

    If frm.Cancelled Then 
     Me.CancelledByUser = True 
    Else 
     'Get the username and password from the form' 
     Me.Username = frm.txtUsername 
     Me.Password = frm.txtPassword 
     Me.CancelledByUser = False 
    End If 

    'Unload the form' 
    Unload frm 

End Function 
  • 액티브 X 프로젝트를 컴파일하고 (즉, MyAppLoginUI에게) 이름을 지정 :이 클래스 다른 프로젝트는 로그인 양식 (frmLogin을)으로 모니터하는 데 사용하는 것입니다 그것을 다른 프로젝트에 추가해야 할 때 쉽게 식별 할 수 있습니다.

  • 다른 프로젝트에서 양식을 사용하려면 메뉴에서 프로젝트 -> 참조 ...으로 이동하여 ActiveX DLL을 프로젝트에 추가하십시오. 찾으려면 Browse...을 클릭해야 할 수도 있습니다. 이 약자로


'LoginExample.bas' 

' A simple example of how another project might use  ' 
' the generic login form created in the previous steps. ' 
' This example displays the login screen, then tries ' 
' to authenticate the user against a database.   ' 
'              ' 
Public Sub PerformLogin() 

    Dim login As New LoginDialog 

    'Pre-fill the username with the username of the last user who logged in.' 
    login.Username = GetSetting("MyApp", "Settings", "LastUser") 

    'Show the login screen. It will stay up until the user clicks OK or Cancel' 
    login.Show() 

    'If the user cancelled the login form, exit now...' 
    If login.CancelledByUser Then 
     Exit Sub 
    End If 

    'Pretend DAL is a data access layer module...' 
    If Not DAL.LoginUser(login.Username, login.Password) 
     MsgBox "Invalid username or password.", vbCritical+vbOKOnly, "Login Failure" 
    End If 

End Sub