我试图让字母在一条线上居中 [英] I am trying to get letters to be centered over a line
问题描述
我试图完成的是让数组中的字母居中在写出的每一行之上,但我目前无法做到这一点,也不确定如何继续.
Private Sub Form1_Load(sender As Object, e As EventArgs) 处理 MyBase.LoadDim understrek(9), bokstav(9), utskrift, bokstUtskrift As String, i As Integerutskrift = ""bokstUtskrift = ""bokstav(0) = "a" : bokstav(1) = "b" : bokstav(2) = "c" : bokstav(3) = "d"bokstav(4) = "e" : bokstav(5) = "f" : bokstav(6) = "g" : bokstav(7) = "h"bokstav(8) = "i" : bokstav(9) = "j"'处理行For Each b In bokstavutskrift = utskrift &___"下一个'处理字母对于 i = 0 到 9bokstUtskrift = bokstUtskrift &" " &博克斯塔夫(一)下一个Label1.Text = utskriftLabel2.Text = bokstUtskrift结束子
这也应该用于刽子手游戏,目的是显示行,以便用户知道单词中有多少个字母.单词本身应该位于线条上方(并以每一行为中心,一行是___"),同时由于用户不知道它而被隐藏.当用户猜出 1 个正确的字母时,应该显示该特定字母,依此类推.
What I am attempting too accomplish is to get the letters that are in the array to be centered above each line that is written out, but I am currently not able to do that and unsure how to proceed.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim understrekk(9), bokstav(9), utskrift, bokstUtskrift As String, i As Integer
utskrift = ""
bokstUtskrift = ""
bokstav(0) = "a" : bokstav(1) = "b" : bokstav(2) = "c" : bokstav(3) = "d"
bokstav(4) = "e" : bokstav(5) = "f" : bokstav(6) = "g" : bokstav(7) = "h"
bokstav(8) = "i" : bokstav(9) = "j"
'Handles the lines
For Each b In bokstav
utskrift = utskrift & " ___"
Next
'Handles the letters
For i = 0 To 9
bokstUtskrift = bokstUtskrift & " " & bokstav(i)
Next
Label1.Text = utskrift
Label2.Text = bokstUtskrift
End Sub
This is also supposed to be used in a hangman game where the purpose is to show the lines so the user knows how many letters there are in the word. The word itself should come above the lines (and centered to each line, a line being the " ___"), while at the same time being hidden since it is unknown to the user. When the user guesses 1 correct letter, that specific letter is supposed to show and so on. This is how the user interface currently looks...
Ok let´s divide the problem into several pieces.
- All functions/subs/variables are located in class Form1
- For the placeholder I chose _
with FontStyle.Underline
option. This will always underline in the correct place. Maybe a blank should also work as a placeholder in this case.
Determine the chars of the hangman word.
Store the result in a class variable so it can be used later when setting the underline placeholder or reveal a correct char.
private _solution as Char()
Private Sub setSolution(word As String)
'Get chars from word and put into class variable
_solution = word.ToArray()
End Sub
Create a table-like panel layout. Each column contains one char:
Private Sub createLayout(word As String)
setSolution(word)
'Create a panel layout with columns to get a proper distribution of chars
Dim table As New TableLayoutPanel()
table.Name = "tblHangmanWord"
table.ColumnCount = _solution.Length
table.RowCount = 1
table.AutoSize = True
table.ColumnStyles.Add(New ColumnStyle(SizeType.AutoSize))
'Adjust the bounds to your needs
table.SetBounds(40, 150, 400, 25)
table.CellBorderStyle = TableLayoutPanelCellBorderStyle.None
'Create labels which display placeholder first, add them to the panel. The array index of _solution correspond to the table columns index.
For i As Integer = 0 To _solution.Length - 1
Dim label As New Label()
label.Name = "lblWord" & i.ToString
label.Text = "___" 'Placeholder
label.AutoSize = True
label.Font = New Font(DefaultFont, FontStyle.Underline)
'Add label to pabel
table.Controls.Add(label)
Next
'Add panel to form
Me.Controls.Add(table)
End Sub
Handle a user´s click on a char button.
Check which chars in the solution match with the button, then reveal the correct ones.
Private Sub btnM_Click(sender As Object, e As EventArgs) Handles btnM.Click
'Get the indexes of the matches of the current button char
Dim idxOfMatches = getIndexOfMatches(DirectCast(sender, Button).Text.First())
'Show the correct chars in the grid
revealMatchingChars(idxOfMatches)
End Sub
Private Function getIndexOfMatches(selected As Char) As Integer()
Dim idxOfMatches As New List(Of Integer)
For i As Integer = 0 To _solution.Length - 1
If selected = _solution(i) Then
idxOfMatches.Add(i)
End If
Next
Return idxOfMatches.ToArray()
End Function
Private Sub revealMatchingChars(idxOfMatches As Integer())
'Get the table panel layout
Dim tbl = Me.Controls.OfType(Of TableLayoutPanel).Where(Function(t) t.Name = "tblHangmanWord").First()
'reveal correct chars
For Each idx As Integer In idxOfMatches
tbl.Controls(idx).Text = _solution(idx)
Next
End Sub
Putting all together and create a test layout:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
createLayout("Millencolin")
End Sub
这篇关于我试图让字母在一条线上居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!