为表单创建一条可选字母

时间:2008-12-01 12:57:14

标签: windows winforms

我希望在表单上显示a到z的字母列表。 每个字母都需要可点击,该值作为点击参数传递。 除了创建26个字母并使用每个字母的点击事件之外,有没有人知道快速的方法来做到这一点? 我知道如何加载动态控件等以及如何以这种方式执行。只是想知道是否有人知道这样做的聪明方法?

干杯

3 个答案:

答案 0 :(得分:1)

这是我将要做的“动态方式”。我知道你要求其他聪明的方法来实现它,但我认为这是最容易接受的方式。 这将生成这些按钮并添加一个单击处理程序,将按钮作为发送方。如果在窗体宽度之外,它还会看到按钮位置包装。

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

答案 1 :(得分:1)

您可以使用FlowLayoutPanel和这样的循环:

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);
}

答案 2 :(得分:0)

在控件上绘制一个字符串,然后将鼠标单击与表单上的字符位置匹配。它实际上比听起来更容易(这是根据MeasureCharacterRanges的标准文档改编的,它简化了委托任务)。该示例是在表单上绘制的,将其转换为用户控件非常简单。

在此示例中,单击一个字母将显示一个消息框,告诉您刚刚单击了哪个字母。

希望它有所帮助。

P.S。请原谅“魔术数字”,例如“已知”阵列中将有25个项目,毕竟这只是一个样本:)

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