A demo using user controls (including components).

xiaoxiao2021-03-05  24

1.

Imports system.componentmodelpublic class pic inherits system.windows.forms.userControl

#Region "Windows Form Designer Generated Code"

'UserControl1 Rewinds Dispose to clean the component list. Protected overloads overrides sub dispose (byval disposing as boolean) ing disponation kiln (Components Is Nothing) Then components.dispose () end if endiffs) End sub

'Windows Form Designer Supply Private Components as System.comPonentModel.icontainer

'Note: The following procedure is necessary to use the Windows Form Designer to modify this process using the Windows Form Designer. 'Don't modify it using the code editor. private subinitiRizeComponent () 'pic' me.name = "pic" me.size = new system.drawing.size (48, 48)

End Sub

#End region

Public const m_maxlen as integer = 48 'fixed wide and high public const m_maxheight as integer = 48 Public Sub New (BYVAL M As Image) "Mainly used to use mybase.new when you create instances in the PicControls component

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

'Add any initialization m_image = m = m_im () mybase.new () in initializeComponent ()

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

'Add any initialization after INITIALIZECOMPONENT ()

End Sub

Private m_image as image = image.fromfile ("G: / Exercise / Important Rollers / Using Issues (Pictures in Plaid) / Gounda Takeshi.ico") _ Public property () as image get return m_image end set (byval value as image) m_image = value me.refresh () end set endprote 'Draw Border and Image Protected Overrides Sub onpaint (Byval E AS system.windows.Forms.Painteventargs) Dim g as graphics = me.creategraphics me.backcolor = color.white g.drawRectangle (system.drawing.pens.black, 0, 0, me.width - 1, me.height - 1) DIM IC as image = ctype (m_image, image) g.drawImage (IC, 0, 0) End Sub 'does not allow adjustment Size Protected Overrides Sub onsizechanged (Byval E ASTEM.EventArgs) me.size = new size (m_maxlen , m_maxheight) End sub 'matching whether the private m_double as boolean = false _ public property doubles () as boolean get return m_ Double End Get Set (BYVAL VALUE AS BOOLEAN) M_DOUBLE = VALUE End End Property Private M_ID AS Integer _ Public property id () AS integer get return m_id End get set (Byval value as integer) m_id = value end versaver End Class2.

Imports my_namespaceimports system.componentmodelpublic class piccontrols inherits system.componentmodel.component

#REGION "Component Designer Generated Code"

'Component override Dispose to clean 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 'designer component required Private components As System .Componentmodel.icontainer

'Note: The following procedure is that the component designer must modify this process using the Component Designer. 'Don't modify it using the code editor. private subinitializecomponent () Components = new system.componentmodel.container () End Sub

#End region public subnewele (byval container as system.componentmodel.icontainer) myclass.new ()

'Windows.Forms Class Writing Designer Support for Container.Add (ME)

ChangePic () 'If the selected picture has changed, then the creation of the card set should also change accordingly.

End Sub

Public Sub new () mybase.new ()

'This call is necessary for the component designer. InitializeComponent ()

'Add any initialization after INITIALIZECOMPONENT ()

ChangePic () 'If the selected picture has changed, then the creation of the card set should also change accordingly.

End Sub '

Dim m_piccontrols as new system.collections.ArrayList () 'total number _ Public readonly property count () AS integer get return m_PicControls.count end get Get End Property 'Specified Location Card (This for VB.NET is a relatively special, with parametric attribute) Default public readonly proty tem GET IF INDEX> = 0 and index

Public Sub shuffle () 'mixed card, that is, generated a set of random card sets. (Here the algorithm!) Dim r as new system.random () DIM D AS New System.Collections.ArrayList () Dim P AS Pic While (m_piccontrols.count> 0) Dim RemoveIndex as integer = r.next (0, m_piccontrols.Count - 1) p = CType (m_piccontrols (removeindex), my_namespace.pic) m_piccontrols.RemoveAt (removeindex) d.Add (p) End While m_piccontrols = d End SubPrivate m_image As imagelist _ Public property imagelist () as imagelist get return m_image end set (byval value as imagelist) m_image = value changePic () End set end entry '/' This event is more important, mainly It is based on the change of the picture to generate different card sets. Private sub chatpic () IF m_IMAGE IS NOTHING THEN EXIT SUB DIM I AS INTEGER for i = 0 to m_piccontrols.count - 1 CType (M_PicControls (i), PIC) .dispose () 'Note here. Next m_piccontrols.clear () DIM J AS INTEGER for i = 0 to m_image.images.count - 1 for j = 0 to 3 DIM P AS New Pic (m_image.images (i)) P.ID = I m_piccontrols.add ( p) NEXT NEXT End Sub 'Since the Doubles property of each card in the collection is set to true after being arranged, then set all the cards to set all the cards in False Public SUB setfalse. () DIM I as integer for i = 0 to m_piccontrols.count - 1 DIM APIC AS PIC = CTYPE (M_PicControls (i), PIC) APIC.DouBles = false Next End Sub

END CLASS

3.

Imports System.comPonentmodelpublic Class PicShow Inherits System.Windows.Forms.userControl # Region "Windows Form Designer Generated Code"

Public Sub new () mybase.new ()

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

'Add any initialization after INITIALIZECOMPONENT ()

End Sub

'UserControl overrides Dispose to clean the component list. Protected overloads overrides sub dispose (byval disposing as boolean) ing disponation kiln (Components Is Nothing) Then components.dispose () end if endiffs) End sub

