String Manipulation by hiding and showing - vb.net

The idea is simple, if I have a string value "ABCD" then with a ButtonClick event it should randomly reveal a char while others are hidden. i.e, "B*" another click would "AB**" and so on.
So far, my I have been stuck in a for loop.
For Each c As Char In x
y = Random.Next(0, x.IndexOf(c))
Next
I'm still learning VB.NET at this phase.

Public Class Form1
Private _indexes As Integer()
Private _currentIndex As Integer
Private _chars As Char()
Private _template As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim rnd = New Random()
_template = "ABCD"
' Create indexes that are randomly sorted
_indexes = Enumerable _
.Range(0, _template.Length) _
.OrderBy(Function(i) rnd.Next()) _
.ToArray()
'Create an array of chars with stars '****'.
_chars = New String("*"c, _template.Length).ToCharArray()
_currentIndex = 0
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If _currentIndex < _template.Length Then
Dim index As Integer = _indexes(_currentIndex)
_currentIndex += 1
_chars(index) = _template(index)
Dim result As String = New String(_chars)
Label1.Text = result
End If
End Sub
End Class

I have already posted an answer where I focused on the algorithm. The algorithm code was directly integrated into a form. This works but is not a good practice. The code would be more reusable, more understandable and could be tested more easily if it was extracted to a separate class.
Public Class RandomTextRevealer
Private _indexes As Integer()
Private _currentIndex As Integer
Private _chars As Char()
Private _template As String
Private _result As String
Public Sub New(ByVal templateText As String)
Dim rnd = New Random()
_template = templateText
' Create indexes that are randomly sorted
_indexes = Enumerable _
.Range(0, _template.Length) _
.OrderBy(Function(i) rnd.Next()) _
.ToArray()
'Create an array of chars with stars '****'.
_chars = HiddenText.ToCharArray()
_currentIndex = 0
End Sub
Public Function RevealNext() As String
If _currentIndex < _template.Length Then
Dim index As Integer = _indexes(_currentIndex)
_currentIndex += 1
_chars(index) = _template(index)
_result = New String(_chars)
End If
Return _result
End Function
Public ReadOnly Property HiddenText() As String
Get
Return New String("*"c, _template.Length)
End Get
End Property
End Class
We can test the class like this in a little console application, without a form:
Module Programm
Public Sub Main()
Dim revealer = New RandomTextRevealer("Just a test")
Console.WriteLine(revealer.HiddenText)
For i As Integer = 1 To 12
Console.WriteLine(revealer.RevealNext())
Next
Console.ReadKey()
End Sub
End Module
Now we can integrate it in a form like this (I added a Reset-button, in order to be able to repeat the test with different random values):
Public Class Form2
Dim revealer As RandomTextRevealer
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Reset()
End Sub
Private Sub btnReveal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReveal.Click
Label1.Text = revealer.RevealNext()
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
Reset()
End Sub
Private Sub Reset()
revealer = New RandomTextRevealer("ABCD")
Label1.Text = revealer.HiddenText
End Sub
End Class
Now our form-code looks much cleaner.

Related

Error : reference to a non-shared member requires an object reference

