Write a DLL

Erdenemandal

Regular
Joined
Jul 5, 2004
Messages
63
Location
Deutschland
Hi, ALL

I am writing a DLL in VB.NET, But I habe a problem. I want to draw a Graphic

This is my DLL class

Code:
' MyClassLibrary
' 2003/09/04 Copyright 2003, Larry Rebich


Imports System.Drawing

Public Class clsMyClass
        Private mMyName As String

        Property MyName() As String
                Get
                        MyName = mMyName
                End Get
                Set(ByVal Value As String)
                        mMyName = Value
                End Set
        End Property

        Public Function GetDate() As String
                ' Return today's date
                Return "Today is " & Today
        End Function

        Public Sub DrawResult(ByRef Obj As Windows.Forms.Panel, _
                 ByRef TotalHit As Integer, _
                 ByRef RightHit As Integer, _
                 ByRef FormObj As Windows.Forms.TabPage)

                Obj.Visible = False

                ' Get a Graphics object from the current form and clear its background.

                Dim gr As Graphics = FormObj.CreateGraphics

                Dim mPen As New Pen(Color.White, 2)
                Dim mPen2 As New Pen(Color.RoyalBlue.Black, 1)

                Dim TotalProzent, Total2 As Integer


                Dim rr As New Rectangle(Obj.Left, Obj.Top, Obj.Width, Obj.Height)
                gr.FillRectangle(Brushes.White, rr)
                

                TotalProzent = Int(RightHit / TotalHit * 360)

                ' Draw a green filled rectangle.

                Dim x, y, mWidth, mHeight, StartAngle, SweepAngle As Integer

                x = Obj.Left + 10
                y = Obj.Top + 10
                mHeight = Obj.Height - 20
                mWidth = Obj.Width - 20

                ' Draw a red pie (portion of an ellipse).
                Dim i, j As Integer
                For i = 0 To TotalProzent
                        gr.FillPie(Brushes.Blue, x, y, mWidth, mHeight, 0, i)
                Next


                gr.DrawPie(mPen2, x, y, mWidth, mHeight, 0, TotalProzent)

                Total2 = 360 - TotalProzent

                For i = 0 To Total2
                        gr.FillPie(Brushes.Red, x, y, mWidth, mHeight, TotalProzent, i)
                Next

                gr.DrawPie(mPen2, x, y, mWidth, mHeight, TotalProzent, Total2)
                gr.Dispose()
                
        End Sub
End Class

and I generate DLL and use it ni another form.

