nepalbinod
Sep 9th, 2007, 02:42 AM
Hi friends,
During my programming hours, I found so many things that consumed a lot of time but it did not include any skills. So, for fast and rapid development, I have created some functions that I use as code generators. They include codes which are basically used on all of the forms and I thought it might be interesting. If you, too, have such generators, please let us all know.
'This function generates code for highlighting all the controls of a form ...
Public Sub CodeGen_HighLightAllControlsOnFocus(ByVal theForm As Form)
On Error Resume Next
Dim Sttr As String
For Each MyControl In theForm.Controls: DoEvents
If TypeOf MyControl Is TextBox Then
Sttr = Sttr & vbNewLine & HighLightAssistant(MyControl)
ElseIf TypeOf MyControl Is ComboBox Then
Sttr = Sttr & vbNewLine & HighLightAssistant(MyControl)
ElseIf TypeOf MyControl Is ListBox Then
Sttr = Sttr & vbNewLine & HighLightAssistant(MyControl)
ElseIf TypeOf MyControl Is MSFORms.TextBox Then
Sttr = Sttr & vbNewLine & HighLightAssistant(MyControl)
ElseIf TypeOf MyControl Is MSFORms.ComboBox Then
Sttr = Sttr & vbNewLine & HighLightAssistant(MyControl)
ElseIf TypeOf MyControl Is MSFORms.ListBox Then
Sttr = Sttr & vbNewLine & HighLightAssistant(MyControl)
End If
Next
frmcodegen.TextBox1.Text = Sttr
ShowForm frmcodegen
'Create a new form frmcodegen and put a large textbox with multiline enabled.
End Sub
Function HighLightAssistant(ByVal MyControl As Control) As String
Dim sttr1 As String
sttr1 = sttr1 & vbNewLine & "Private Sub " & MyControl.Name & "_GotFocus()" & vbNewLine & " " & "HighLight " & MyControl.Name & vbNewLine & "End Sub" & vbNewLine
sttr1 = sttr1 & vbNewLine & "Private Sub " & MyControl.Name & "_LostFocus()" & vbNewLine & " " & "UnHighLight " & MyControl.Name & vbNewLine & "End Sub" & vbNewLine
HighLightAssistant = sttr1
End Function
Public Sub ShowForm(ByVal theForm As Form)
On Error GoTo ErrorHandler
If theForm.Visible <> True Then
theForm.Show vbModal
End If
Exit Sub
ErrorHandler:
Msgbox Err.Number & vbNewLine & Err.Description
End Sub
'This function generates code for sending tab keys when the Enter button is pressed on controls
Public Sub CodeGen_GetKeyDownLines(ByVal theForm As Form)
Dim Sttr As String
For Each MyControl In theForm.Controls: DoEvents
If TypeOf MyControl Is TextBox Then
Sttr = Sttr & vbNewLine & GetKeyDownLine(MyControl)
ElseIf TypeOf MyControl Is ComboBox Then
Sttr = Sttr & vbNewLine & GetKeyDownLine(MyControl)
ElseIf TypeOf MyControl Is ListBox Then
Sttr = Sttr & vbNewLine & GetKeyDownLine(MyControl)
ElseIf TypeOf MyControl Is MSFORms.TextBox Then
Sttr = Sttr & vbNewLine & GetKeyDownLine(MyControl)
ElseIf TypeOf MyControl Is MSFORms.ComboBox Then
Sttr = Sttr & vbNewLine & GetKeyDownLine(MyControl)
ElseIf TypeOf MyControl Is MSFORms.ListBox Then
Sttr = Sttr & vbNewLine & "asfd" & GetKeyDownLine(MyControl)
End If
Next
frmcodegen.TextBox1.Text = Sttr
ShowForm frmcodegen
End Sub
Function GetKeyDownLine(ByVal MyControl As Control) As String
Dim MyStrrr As String
MyStrrr = "Private Sub " & MyControl.Name & "_KeyDown(KeyCode As Integer, Shift As Integer)"
MyStrrr = MyStrrr & vbNewLine & " If KeyCode = 13 Then SendKeys vbTab" & vbNewLine & "End Sub" & vbNewLine
GetKeyDownLine = MyStrrr
End Function
'This function generates code for sizing a grid's width. Just execute the code, size the grid, call this function. Depending upon the current size of the grid, code will be generated which can be pasted directly into the form as required. Here, grid refers to MSHFlexGrid
Public Sub CodeGen_GRIDSIZE(ByVal MYGRID As MSHFlexGrid)
Dim mytext As String
Dim myrows As Integer
Dim mycols As Integer
Dim i As Integer
Dim j As Integer
mycols = MYGRID.Cols - 1
mytext = MYGRID.Name & ".colwidth(0)=300" & vbNewLine
For i = 1 To mycols: DoEvents
mytext = mytext & vbNewLine & MYGRID.Name & ".colwidth(" & i & ")=" & MYGRID.ColWidth(i)
Next i
frmcodegen.TextBox1.Text = mytext
ShowForm frmcodegen
End Sub
During my programming hours, I found so many things that consumed a lot of time but it did not include any skills. So, for fast and rapid development, I have created some functions that I use as code generators. They include codes which are basically used on all of the forms and I thought it might be interesting. If you, too, have such generators, please let us all know.
'This function generates code for highlighting all the controls of a form ...
Public Sub CodeGen_HighLightAllControlsOnFocus(ByVal theForm As Form)
On Error Resume Next
Dim Sttr As String
For Each MyControl In theForm.Controls: DoEvents
If TypeOf MyControl Is TextBox Then
Sttr = Sttr & vbNewLine & HighLightAssistant(MyControl)
ElseIf TypeOf MyControl Is ComboBox Then
Sttr = Sttr & vbNewLine & HighLightAssistant(MyControl)
ElseIf TypeOf MyControl Is ListBox Then
Sttr = Sttr & vbNewLine & HighLightAssistant(MyControl)
ElseIf TypeOf MyControl Is MSFORms.TextBox Then
Sttr = Sttr & vbNewLine & HighLightAssistant(MyControl)
ElseIf TypeOf MyControl Is MSFORms.ComboBox Then
Sttr = Sttr & vbNewLine & HighLightAssistant(MyControl)
ElseIf TypeOf MyControl Is MSFORms.ListBox Then
Sttr = Sttr & vbNewLine & HighLightAssistant(MyControl)
End If
Next
frmcodegen.TextBox1.Text = Sttr
ShowForm frmcodegen
'Create a new form frmcodegen and put a large textbox with multiline enabled.
End Sub
Function HighLightAssistant(ByVal MyControl As Control) As String
Dim sttr1 As String
sttr1 = sttr1 & vbNewLine & "Private Sub " & MyControl.Name & "_GotFocus()" & vbNewLine & " " & "HighLight " & MyControl.Name & vbNewLine & "End Sub" & vbNewLine
sttr1 = sttr1 & vbNewLine & "Private Sub " & MyControl.Name & "_LostFocus()" & vbNewLine & " " & "UnHighLight " & MyControl.Name & vbNewLine & "End Sub" & vbNewLine
HighLightAssistant = sttr1
End Function
Public Sub ShowForm(ByVal theForm As Form)
On Error GoTo ErrorHandler
If theForm.Visible <> True Then
theForm.Show vbModal
End If
Exit Sub
ErrorHandler:
Msgbox Err.Number & vbNewLine & Err.Description
End Sub
'This function generates code for sending tab keys when the Enter button is pressed on controls
Public Sub CodeGen_GetKeyDownLines(ByVal theForm As Form)
Dim Sttr As String
For Each MyControl In theForm.Controls: DoEvents
If TypeOf MyControl Is TextBox Then
Sttr = Sttr & vbNewLine & GetKeyDownLine(MyControl)
ElseIf TypeOf MyControl Is ComboBox Then
Sttr = Sttr & vbNewLine & GetKeyDownLine(MyControl)
ElseIf TypeOf MyControl Is ListBox Then
Sttr = Sttr & vbNewLine & GetKeyDownLine(MyControl)
ElseIf TypeOf MyControl Is MSFORms.TextBox Then
Sttr = Sttr & vbNewLine & GetKeyDownLine(MyControl)
ElseIf TypeOf MyControl Is MSFORms.ComboBox Then
Sttr = Sttr & vbNewLine & GetKeyDownLine(MyControl)
ElseIf TypeOf MyControl Is MSFORms.ListBox Then
Sttr = Sttr & vbNewLine & "asfd" & GetKeyDownLine(MyControl)
End If
Next
frmcodegen.TextBox1.Text = Sttr
ShowForm frmcodegen
End Sub
Function GetKeyDownLine(ByVal MyControl As Control) As String
Dim MyStrrr As String
MyStrrr = "Private Sub " & MyControl.Name & "_KeyDown(KeyCode As Integer, Shift As Integer)"
MyStrrr = MyStrrr & vbNewLine & " If KeyCode = 13 Then SendKeys vbTab" & vbNewLine & "End Sub" & vbNewLine
GetKeyDownLine = MyStrrr
End Function
'This function generates code for sizing a grid's width. Just execute the code, size the grid, call this function. Depending upon the current size of the grid, code will be generated which can be pasted directly into the form as required. Here, grid refers to MSHFlexGrid
Public Sub CodeGen_GRIDSIZE(ByVal MYGRID As MSHFlexGrid)
Dim mytext As String
Dim myrows As Integer
Dim mycols As Integer
Dim i As Integer
Dim j As Integer
mycols = MYGRID.Cols - 1
mytext = MYGRID.Name & ".colwidth(0)=300" & vbNewLine
For i = 1 To mycols: DoEvents
mytext = mytext & vbNewLine & MYGRID.Name & ".colwidth(" & i & ")=" & MYGRID.ColWidth(i)
Next i
frmcodegen.TextBox1.Text = mytext
ShowForm frmcodegen
End Sub