'Windows Form Designer Supply Private Components as System.comPonentModel.icontainer

'Note: The following procedure is necessary to use the Windows Form Designer to modify this process using the Windows Form Designer. 'Don't modify it using the code editor. private subinitializecomponent () Components = new system.componentmodel.container () End Sub

#End region '// This program Principle // First make a PIC control, you can set the corresponding picture, do not allow the size, to override SizeChange, onpait event //' // to do a collection component PicControls to accommodate a certain number of PIC cards but do not display it because it is a component. Just the container // '// finally makes a PICSHOW control to display the number of cards for the number of PicControls.count. // '// More important places are how to carry out randomized mixing (SHUFFLE methods of PicControls) and PICSHOW controls // contrains, start methods. Always pay attention to the order of sorting: Yes, it is blended (randomly) in the collection, so // '// we have obtained is random, then in the PicShow control according to each card Doubles The id attribute is to perform // '// sort, separate random and sorting. Of course, they can also merge them into the PICSHOW control. But here is not recommended. // '// Because for the PicControls component, its collection is a randomly generated card collection. This is better understood. //

Private const m_spacing as integer = 10 'Interval Setting constant private m_rows as integer = 2' For an array, 2 lines should be more meaningful. _ Public Property Row () AS Integer Get Return M_Rows End Set (Byval Value As Integer) ing value> 0 Then M_ROWS = Value me.refresh () end if End vendy private m_columns as integer = 2 _ Public Property Column () AS Integer Get Return M_Columns End Get Set (Value> 0) and (Value Mod 2 = 0) Then M_Columns = Value Me.Refresh () Else Throw new exception ("Not a valid column value! Please enter 2 column value multiples. ") End If End Set End Property Private m_collection As piccontrols _ Public Property getcontrols () As piccontrols Get Return m_collection End GetSet (Byval Value As Piccontrols) m_collection = value end set end version The border is drawn, since the card has not been added to the Me.Controls collection, so there is only a border.

Protected Overrides Sub OnPaint (ByVal e As System.Windows.Forms.PaintEventArgs) Dim height As Integer = my_namespace.pic.m_maxheight Dim width As Integer = my_namespace.pic.m_maxlen Me.Width = (width m_spacing) * m_columns m_spacing Me .Height = (height m_spacing) * m_rows m_spacing Dim g As Graphics = Me.CreateGraphics Dim row, column As Integer For row = 0 To m_rows - 1 For column = 0 To m_columns - 1 g.DrawRectangle (System.Drawing. Pens.gray, column * (width m_spacing) m_spacing, _ row * (Height m_spacing) m_spacing, width, height) Next Next End subprivate m_double as pic 'record the same card

Private m_last as integer 'records the last card in the lattice

'Starting public sub start ()

Me.Controls.clear () 'First empty container

If not isnothing (m_collection) THEN 'Judgment Rat Column The number of the cards is equal to if (m_collection.count <> m_rows * m_column) THROW New Exception ("The number of cards:" & cstr (m_collection.count) & "grid quantity To: "& CSTR (m_rows * m_columns) Endiff

'///

M_last = -2 'initialization, because starting from 0 to the first lattice, so the initial value is -2

m_collection.setfalse () 'Because you start sorting, you will set all the Double properties of all card PICs to True. So, here you have to set back to False

m_collection.shuffle () 'Mix the card

DIM cardcount as integer = 0 'card pointer DIM ROW, Column As INTEGER

For row = 0 to m_rows - 1 for column = 0 To m_columns - 1 DIM APIC APIC APIC APIC AS PIC = CTYPE (m_collection (cardcount), PIC) 'Add to ME Control Set Me.Controls.Add (APIC)' Control Collection The original card is traversed, see if there is a single card DIM RINT AS INTEGER = Contrains (APIC) SELECT CASE RINT CASE 0 'matching arrangement APIC.PIC = M_DOUBLE.PIC .m_maxlen apic.top = m_double.top case 1 'No match, interval arrangement

'Opening, pay attention must be used to transform using int, otherwise it will be rounded. DIM R AS INTEGER = INT (m_last / (m_rows)) 'column DIM C AS INTEGER = M_LAST MOD (M_ROWS)' After obtaining the ranks, you can set the position directly. APIC.LEFT = C * (pic.m_maxlen m_spacing) m_spacing apic.top = r * (pic.m_maxheight m_spacing) m_spacing

End Select CardCount = 1 'Next Card Next Next MessageBox.show ("Sorted!")

End if End Sub 'Sorted Functions Public Function Contrains (Byval P AS PIC) AS Integer M_Double = Nothing' The initial value is empty, and set to empty each time you sort. DIM APIC AS PIC DIM I AS INTEGER DIM Count As integer = me.controls.count - 1 for i = 0 to count - 1 'From 0 to the end - 1, remove yourself, you don't have to compare APIC = CType (Me.Controls (i), PIC) IF (APIC.ID = P.ID) and apic.doubles = false the 'and i <> count apic.doubles = true' matching p.douBles = true 'matches m_double = APIC END IF NEXT 'found the matching IF not (m_double is nothing) Then Return 0 else m_last = 2' separated a lattice (here the lattice starts to be 0, and finally m_collection count-1) Return 1 End IFend Functionend Class

转载请注明原文地址:https://www.9cbs.com/read-33576.html

New Post(0)