The most important function is
Public Function DynamicallyLoadedObject(ByVal objectName As String, Optional ByVal args() As Object = Nothing) As Object
Dim returnObj As Object = Nothing
Dim type As Type = Assembly.GetExecutingAssembly().GetType(Application.ProductName & "." & objectName)
If Not type Is Nothing Then
returnObj = Activator.CreateInstance(type, args)
End If
Return returnObj
End Function
This function converts form names from text format to form objects. Here is my piece of code generating menu and assigning events to menu click.
Private Sub menuClickedEvent(ByVal sender As Object, ByVal e As EventArgs)
Dim ChildForm As New System.Windows.Forms.Form
ChildForm = DynamicallyLoadedObject(newItem.Tag)
ChildForm.Show()
End Sub
Public Sub CreateMenu(ByVal MyMenuStrip As MenuStrip, Optional ByVal MyToolbar As ToolStrip = Nothing)
Dim MyDB As New LogicPool.DB(_MySettings._DataConnection)
Dim MYDS As New Data.DataSet
MyDB.SQLReturnDT(MYDS, "Menu", "SELECT * FROM SysMenu M, SysUserRight H WHERE H.SysMenuID = M.ID AND H.SysUserID = " & _User._ID & " ORDER BY SortOrder")
MyMenuStrip.Items.Clear()
MenuPrepare(MYDS.Tables("Menu"), MyMenuStrip, MyToolbar)
End Sub
Private Sub MenuPrepare(ByVal db As DataTable, ByVal MS As MenuStrip, Optional ByVal tb1 As System.Windows.Forms.ToolStrip = Nothing, Optional ByVal ref As Integer = 0, Optional ByVal ItemKey As Object = Nothing)
Dim z As Integer
Dim MyDB As New LogicPool.DB(_MySettings._DataConnection)
For z = 0 To db.Rows.Count - 1
' 1. Seviye
If ref = 0 Then
If CInt(db.Rows(z).Item("RefID")) = 0 Then
Dim newItem As New System.Windows.Forms.ToolStripMenuItem
If db.Rows(z).Item("FormName").ToString.Trim.Length > 0 Then
newItem = MS.Items.Add(db.Rows(z).Item("MenuName"), Nothing, New System.EventHandler(AddressOf menuClickedEvent))
newItem.Tag = db.Rows(z).Item("FormName")
Else
newItem = MS.Items.Add(db.Rows(z).Item("MenuName"), Nothing)
End If
If db.Rows(z).Item("ImageName").ToString.Trim.Length > 0 Then
Dim Newimg As System.Drawing.Image
Newimg = FrmImages.imgMenu.Images(db.Rows(z).Item("ImageName").ToString)
newItem.Image = Newimg
End If
newItem.Name = "Mn" & db.Rows(z).Item("ID")
MenuPrepare(db, MS, tb1, db.Rows(z).Item("ID"), newItem)
End If
' Diğer Seviyeler
Else
If CInt(db.Rows(z).Item("RefID")) = ref Then
Dim newItem As New System.Windows.Forms.ToolStripMenuItem
Dim OldItem As System.Windows.Forms.ToolStripMenuItem
OldItem = ItemKey
newItem = OldItem.DropDown.Items.Add(db.Rows(z).Item("MenuName"), Nothing, New System.EventHandler(AddressOf menuClickedEvent))
If db.Rows(z).Item("FormName").ToString.Trim.Length > 0 Then
newItem.Tag = db.Rows(z).Item("FormName")
End If
If db.Rows(z).Item("ImageName").ToString.Trim.Length > 0 Then
Dim Newimg As System.Drawing.Image
Newimg = FrmImages.imgMenu.Images(db.Rows(z).Item("ImageName").ToString)
newItem.Image = Newimg
If Not tb1 Is Nothing Then
If db.Rows(z).Item("Toolbar") = 1 Then
Dim TB As System.Windows.Forms.ToolStripItem
TB = tb1.Items.Add(db.Rows(z).Item("MenuName"), Newimg, New System.EventHandler(AddressOf ToolbarClickEvent))
TB.Tag = db.Rows(z).Item("FormName")
If MyDB.ParameterGet("ToolbarText") = 0 Then
TB.DisplayStyle = ToolStripItemDisplayStyle.Image
Else
TB.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText
End If
TB.ToolTipText = db.Rows(z).Item("MenuName")
tb1.Items.Add(New System.Windows.Forms.ToolStripSeparator)
End If
End If
End If
newItem.Name = "Mn" & db.Rows(z).Item("ID")
MenuPrepare(db, MS, tb1, db.Rows(z).Item("ID"), newItem)
End If
End If
Next
End Sub
After 20 years of software development, I want to share my experiences with the world instead of just a small group of my collegues.
Tuesday, 6 March 2012
Data to Image
This little function converts image data field in database to System.Drawing.Image object
Consider a datatable with an image field named ImgItem, we'll call the function below like this :
Dim MyImg as System.Drawing.Image
MyImg = DataToImage(MyDS.tables("SomeTable").rows(0).Item("ImgItem")
Public Shared Function DataToImage(ByVal DataItem As Object) As System.Drawing.Image
Dim arrImage() As Byte = DirectCast(DataItem, Byte())
Dim ms1 As New System.IO.MemoryStream(arrImage)
Dim origimage As System.Drawing.Image = System.Drawing.Image.FromStream(ms1)
Return origimage
End Function
When It comes to saving
Dim ms As New System.IO.MemoryStream
Me.ImgFoto.Image.Save(ms, ImgFoto.Image.RawFormat)
Dim arrImage() As Byte = ms.GetBuffer
ms.Close()
Now you may use arrImage to save into a datafield.
This is my way of saving. I'll explain this way later.
MyPair.Add("Fotograf", arrImage)
Consider a datatable with an image field named ImgItem, we'll call the function below like this :
Dim MyImg as System.Drawing.Image
MyImg = DataToImage(MyDS.tables("SomeTable").rows(0).Item("ImgItem")
Public Shared Function DataToImage(ByVal DataItem As Object) As System.Drawing.Image
Dim arrImage() As Byte = DirectCast(DataItem, Byte())
Dim ms1 As New System.IO.MemoryStream(arrImage)
Dim origimage As System.Drawing.Image = System.Drawing.Image.FromStream(ms1)
Return origimage
End Function
When It comes to saving
Dim ms As New System.IO.MemoryStream
Me.ImgFoto.Image.Save(ms, ImgFoto.Image.RawFormat)
Dim arrImage() As Byte = ms.GetBuffer
ms.Close()
Now you may use arrImage to save into a datafield.
This is my way of saving. I'll explain this way later.
MyPair.Add("Fotograf", arrImage)
Subscribe to:
Posts (Atom)