代码之家  ›  专栏  ›  技术社区  ›  anonym0use

为表单创建一条可选择的字母

  •  1
  • anonym0use  · 技术社区  · 17 年前

    我希望在表单上显示从a到z的字母列表。 除了创建26个字母和使用每个字母的点击事件之外,有人知道一种快速的方法吗?

    干杯

    3 回复  |  直到 17 年前
        1
  •  1
  •   Stefan    17 年前

    这就是我要用的“动态方式”。我知道你要求用其他聪明的方法来做这件事,但我认为这是最被接受的方法。 这将生成这些按钮,并添加一个将按钮作为发送者的单击处理程序。它还将确保按钮位置在表单宽度之外进行换行。

    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

        2
  •  1
  •   Tomalak    17 年前

    您可以使用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);
    }
    
        3
  •  0
  •   Binary Worrier    17 年前

    在控件上绘制字符串,然后将鼠标单击与窗体上的字符位置相匹配。这实际上比听起来容易(这是根据MeasureCharacterRanges标准文档改编的,简化了Entire任务)。这个例子是在一个表单上绘制的,很简单,可以把它变成一个用户控件。

    在本例中,单击一封信将显示一个messagebox,告诉您刚才单击的是哪封信。

    另外,请原谅“幻数”,例如“知道”,数组中有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