The game i am making is connect 4 , the form I am calling from is here :
Public Class Form1
Public players(1) As IPlayer
Public playerturn As IPlayer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
buttons()
init()
End Sub
Public Sub init()
playerturn = players(0)
Board1.Init()
currentaturn.Text = String.Format("{0}'s turn", playerturn.name)
End Sub
Sub buttons()
For i As Integer = 0 To 7
Dim btn As New Button()
btn.Text = "drop"
btn.BackColor = Color.Pink
btn.Size = New Size(40, 40)
btn.Location = New Point(i * 48, 0)
btn.Tag = i
AddHandler btn.Click, AddressOf clickbutton
Panel1.Controls.Add(btn)
Next
End Sub
Sub clickbutton(ByVal sender As Object, ByVal e As EventArgs)
Dim clickedbutton As Button = CType(sender, Button)
Dim clickedcolumn As Integer = clickedbutton.Tag
If (Board1.addpiecetocolumn(clickedcolumn, Color.Red) = False) Then
Else
End If
End Sub
Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LDBTN.Click
End Sub
Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles currentaturn.Click
End Sub
Private Sub RESETBTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RESETBTN.Click
loginsys.Show()
Me.close
End Sub
End Class
and the form I am calling to :
Public Class board
Public board(8, 8) As panelbox
Public columns As Integer = 7
Public rows As Integer = 7
Public Sub New()
InitializeComponent()
Init()
End Sub
Public Sub Init()
Me.Controls.Clear()
For i As Integer = 0 To columns - 1
For j As Integer = 0 To rows - 1
board(i, j) = New panelbox()
board(i, j).Size = New Size(50, 50)
board(i, j).Location = New Point((i * 45), (j * 45))
board(i, j).BackColor = Color.DodgerBlue
Me.Controls.Add(board(i, j))
Next
Next
End Sub
Public Function addpiecetocolumn(ByVal x As Integer, ByVal colo As Color) As Boolean
For y As Integer = rows - 1 To 0 Step -1
If (board(x, y).Used = False) Then
board(x, y).Used = True
board(x, y).BackColor = colo
Return True
End If
Next
Return False
End Function
End Class
it should be noted that I am naming the second form board and the part that's calling the error is from the first form :
Public Sub init()
playerturn = players(0)
Board1.Init()
currentaturn.Text = String.Format("{0}'s turn", playerturn.name)
End Sub
Any help would be great
Use Public Shared Function function_name, in this case, you have to call it like board.function_name

How change Compare Multiple Textbox

I have 2 codes to change the color to Textbox, and unfortunately none of them work. What's wrong here? Why the code is not good. Every time I try the code, not worked, The first code changes the color to Textbox if there is a value between 1 and 7, and the second changes the value in ascending order from the lowest to the highest and assigns a corresponding color. Please tell me if these 2 codes are written correctly, or there is a write error.
Code 1: Image: http://www.imagebam.com/image/5ac5ee1073004874
Public Class TextBoxColors
Private ColorTable As Dictionary(Of String, Color) = New Dictionary(Of String, Color)()
Public Sub New()
ColorTable.Add("1", Color.Red)
ColorTable.Add("2", Color.Aqua)
ColorTable.Add("3", Color.Chocolate)
ColorTable.Add("4", Color.BlanchedAlmond)
ColorTable.Add("5", Color.BurlyWood)
ColorTable.Add("6", Color.BlueViolet)
ColorTable.Add("7", Color.DarkBlue)
End Sub
Public Function GetColor(ColorMap As String) As Color
Return If(ColorTable.Keys.Contains(ColorMap), ColorTable(ColorMap), Color.White)
End Function
End Class
Private txtColor As TextBoxColors = New TextBoxColors()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each txtDraw As TextBox In Me.Controls.OfType(Of TextBox).Where(Function(txt) txt.Name.StartsWith("txtDraw"))
AddHandler txtDraw.TextChanged,
Sub()
If Not String.IsNullOrEmpty(txtDraw.Text) Then
txtDraw.BackColor = txtColor.GetColor(txtDraw.Text)
End If
End Sub
Next
End Sub
Code 2: This 2nd code must be changed based on my text box, which starts with SumtxtDraw
- Image: http://www.imagebam.com/image/92a4091073004904
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FillColorList()
End Sub
Private Sub ColorTextBoxes()
FillTextBoxList(16, 23)
Dim SortedList As List(Of TextBox) = SortList()
Dim index As Integer
For Each txt As TextBox In SortedList
txt.BackColor = lstColor(index)
index += 1
Next
End Sub
Private Sub FillColorList()
lstColor.Add(Color.Red) 'for lowest number
lstColor.Add(Color.BlanchedAlmond)
lstColor.Add(Color.PaleGreen)
lstColor.Add(Color.Chartreuse)
lstColor.Add(Color.CadetBlue)
lstColor.Add(Color.Orange)
lstColor.Add(Color.DarkMagenta)
lstColor.Add(Color.Violet) 'for highest number
End Sub
Private Sub FillTextBoxList(StartNumber As Integer, EndNumber As Integer)
lstTextBox.Clear()
For suffix = StartNumber To EndNumber
lstTextBox.Add(DirectCast(Controls("TextBox" & suffix.ToString), TextBox))
Next
End Sub
Private Function SortList() As List(Of TextBox)
Dim orderedList = From txt In lstTextBox Order By CInt(txt.Text) Descending Select txt '$"{scorer.Score} - {scorer.Name}"
Dim SortedList As List(Of TextBox) = orderedList.ToList
Return SortedList
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ColorTextBoxes()
End Sub

