CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2009
    Posts
    28

    modules within class

    hi all again, is it possible to create a module within a class?

    what i need is something like this:

    public class form1
    public sub i_nid_this_in_many_place()
    end sub
    end class

    i couldn't inherit form1 with a class that had i_nid_this_in_many_place() because its devastatingly sad that form1 is already inherited

    so i thought of this :
    public class form1
    end class
    module cool
    public sub i_nid_this_in_many_place()
    end sub
    end module

    but this wouldn't work cause i_nid_this_in_many_place() needs to access a property of form1

    this would work: (however that would make my module in other class useless)

    public class form1
    end class
    module cool
    public sub i_nid_this_in_many_place()
    ' form1.this_prop
    end sub
    end module

    i could only think of this solution (passing form1 itself as an argument):
    public class form1
    ' stuff
    i_nid_this_in_many_place(me)
    ' and stuff
    end class
    module cool
    public sub i_nid_this_in_many_place(byvar o as object)
    ' o.this_prop
    end sub
    end module

    is there a more efficient way so that i do not have to include the object itself as an argument to my sub?

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: modules within class

    Please use CODE TAGS around your code. You can create a new instance of a form, and set the properties of that object. Take a look at this CDC form:

    Code:
    Option Explicit
    
    ' cdlCFPrinterFonts &H2
    
    ' Save File Options
    '&H2 - Forces a warning before overwriting a file
    '&H8 - Stops default directory from changing
    '&H200 - more than one file can be selected.
    '&H1000 - This makes it so the file must exist.
    '&H2000 - This warns the user before creating a new file.
    
    Private Sub cmdOpen_Click()
      ' CancelError is True.
       On Error GoTo ErrHandler
       CommonDialog1.InitDir = App.Path
       ' Set Flags
        CommonDialog1.Flags = cdlOFNAllowMultiselect Or cdlOFNLongNames
       ' Set filters.
       CommonDialog1.Filter = "All Files (*.*)|*.*|Text" & _
          "Files (*.txt)|*.txt|Batch Files (*.bat)|*.bat"
       '  CommonDialog1.Filter = "AutoForm Files (*.doc) (*.rtf)|*.doc;*.rtf|All " & _
              "Files (*.*)|*.*"
       
       ' Specify default filter.
       CommonDialog1.FilterIndex = 2 ' Default to TEXT
       ' Display the Open dialog box.
       CommonDialog1.ShowOpen
       ' Call the open file procedure.
     '  OpenFile (CommonDialog1.FileName)
       Debug.Print CommonDialog1.FileName
       Exit Sub
    
    ErrHandler:
    ' User pressed Cancel button.
       Exit Sub
    
    End Sub
    
    Private Sub cmdShowColor_Click()
      ' Set Cancel to True
      CommonDialog1.CancelError = True
      On Error GoTo ErrHandler
      ' Set the Flags property
      CommonDialog1.Flags = cdlCFEffects Or cdlCFBoth
      ' Display the Font dialog box
      CommonDialog1.ShowColor
      rtb.BackColor = CommonDialog1.Color
      Exit Sub
    ErrHandler:
      ' User pressed the Cancel button
      Exit Sub
    End Sub
    
    Private Sub cmdShowFont_Click()
      ' Set Cancel to True
      CommonDialog1.CancelError = True
      On Error GoTo ErrHandler
      ' Set the Flags property
      CommonDialog1.Flags = cdlCFEffects Or cdlCFBoth ' cdlCFPrinterFonts
      ' Display the Font dialog box
      CommonDialog1.ShowFont
      If rtb.SelLength = 0 Then
        rtb.SelStart = 0
        rtb.SelLength = Len(rtb.SelRTF)
      End If
      rtb.SelFontName = CommonDialog1.FontName
      rtb.SelFontSize = CommonDialog1.FontSize
      rtb.SelBold = CommonDialog1.FontBold
      rtb.SelItalic = CommonDialog1.FontItalic
      rtb.SelUnderline = CommonDialog1.FontUnderline
      rtb.SelStrikeThru = CommonDialog1.FontStrikethru
      rtb.SelColor = CommonDialog1.Color
      Exit Sub
    ErrHandler:
      ' User pressed the Cancel button
      Exit Sub
    End Sub
    
    Private Sub cmdSaveFile_Click()
        Dim strFileName As String
        Dim ans As Integer
        CommonDialog1.Flags = &H2 ' Overwrite Flag
        CommonDialog1.Filter = "RTF|*.rtf|Text|*.txt"
        CommonDialog1.ShowSave
        On Error GoTo SaveProblems
        strFileName = CommonDialog1.FileName
        If CommonDialog1.FilterIndex = 1 Then
            CommonDialog1.DefaultExt = "rtf"
            rtb.SaveFile strFileName
        Else
            CommonDialog1.DefaultExt = "txt"
            rtb.SaveFile strFileName, rtfText
        End If
        Exit Sub
    SaveProblems:
            MsgBox "Can’t save the file, try again.", vbCritical
        Exit Sub
    End Sub
    
    Private Sub Form_Load()
      Label1.Caption = "Open File into RTB " & vbCrLf & _
                       "Save File from RTB" & vbCrLf & _
                       "Show Font" & vbCrLf & _
                       "Change selected text" & vbCrLf & _
                       "       or all text." & vbCrLf & _
                       "Show Color" & vbCrLf & _
                       "Change BackColor of RTB"
    End Sub
    
    Sub SaveMultipleFiles()
        Dim strOrigDir As String
        Dim strNewDir As String
        Dim varTemp As Variant
        Dim lngIdx As Long
            strNewDir = "D:\Temp\"
            With CommonDialog1
            .Flags = cdlOFNAllowMultiselect Or cdlOFNLongNames Or cdlOFNExplorer
            .ShowSave
            varTemp = Split(.FileName, vbNullChar)
        End With
            If IsArray(varTemp) Then
            If UBound(varTemp) = 0 Then
                FileCopy varTemp(lngIdx), strNewDir & Right$(varTemp(lngIdx), Len(varTemp(lngIdx)) - InStrRev(varTemp(lngIdx), "\"))
            Else
                strOrigDir = varTemp(0)
                For lngIdx = LBound(varTemp) + 1 To UBound(varTemp)
                    FileCopy strOrigDir & "\" & varTemp(lngIdx), strNewDir & varTemp(lngIdx)
                Next
            End If
        End If
    End Sub
    You can Dim x as Form and then set it.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured