Click here to Skip to main content
15,895,011 members
Articles / Desktop Programming / Windows Forms

Shape Control for .NET

Rate me:
Please Sign up or sign in to vote.
4.84/5 (170 votes)
23 Mar 2017CPOL10 min read 381.4K   21.7K   301  
Implementing shape control that supports transparency, custom design-time editors and simple animation
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms

Public Partial Class Form4a
	Inherits Form
	Private hints As String
	Private options As String
	Private ctrllist As List(Of ShapeControl.CustomControl1)
	Private num_correct As Integer = 0
	Private num_total As Integer = 8
	Private num_choosen As Integer = 0

	Public Sub New()
		InitializeComponent()

        options = Global.TestShapeControl.My.Resources.ResourceManager.GetString("Options")
        hints = Global.TestShapeControl.My.Resources.ResourceManager.GetString("Hints")
		Dim vhints = hints.Split("|"C)
		Dim vInitials = vhints(0).Split(" "C)
		Dim vNames = vhints(1).Split(";"C)

		Dim v = options.Split(" "C)
		ctrllist = New List(Of ShapeControl.CustomControl1)()
		Dim sx As Integer = customControl11.Location.X
		Dim sy As Integer = customControl11.Location.Y
		Dim gapx As Integer = 70, gapy As Integer = 80
		For i As Integer = 0 To v.Length - 1
			Dim ctrl1 As New ShapeControl.CustomControl1()
			ctrl1.Name = "ctrl_" & i
			ctrl1.Shape = customControl11.Shape
			If customControl11.ShapeImage IsNot Nothing Then
				ctrl1.ShapeImage = customControl11.ShapeImage
			End If
			ctrl1.Size = customControl11.Size
			ctrl1.BorderColor = customControl11.BorderColor
			ctrl1.BackColor = customControl11.BackColor
			ctrl1.BorderWidth = customControl11.BorderWidth
			Dim x As Integer = sx + (i Mod 5) * (ctrl1.Width + gapx)
			Dim y As Integer = sy + (i \ 5) * (ctrl1.Height + gapy)
			ctrl1.Location = New Point(x, y)
			Dim lbl1 As New Label()
			lbl1.Name = "lbl_" & i
			lbl1.AutoSize = True
			lbl1.MaximumSize = New Size(ctrl1.Width, gapy - 20)
			lbl1.TextAlign = ContentAlignment.TopCenter

			' lbl1.BackColor = Color.Blue;
			lbl1.Location = New Point(x, y + ctrl1.Height + 5)

			ctrl1.Text = v(i)
			ctrl1.Tag = "X"
			'default no hit
			ctrl1.Visible = True
			lbl1.Visible = False

            AddHandler ctrl1.MouseClick, New MouseEventHandler(AddressOf ctrl1_MouseClick)
			For j As Integer = 0 To vInitials.Length - 1
				If v(i).ToString() = vInitials(j) Then
					ctrl1.Tag = "" & j
					'store index of the answer
					ctrl1.Tag2 = lbl1.Name

					lbl1.Tag = vNames(j)
					lbl1.Text = lbl1.Tag.ToString()
					Exit For

				End If
			Next
			Controls.Add(ctrl1)
			ctrllist.Add(ctrl1)

			Controls.Add(lbl1)
		Next
	End Sub

	Private Sub ctrl1_MouseClick(sender As Object, e As MouseEventArgs)
		If e.Button = MouseButtons.Left Then
			If DirectCast(sender, ShapeControl.CustomControl1).BorderWidth = 0 Then
				If num_choosen < num_total Then
					DirectCast(sender, ShapeControl.CustomControl1).BorderWidth = 3
					num_choosen += 1
				End If
			Else
				DirectCast(sender, ShapeControl.CustomControl1).BorderWidth = 0
				num_choosen -= 1

			End If
		End If
		' throw new NotImplementedException();
	End Sub

	Private Sub button1_Click(sender As Object, e As EventArgs)

		For i As Integer = 0 To ctrllist.Count - 1
			If ctrllist(i).BorderWidth <> 0 Then
				'choosen
				If ctrllist(i).Tag.ToString() <> "X" Then
					num_correct += 1
					ctrllist(i).BackColor = Color.FromArgb(100, Color.LightGreen)
					Dim c As Control() = Controls.Find(ctrllist(i).Tag2.ToString(), False)
					c(0).Visible = True
					Dim sindex As String = ctrllist(i).Tag.ToString()
					ctrllist(i).BackgroundImageLayout = ImageLayout.Zoom


                    ctrllist(i).BackgroundImage = DirectCast(Global.TestShapeControl.My.Resources.ResourceManager.GetObject("photo" & sindex), Bitmap)
				Else
					ctrllist(i).BackColor = Color.FromArgb(90, Color.Red)
				End If


			End If
		Next
		If num_correct = num_total Then
			label1.Text = "Congratulations, all correct, you rock!"
		Else
			label1.Text = num_correct & " of " & num_total & " correct.."
		End If
		button1.Visible = False

	End Sub
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Singapore Singapore
Coder. Hacker. Fixer.

Comments and Discussions