2008-12-01 26 views
1

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

干杯

回答

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

正是我所需要的,为此欢呼! – anonym0use 2008-12-01 14:20:26

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

+0

+1为进一步英里和做位置数学。 :) FlowLayoutPanel可能并不总是一个选项。 – Tomalak 2008-12-01 14:24:15

+0

我喜欢FlowLayoutPanel,但在编写简短示例时,我希望尽可能少地使用控件。 ;) – Stefan 2008-12-01 14:25:56

0

画上一个控件的字符串,然后匹配鼠标点击窗体上的字符位置。它实际上比听起来容易(这是从MeasureCharacterRanges的标准文档中调整的,它简化了entrire任务)。这个例子是在一个表单上绘制的,将它变成一个用户控件就足够简单了。

在这个例子中,点击一个字母会导致一个信息框出现,告诉你刚才点击了哪个字母。

希望它有帮助。

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