Change Color a Textboxes from the Lower of the Highest

I have 2 codes to change the color to Textbox, and unfortunately none of them work. What's wrong here? Why the code is not good. Every time I try the code, it tells me there are no variables. The first code changes the color to Textbox if there is a value between 1 and 7, and the second changes the value in ascending order from the lowest to the highest and assigns a corresponding color.
Images: http://www.imagebam.com/image/5ac5ee1073004874
http://www.imagebam.com/image/92a4091073004904
Code 1:
Public Class TextBoxColors
Private ColorTable As Dictionary(Of String, Color) = New Dictionary(Of String, Color)()
Public Sub New()
ColorTable.Add("1", Color.Red)
ColorTable.Add("2", Color.Aqua)
ColorTable.Add("3", Color.Chocolate)
ColorTable.Add("4", Color.BlanchedAlmond)
ColorTable.Add("5", Color.BurlyWood)
ColorTable.Add("6", Color.BlueViolet)
ColorTable.Add("7", Color.DarkBlue)
End Sub
Public Function GetColor(ColorMap As String) As Color
Return If(ColorTable.Keys.Contains(ColorMap), ColorTable(ColorMap), Color.White)
End Function
End Class
Private txtColor As TextBoxColors = New TextBoxColors()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each txtDraw As TextBox In Me.Controls.OfType(Of TextBox).Where(Function(txt) txt.Name.StartsWith("txtDraw"))
AddHandler txtDraw.TextChanged,
Sub()
If Not String.IsNullOrEmpty(txtDraw.Text) Then
txtDraw.BackColor = txtColor.GetColor(txtDraw.Text)
End If
End Sub
Next
End Sub
Code 2: This 2nd code must be changed based on my text box, which starts with SumtxtDraw
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FillColorList()
End Sub
Private Sub ColorTextBoxes()
FillTextBoxList(16, 23)
Dim SortedList As List(Of TextBox) = SortList()
Dim index As Integer
For Each txt As TextBox In SortedList
txt.BackColor = lstColor(index)
index += 1
Next
End Sub
Private Sub FillColorList()
lstColor.Add(Color.Red) 'for lowest number
lstColor.Add(Color.BlanchedAlmond)
lstColor.Add(Color.PaleGreen)
lstColor.Add(Color.Chartreuse)
lstColor.Add(Color.CadetBlue)
lstColor.Add(Color.Orange)
lstColor.Add(Color.DarkMagenta)
lstColor.Add(Color.Violet) 'for highest number
End Sub
Private Sub FillTextBoxList(StartNumber As Integer, EndNumber As Integer)
lstTextBox.Clear()
For suffix = StartNumber To EndNumber
lstTextBox.Add(DirectCast(Controls("TextBox" & suffix.ToString), TextBox))
Next
End Sub
Private Function SortList() As List(Of TextBox)
Dim orderedList = From txt In lstTextBox Order By CInt(txt.Text) Descending Select txt '$"{scorer.Score} - {scorer.Name}"
Dim SortedList As List(Of TextBox) = orderedList.ToList
Return SortedList
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ColorTextBoxes()
End Sub

Object reference not set to instance of an object in VB.NET

Why am I getting the error "Object reference not set to instance of an object" with my code?
Public Class Form2
Dim i As Integer = 0
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMainMenu.Click
Me.Close()
End Sub
Private Sub btnEnterPatient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnterPatient.Click
Names(i) = txtPatientName.Text
i = i + 1
End Sub
End Class
Names() is a global variable
Thanks
Updated:
Module Module1
Public Names() As String
Public Heights() As Integer
Public Weights() As Integer
End Module
Public Class Form2
Dim i As Integer = 0
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMainMenu.Click
Me.Close()
End Sub
Private Sub btnEnterPatient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnterPatient.Click
ReDim Preserve Names(0 To i)
Names(i) = txtPatientName.Text
ReDim Preserve Heights(0 To i)
Heights(i) = txtPatientHeight.Text
ReDim Preserve Weights(0 To i)
Weights(i) = txtPatientWeight.Text
i = i + 1
End Sub
End Class
If you insist on using a module, you should redim preserve your array.
Public Module Module1
Public i As Integer = 0
Public Names() As String
Public Heights() As Integer
Public Weights() As Integer
End Module
Public Class Form1
Dim i As Integer = 0
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
Private Sub btnEnterPatient_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
ReDim Preserve Names(0 To i)
Names(i) = txtpatientName.Text
ReDim Preserve Heights(0 To i)
Heights(i) = txtpatientheight.Text
ReDim Preserve Weights(0 To i)
Weights(i) = txtpatientweight.Text
i = i + 1
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
For Each j In Names
MsgBox(j.ToString)
Next
End Sub
End Class
You need to make module as public. So i suggest below
Public Module Module1
Public Names() As String
Public Heights() As Integer
Public Weights() As Integer
End Module
Then access it in form like
Dim mod1 = Module1
mod1.Names(i) = txtPatientName.Text

Dynamic UserControl AddHandler

I am trying to understand why my AddHandler isn't working.
I have found a workaround if buttons always on same form but they may not be in the future.
I am also creating these buttons so I can add several variables for later use
Any have a simple answer for me please?
Thanks
Mark
FORM
Public Class Form1
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For n = 0 To 3
Dim ctl As New item_button
AddHandler ctl.Click, AddressOf Me.ClickMe
ctl.Name = "btn" & n
ctl.btn.Text = "Button " & n
ctl.btnID = n
ctl.Location = New Point(10, n * 50)
Me.Controls.Add(ctl)
Next
End Sub
Public Sub ClickMe(ByVal s As Object, ByVal e As EventArgs)
'do something
Dim btn As item_button
btn = CType(s, item_button)
TextBox1.Text = "Button " & s.btnID & " was pressed"
End Sub
End Class
ITEM_BUTTON
Public Class item_button
Public btnID As Integer
Public btnColor As System.Drawing.Color
Public Function ClickIt() As Integer
Return btnID
End Function
End Class
Your "Button" does not inherit from Button:
Public Class ItemButton ' Naming-Conventions: http://msdn.microsoft.com/en-us/library/ms229040(v=vs.110).aspx
Inherits Button
Public Property BtnID As Integer
Public Property BtnColor As System.Drawing.Color
Public Function ClickIt() As Integer
Return btnID
End Function
End Class
Since i'm not sure what you're actually trying to achieve i show you an example with a custom event that is raised in the custom button and handled in the form:
Public Class ItemButton
Inherits Button
Public Property BtnID As Integer
Public Property BtnColor As System.Drawing.Color
Public Event ButtonClicked(sender As ItemButton, buttonID As Int32)
Private Sub clicked(sender As Object, e As EventArgs) Handles Me.Click
RaiseEvent ButtonClicked(Me, BtnID)
End Sub
End Class
in the form:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For n = 0 To 3
Dim ctl As New ItemButton
AddHandler ctl.ButtonClicked, AddressOf Me.ItemButtonClicked
ctl.Name = "btn" & n
ctl.Name = "Button " & n.ToString()
ctl.btnID = n
ctl.Location = New Point(10, n * 50)
Me.Controls.Add(ctl)
Next
End Sub
Public Sub ItemButtonClicked(ByVal btn As ItemButton, ByVal buttonID As Int32)
TextBox1.Text = "Button " & buttonID & " was pressed"
End Sub
Sorted
Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click
RaiseEvent ButtonClicked(Me, btnID)
End Sub
Thanks Tim, your code helped