Create a strip of selectable letters for a shape
I want to display a list of letters with run z in a form. Each letter must be clickable if this value is passed as a click argument. Apart from creating 26 letters and using the click event of each letter, does anyone know a quick way to do this? I know how to load dynamic controls, etc. And how to do it. Just wondering if anyone knew of a clever way to do this?
Greetings
source to share
You can use FlowLayoutPanel and loop like this:
private void button1_Click(object sender, EventArgs e)
{
flowLayoutPanel1.FlowDirection = FlowDirection.LeftToRight;
flowLayoutPanel1.AutoSize = true;
flowLayoutPanel1.WrapContents = false; //or true, whichever you like
flowLayoutPanel1.Controls.Clear();
for (char c = 'A'; c <= 'Z'; c++)
{
Label letter = new Label();
letter.Text = c.ToString();
letter.AutoSize = true;
letter.Click += new EventHandler(letter_Click);
flowLayoutPanel1.Controls.Add(letter);
}
}
private void letter_Click(object sender, EventArgs e)
{
MessageBox.Show("You clicked on " + ((Label)sender).Text);
}
source to share
This is the "dynamic way" in which I would do it. I know you asked for other clever ways to do this, but I think this is the most acceptable way to do it. This will create these buttons and add a click handler that accepts the button as the sender. It will also see the button layout wrap if outside the width of the forms.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ButtonSize As New Size(20, 20)
Dim ButtonLocation As New Point(10, 20)
For p As Integer = Asc("A") To Asc("Z")
Dim newButton As New Button
If ButtonLocation.X + ButtonSize.Width > Me.Width Then
ButtonLocation.X = 10
ButtonLocation.Y += ButtonSize.Height
End If
newButton.Size = ButtonSize
newButton.Location = ButtonLocation
newButton.Text = Chr(p)
ButtonLocation.X += newButton.Width + 5
AddHandler newButton.Click, AddressOf ButtonClicked
Me.Controls.Add(newButton)
Next
End Sub
Sub ButtonClicked(ByVal sender As Object, ByVal e As System.EventArgs)
MsgBox(CType(sender, Button).Text)
End Sub
End Class
alt text http://img235.imageshack.us/img235/2267/testoa6.jpg
source to share
Draw a line in the control and then map mouse clicks to character positions on the form. This is actually easier than it sounds (this is adapted from the standard documentation on MeasureCharacterRanges, which makes the entrire task easier). The example is drawn on a form, it would be easy enough to do it in a custom control.
In this example, clicking on a letter will bring up a message telling you which letter you just pressed.
Hope it helps.
PS Please excuse the "magic numbers" for example. "Known", there will be 25 elements in the array, this is just a sample in the end :)
Public Class Form1
Const LETTERS As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Private letterRects(25) As System.Drawing.RectangleF
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
Dim index As Integer = -1
Dim mouseP As Point = Me.PointToClient(MousePosition)
For i As Integer = 0 To 25
If letterRects(i).Contains(mouseP.X, mouseP.Y) Then
index = i
Exit For
End If
Next
If index >= 0 Then
MessageBox.Show("Letter = " + LETTERS(index).ToString())
End If
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
' Set up string.
Dim stringFont As New Font("Times New Roman", 16.0F)
' Set character ranges
Dim characterRanges(26) As CharacterRange
For i As Integer = 0 To 25
characterRanges(i) = New CharacterRange(i, 1)
Next
' Create rectangle for layout, measurements below are not exact, these are "magic numbers"
Dim x As Single = 50.0F
Dim y As Single = 50.0F
Dim width As Single = 400.0F
Dim height As Single = 40.0F
Dim layoutRect As New RectangleF(x, y, width, height)
' Set string format.
Dim stringFormat As New StringFormat
stringFormat.FormatFlags = StringFormatFlags.FitBlackBox
stringFormat.SetMeasurableCharacterRanges(characterRanges)
' Draw string to screen.
e.Graphics.DrawString(letters, stringFont, Brushes.Black, _
x, y, stringFormat)
Dim stringRegions() As [Region]
' Measure two ranges in string.
stringRegions = e.Graphics.MeasureCharacterRanges(letters, _
stringFont, layoutRect, stringFormat)
For i As Integer = 0 To 25
letterRects(i) = stringRegions(i).GetBounds(e.Graphics)
Next
End Sub
End Class
source to share