高中課的作業,試了很多東西,查了很多東西,就是搞不定!任務是制作一個魔法詞,無論用戶想要它是什么。有點亂,但是我想學!任何建議都會很棒!我已經嘗試了下面代碼中的內容,但我不知道如何指定將其添加到標簽的開頭,分配是有一個標簽,并且有能夠在文本框中添加字符的按鈕到標簽的開頭、中間和結尾。截止日期為 10/20 星期三,因此,如果您對 Visual Basic 有所了解,我們將不勝感激。謝謝!
這是我嘗試過的!它只向標簽添加一次字串字符,但不會再次添加,這是我嘗試添加到開頭但尚未嘗試添加到中間和結尾的唯一代碼。
Dim MagicLetter As String
Dim NewString As String
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
MagicLetter = TextBox1.Text
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
NewString = Len(Label2.Text)
NewString = Mid(MagicLetter, 1, 0)
NewString = MagicLetter.Insert(1, 0)
If MagicLetter = TextBox1.Text Then
NewString = Mid(MagicLetter, 1, 1)
End If
Label3.Text = "Magic Word: " & MagicLetter
NewString = MagicLetter & Label2.Text
uj5u.com熱心網友回復:
問題出在這里
NewString = Len(Label2.Text)
NewString = Mid(MagicLetter, 1, 0)
NewString = MagicLetter.Insert(1, 0)
你在這里做的是你在同一個變數中寫入 3 次,NewString所以最后只有最后一個值NewString = MagicLetter.Insert(1, 0)在變數中,因為之前的 2 被下一個覆寫了。所以如果你洗掉前 2 行,這三行仍然會做同樣的事情。
那么你不需要任何全域變數:
Dim MagicLetter As String
Dim NewString As String
您可以使用程序中的區域變數來完成Button1_Click。如果可以,請始終使用區域變數而不是全域變數。
此外,您不需要該TextBox1_TextChanged事件,因為您對此文本框的每次更改都不感興趣。當您單擊按鈕時,您只想知道其內容。
所以我們可以在Button1_Click程式中做所有事情
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim OriginalText As String
OriginalText = Label3.Text ' here we get the text from the label
Dim MagicLetter As String
MagicLetter = TextBox1.Text ' here we get the magic letter from the textbox
Dim NewText As String
NewText = OriginalText ' our new text should be created from the original text
' now we add the magic letter infront
NewText = MagicLetter & NewText
' now we add the magic letter in the end
NewText = NewText & MagicLetter
' now we add the magic letter in the middle
Dim TextLength As Long
TextLength = Len(NewText) ' here we get the length of our text (we need to split it in the middle)
Dim LeftPart As String
LeftPart = Mid(NewText, 1, CLng(TextLength / 2)) ' here we get the left part of the text
Dim RightPart As String
RightPart = Mid(NewText, Len(LeftPart) 1) ' here we get the right part of the text
' now we add the magic letter between the left and right part
NewText = LeftPart & MagicLetter & RightPart
' finall we write the new text into our label
Label3.Text = NewText
End Sub
uj5u.com熱心網友回復:
Public Class FormMagicWord
Private Function GenerateMagicWord(MagicLetter As Char, Type As String)
'Declare the MagicWord as the label, which is set as just "Magic" in the designer
Dim MagicWord As String = LblMagicWord.Text
'Use a case statement (which is just a cleaner if/else if/else)
Select Case Type
Case "Beginning"
'Combine the MagicLetter and the MagicWord into the MagicWord string.
MagicWord = MagicLetter & MagicWord
Case "Middle"
'Set the initial "midpoint" as 0 in-case the label is empty.
Dim MidPoint As Integer = 0
'Get the middle of the MagicWord string if its length > 0. I used Math.Floor() which will round down to the nearest whole number, so if the length was 9: 9/2 = 4.5 it would round down to 4.
'Alternatively you can use Math.Ceiling() which does the opposite, it rounds up to the next whole number, so if the length was 9: 9/2 = 4.5 it would round up to 5.
'It's cast as an integer (CInt) because we only care about whole numbers for this
If MagicWord.Length > 0 Then
MidPoint = CInt(Math.Floor(MagicWord.Length / 2))
End If
'Insert the MagicLetter at the midpoint of the MagicWord string.
MagicWord = MagicWord.Insert(MidPoint, MagicLetter)
Case "End"
'Combine the MagicWord and the MagicLetter into the MagicWord string.
MagicWord = MagicWord & MagicLetter
Case Else
'Not used, but this would be the "else" equivalent for a Select/Case/Switch statement
End Select
'Return the MagicWord string
Return MagicWord
End Function
'I've changed the handler to manage all three buttons: (BtnBeginning, BtnMiddle, BtnEnd) because the logic is the same for all of them.
'I've also changed the default sender object to Btn as a button, so it explicitly knows what type of control we're handling
Private Sub BtnBeginning_Click(Btn As Button, e As EventArgs) Handles BtnBeginning.Click, BtnMiddle.Click, BtnEnd.Click
'Get the magic letter as a single character, which is all we need.
'The designer also has the max length of the TxtMagicLetter textbox set to 1
Dim MagicLetter As Char = TxtMagicLetter.Text
'Call the GenerateMagicWord function passing the arguments of the letter and the text of the button (Beginning, Middle, End) which will run through the select statement to determine how to format the string
Dim MagicWord As String = GenerateMagicWord(MagicLetter, Btn.Text)
'Finally, set the MagicWord label as the returned string
LblMagicWord.Text = MagicWord
End Sub
End Class
這也是設計器代碼,因此您只需復制/粘貼按鈕/文本框/標簽即可。
以下是訪問設計背后代碼的方法: 在 Visual Studio 2010 中查看設計器代碼
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class FormMagicWord
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.TxtMagicLetter = New System.Windows.Forms.TextBox()
Me.BtnBeginning = New System.Windows.Forms.Button()
Me.BtnMiddle = New System.Windows.Forms.Button()
Me.BtnEnd = New System.Windows.Forms.Button()
Me.LbLMagicLetter = New System.Windows.Forms.Label()
Me.LblMagicWordLabel = New System.Windows.Forms.Label()
Me.LblMagicWord = New System.Windows.Forms.Label()
Me.SuspendLayout()
'
'TxtMagicLetter
'
Me.TxtMagicLetter.Location = New System.Drawing.Point(249, 12)
Me.TxtMagicLetter.MaxLength = 1
Me.TxtMagicLetter.Name = "TxtMagicLetter"
Me.TxtMagicLetter.Size = New System.Drawing.Size(246, 20)
Me.TxtMagicLetter.TabIndex = 0
'
'BtnBeginning
'
Me.BtnBeginning.Location = New System.Drawing.Point(12, 38)
Me.BtnBeginning.Name = "BtnBeginning"
Me.BtnBeginning.Size = New System.Drawing.Size(157, 33)
Me.BtnBeginning.TabIndex = 1
Me.BtnBeginning.Text = "Beginning"
Me.BtnBeginning.UseVisualStyleBackColor = True
'
'BtnMiddle
'
Me.BtnMiddle.Location = New System.Drawing.Point(175, 38)
Me.BtnMiddle.Name = "BtnMiddle"
Me.BtnMiddle.Size = New System.Drawing.Size(157, 33)
Me.BtnMiddle.TabIndex = 2
Me.BtnMiddle.Text = "Middle"
Me.BtnMiddle.UseVisualStyleBackColor = True
'
'BtnEnd
'
Me.BtnEnd.Location = New System.Drawing.Point(338, 38)
Me.BtnEnd.Name = "BtnEnd"
Me.BtnEnd.Size = New System.Drawing.Size(157, 33)
Me.BtnEnd.TabIndex = 3
Me.BtnEnd.Text = "End"
Me.BtnEnd.UseVisualStyleBackColor = True
'
'LbLMagicLetter
'
Me.LbLMagicLetter.AutoSize = True
Me.LbLMagicLetter.Location = New System.Drawing.Point(172, 12)
Me.LbLMagicLetter.Name = "LbLMagicLetter"
Me.LbLMagicLetter.Size = New System.Drawing.Size(66, 13)
Me.LbLMagicLetter.TabIndex = 4
Me.LbLMagicLetter.Text = "Magic Letter"
'
'LblMagicWordLabel
'
Me.LblMagicWordLabel.AutoSize = True
Me.LblMagicWordLabel.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.LblMagicWordLabel.Location = New System.Drawing.Point(8, 141)
Me.LblMagicWordLabel.Name = "LblMagicWordLabel"
Me.LblMagicWordLabel.Size = New System.Drawing.Size(112, 24)
Me.LblMagicWordLabel.TabIndex = 5
Me.LblMagicWordLabel.Text = "Magic Word"
'
'LblMagicWord
'
Me.LblMagicWord.AutoSize = True
Me.LblMagicWord.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.LblMagicWord.Location = New System.Drawing.Point(135, 141)
Me.LblMagicWord.Name = "LblMagicWord"
Me.LblMagicWord.Size = New System.Drawing.Size(0, 24)
Me.LblMagicWord.TabIndex = 6
Me.LblMagicWord.Text = "Magic"
'
'FormMagicWord
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(800, 450)
Me.Controls.Add(Me.LblMagicWord)
Me.Controls.Add(Me.LblMagicWordLabel)
Me.Controls.Add(Me.LbLMagicLetter)
Me.Controls.Add(Me.BtnEnd)
Me.Controls.Add(Me.BtnMiddle)
Me.Controls.Add(Me.BtnBeginning)
Me.Controls.Add(Me.TxtMagicLetter)
Me.Name = "FormMagicWord"
Me.Text = "Magic Word"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents TxtMagicLetter As TextBox
Friend WithEvents BtnBeginning As Button
Friend WithEvents BtnMiddle As Button
Friend WithEvents BtnEnd As Button
Friend WithEvents LbLMagicLetter As Label
Friend WithEvents LblMagicWordLabel As Label
Friend WithEvents LblMagicWord As Label
End Class
uj5u.com熱心網友回復:
Dim magicWord As String = "abcdef"
Label1.Text = $"{TextBox1.Text}{String.Concat(magicWord.Take(magicWord.Length \ 2))}{TextBox1.Text}{String.Concat(magicWord.Skip(magicWord.Length \ 2))}{TextBox1.Text}"
1abc1def1
當magicWord = "abcdefg"(奇數個字符),
1abc1defg1
插入的字串不太在中間,但您的問題中的要求不明確。
這不包括驗證,例如TextBox.Text應該是字符,并且魔術字長度是奇數或偶數。整數除法\用于將整數個字符傳遞給Take和Skip。
這可能無法使用,因為它不使用Mid或Len,但我將其發布以供后代使用
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/322334.html