Code:
Public Class frmMySampleUseClassLibraryProject

        Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

        Public Sub New()
                MyBase.New()

                'This call is required by the Windows Form Designer.
                InitializeComponent()

                'Add any initialization after the InitializeComponent() call

        End Sub

        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
                If disposing Then
                        If Not (components Is Nothing) Then
                                components.Dispose()
                        End If
                End If
                MyBase.Dispose(disposing)
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
        Friend WithEvents Label1 As System.Windows.Forms.Label
        Friend WithEvents Label2 As System.Windows.Forms.Label
        Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
        Friend WithEvents TabControl1 As System.Windows.Forms.TabControl
        Friend WithEvents TabPage1 As System.Windows.Forms.TabPage
        Friend WithEvents Panel1 As System.Windows.Forms.Panel
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
                Me.TextBox1 = New System.Windows.Forms.TextBox
                Me.Label1 = New System.Windows.Forms.Label
                Me.Label2 = New System.Windows.Forms.Label
                Me.TextBox2 = New System.Windows.Forms.TextBox
                Me.TabControl1 = New System.Windows.Forms.TabControl
                Me.TabPage1 = New System.Windows.Forms.TabPage
                Me.Panel1 = New System.Windows.Forms.Panel
                Me.TabControl1.SuspendLayout()
                Me.TabPage1.SuspendLayout()
                Me.SuspendLayout()
                '
                'TextBox1
                '
                Me.TextBox1.Location = New System.Drawing.Point(96, 16)
                Me.TextBox1.Name = "TextBox1"
                Me.TextBox1.ReadOnly = True
                Me.TextBox1.Size = New System.Drawing.Size(328, 20)
                Me.TextBox1.TabIndex = 0
                Me.TextBox1.Text = "TextBox1"
                '
                'Label1
                '
                Me.Label1.AutoSize = True
                Me.Label1.Location = New System.Drawing.Point(40, 24)
                Me.Label1.Name = "Label1"
                Me.Label1.Size = New System.Drawing.Size(38, 16)
                Me.Label1.TabIndex = 1
                Me.Label1.Text = "Name:"
                Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight
                '
                'Label2
                '
                Me.Label2.AutoSize = True
                Me.Label2.Location = New System.Drawing.Point(40, 56)
                Me.Label2.Name = "Label2"
                Me.Label2.Size = New System.Drawing.Size(31, 16)
                Me.Label2.TabIndex = 3
                Me.Label2.Text = "Date:"
                Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight
                '
                'TextBox2
                '
                Me.TextBox2.Location = New System.Drawing.Point(96, 48)
                Me.TextBox2.Name = "TextBox2"
                Me.TextBox2.ReadOnly = True
                Me.TextBox2.Size = New System.Drawing.Size(328, 20)
                Me.TextBox2.TabIndex = 2
                Me.TextBox2.Text = "TextBox2"
                '
                'TabControl1
                '
                Me.TabControl1.Controls.Add(Me.TabPage1)
                Me.TabControl1.Location = New System.Drawing.Point(32, 104)
                Me.TabControl1.Name = "TabControl1"
                Me.TabControl1.SelectedIndex = 0
                Me.TabControl1.Size = New System.Drawing.Size(416, 200)
                Me.TabControl1.TabIndex = 4
                '
                'TabPage1
                '
                Me.TabPage1.Controls.Add(Me.Panel1)
                Me.TabPage1.Location = New System.Drawing.Point(4, 22)
                Me.TabPage1.Name = "TabPage1"
                Me.TabPage1.Size = New System.Drawing.Size(408, 174)
                Me.TabPage1.TabIndex = 0
                Me.TabPage1.Text = "TabPage1"
                '
                'Panel1
                '
                Me.Panel1.BackColor = System.Drawing.SystemColors.ControlText
                Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
                Me.Panel1.Location = New System.Drawing.Point(24, 16)
                Me.Panel1.Name = "Panel1"
                Me.Panel1.Size = New System.Drawing.Size(360, 144)
                Me.Panel1.TabIndex = 0
                '
                'frmMySampleUseClassLibraryProject
                '
                Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
                Me.ClientSize = New System.Drawing.Size(464, 314)
                Me.Controls.Add(Me.TabControl1)
                Me.Controls.Add(Me.Label2)
                Me.Controls.Add(Me.TextBox2)
                Me.Controls.Add(Me.Label1)
                Me.Controls.Add(Me.TextBox1)
                Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
                Me.MaximizeBox = False
                Me.Name = "frmMySampleUseClassLibraryProject"
                Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
                Me.Text = "frmMySampleUseClassLibraryProject"
                Me.TabControl1.ResumeLayout(False)
                Me.TabPage1.ResumeLayout(False)
                Me.ResumeLayout(False)

        End Sub

#End Region

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                UseMyClass()

        End Sub

        Private Sub UseMyClass()

                Dim yy1, yy2 As Integer

                yy1 = 100
                yy2 = 50


                Dim obj As New MyClassLibrary.clsMyClass
                With obj
                        .MyName = "Larry Rebich"
                        Me.TextBox1.Text = .MyName
                        Me.TextBox2.Text = .GetDate()
                        .DrawResult(Panel1, yy1, yy2, Me.TabControl1.TabPages(0))
                End With

        End Sub
End Class


Everything works fine with String and Integer type. But I can not see any Graphic ni my new form

Can somebody help me

Thanks,
Erdenemandal
 
You are calling the drawing routine during the Form's load event, when none of the controls is yet visible. When they do become visible, they will be redrawn in their original appearance since GDI+ does not keep the drawn modifications on controls. Try drawing on your control when the form is visible. But even then, if the control needs to be redrawn, it will loose any appearance modifications, so the best way to keep something drawn on the form/control is to draw it in the Paint event of the object.
 
I seem to remember that when you draw to a picturebox, the graphics are retained and automatically redrawn when for example another form covered your form and is then minimized...
 
Back
Top