Huge amount of textboxes repeated code - vb.net

I have around 200 textboxes in my form.
Pressing "Enter" takes you to the next textbox.
Pressing "Up" takes you to the upper textbox, etc...
I know the coding but it's 200 textboxes so it's gonna be a huge amount of Copy/Paste. Is there any short way i can do it for all the 200 textboxes ?

You really want to show the users 200 textboxes on a single form? You should at least group them in a container control logically, for example in a Panel. Then you could use OfType.
You could add this to the constructor of your form-class which uses anonymous event handler:
Dim allTextBoxes = from txt in Me.TextBoxPanel.Controls.OfType(Of TextBox)()
Order by txt.TabIndex
Dim txtList = allTextBoxes.ToList()
For i As Int32 = 0 To txtList.Count - 1
Dim thisTxt = txtList(i)
Dim nextIndex = If(i + 1 >= txtList.Count, 0, i + 1)
Dim prevIndex = If(i - 1 < 0, txtList.Count - 1, i - 1)
Dim nextTxt = txtList(nextIndex)
Dim prevTxt = txtList(prevIndex)
AddHandler thisTxt.KeyDown,
Sub(txt As Object, e As KeyEventArgs)
If e.KeyCode = 38 Then 'up
Me.ActiveControl = prevTxt
ElseIf e.KeyCode = 13 Then 'enter
Me.ActiveControl = nextTxt
End If
End Sub
Next

Related

How do i get data from a textbox that is made during the runtime of the program?

this is my code its the start of a customer details editing system for my course work
Public Class editCustomerFields
Dim textbox As New TextBox
Private Sub editCustomerFields_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim updownLBL As Integer = 55
Dim updownTXT As Integer = 80
Dim leftRight As Integer = 30
For i = 0 To globalVariables.countForCustomerDetails - 1
Dim label As New Label
label.Size = New System.Drawing.Size(159, 23)
label.Location = New System.Drawing.Point(leftRight, updownLBL)
label.Text = globalVariables.fieldsCustomerDetailsArray(globalVariables.editCustomerDetailsTxtNamesList.Item(i))
label.Name = globalVariables.fieldsCustomerDetailsArray(globalVariables.editCustomerDetailsTxtNamesList.Item(i))
Me.Controls.Add(label)
TextBox.Size = New System.Drawing.Size(315, 30)
TextBox.Location = New System.Drawing.Point(leftRight, updownTXT)
textbox.Name = globalVariables.fieldsCustomerDetailsArray(globalVariables.editCustomerDetailsTxtNamesList.Item(i))
MsgBox(TextBox.Name)
Me.Controls.Add(textbox)
globalVariables.txtEditCustomerDetailsArray(i) = textbox.Name
updownLBL += 60
updownTXT += 60
If updownTXT >= 500 Then
leftRight = 350
updownTXT = 80
updownLBL = 55
End If
Next
End Sub
End Class
i have a bunch of checkboxes that the user selects that correspond to the different fields in the database. then the program takes the input and on another form displays textboxes and labels for the user to enter data into. How do I get the data entered in the textbox?
fieldsCustomerDetailsArray is an array of the field names ("username","password" etc)
editCustomerDetailsTxtNamesList is a list of the checkboxes for the array to use

Split string into Sub String

I Am Using Vb.net. My idea is when I paste (ctrl+v) for example this character :
AHAKAPATARAE
I Have 6 textboxes.
it would automatically paste them in textboxes one by one in order!
so
txtBox1 will contain : AH
txtBox2 : AK
txtBox3 : AP
txtBox4 : AT
texbox5 : AR
texbox6 : AE
The automation of inserting Licence Keys will ease so much
so that user will not work so hard to cut & paste each two digits!
so any suggestion of doing auto-fill inside textboxes..?
Thanks.
This will allow the paste into the first textbox, as well as move forward correctly if the user types the whole thing manually starting in the first box:
Private Sub txtBox1_TextChanged(sender As Object, e As EventArgs) Handles txtBox6.TextChanged, txtBox5.TextChanged, txtBox4.TextChanged, txtBox3.TextChanged, txtBox2.TextChanged, txtBox1.TextChanged
Dim TB As TextBox = DirectCast(sender, TextBox)
Dim value As String = TB.Text
If value.Length > 2 Then
TB.Text = value.Substring(0, 2)
Dim TBs() As TextBox = {txtBox1, txtBox2, txtBox3, txtBox4, txtBox5, txtBox6}
Dim index As Integer = Array.IndexOf(TBs, TB)
If index > -1 AndAlso index < (TBs.Length - 1) Then
index = index + 1
TBs(index).Text = value.Substring(2)
TBs(index).Focus()
TBs(index).SelectionStart = TBs(index).TextLength
End If
End If
End Sub
There are definitely many more ways this could be accomplished...
Handle the key down event on textbox 1
Private Sub txtBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtBox1.KeyDown
If e.Modifiers = Keys.Control AndAlso e.KeyCode = Keys.V Then
' Get Clipboard Text
Dim cpText as String = My.Computer.Clipboard.GetText()
' Assign
txtBox1.Text = cpText.Substring(0, 2)
txtBox2.Text = cpText.Substring(2, 2)
txtBox3.Text = cpText.Substring(4, 2)
txtBox4.Text = cpText.Substring(6, 2)
txtBox5.Text = cpText.Substring(8, 2)
txtBox6.Text = cpText.Substring(10, 2)
'the event has been handled manually
e.Handled = True
End If
End Sub
Another option is to have one masked textbox event. Might be easier

Fitting runtime buttons inside a form

I have a number of buttons between 5-20 and it's variable each time the form loads based on the user settings. I am trying to fit all these buttons on my form no matter what the size of the form is. The buttons are generated during runtime. I would like the first button to be 20 points from the top bar (at any size) and the rest of the buttons simply to fit in the form. This is what I have now but I have to maximize the form to view them all and also while I'm expanding the form the space between the buttons decreases and they overlap with each other whereas they should keep a relative distance. Any ideas?
Dim iButtonWidth, iButtonHeight, iVerticalSpace As Integer
If UserButtons.Count > 0 Then
iButtonHeight = Me.Size.Height - (Me.Size.Height * 0.85)
iButtonWidth = Me.Size.Width - (Me.Size.Width * 0.5)
iVerticalSpace = iButtonHeight / 3
For Each btn In UserButtons
btn.Size = New System.Drawing.Size(iButtonWidth, iButtonHeight)
btn.Location = New Point(20, 20 + btn.TabIndex * iVerticalSpace * 3)
Next
End If
Here's a quick example using the TableLayoutPanel to play with:
Public Class Form1
Private UserButtons As New List(Of Button)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Static R As New Random
Dim NumButtons As Integer = R.Next(5, 21) ' "a number of buttons between 5-20 and it's variable each time"
UserButtons.Clear()
For i As Integer = 1 To NumButtons
Dim btn As New Button()
btn.Text = i.ToString("00")
btn.Dock = DockStyle.Fill ' Optional: See how you like it with this on vs. off
UserButtons.Add(btn)
Next
DisplayButtons()
End Sub
Private Sub DisplayButtons()
TableLayoutPanel1.SuspendLayout()
TableLayoutPanel1.Controls.Clear()
TableLayoutPanel1.ColumnStyles.Clear()
TableLayoutPanel1.ColumnCount = 5 ' Fixed Number of Columns
For i As Integer = 1 To TableLayoutPanel1.ColumnCount
TableLayoutPanel1.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 911)) ' the size doesn't matter here, as long as they are all the same
Next
' Variable Number of Rows:
Dim RowsRequired As Integer = ((UserButtons.Count - 1) \ TableLayoutPanel1.ColumnCount) + 1 ' Integer Division
TableLayoutPanel1.RowStyles.Clear()
TableLayoutPanel1.RowCount = RowsRequired
For i As Integer = 1 To TableLayoutPanel1.RowCount
TableLayoutPanel1.RowStyles.Add(New RowStyle(SizeType.Percent, 911)) ' the size doesn't matter here, as long as they are all the same
Next
TableLayoutPanel1.Controls.AddRange(UserButtons.ToArray)
TableLayoutPanel1.ResumeLayout()
End Sub
End Class
First of all what kind of container are the buttons in? You should be able to set the container's AutoScroll property to true - then when controls within it spill out of the visible bounds you will get a scroll bar.
Then also what you could do is draw each button in more of a table with a certain number next to each other before dropping down to the next line (instead of just 1 button on each line). If that is an option that works for you then you could get more buttons within the visible space.
I happen to have an example to do the same thing with picture boxes (and text boxes under each picture box). Hope this helps:
Dim point As New Point(0, 0)
'create 11 picture and text boxes-you can make this number the number your user selects.
Dim box(11) As PictureBox
Dim text(11) As TextBox
Dim i As UInt16
For i = 0 To 11 'or whatever your number is
box(i) = New PictureBox
box(i).Width = 250 'your button width
box(i).Height = 170 'your button height
box(i).BorderStyle = BorderStyle.FixedSingle
box(i).Location = point
layoutsPanel.Controls.Add(box(i)) 'my container is a panel
text(i) = New TextBox
text(i).Height = 50
text(i).Width = 250
point.Y += box(i).Height
text(i).Location = (point)
layoutsPanel.Controls.Add(text(i))
point.Y -= box(i).Height 'reset Y for next picture box
'Put 4 picture boxes in a row, then move down to next row
If i Mod 4 = 3 Then
point.X = 0
point.Y += box(i).Height + text(i).Height + 4
Else
point.X += box(i).Width + 4
End If
Next

How to customize a "MsgBox" control in Visual Basic

Is there a way to customize the MsgBox control in Visual Basic?
I use it quite often to alert users. However it never pops up on the screen; it just appears along the bottom task bar. It also always has a heading similar to "App_data_xxx". Can I improve this in any way?
My searches aren't throwing up much help.
For example:
' Queries user number and password against the database and returns user
Dim matchingusers = From u In db.Users
Where username = u.Email And password = u.Password
Select u
' Checks if a valid user was returned
If matchingusers.Count = 0 Then
MsgBox("Invalid user entered, try again")
Else
SelectedDeveloper = 0
' Set the logged in user for use in the project
GlobalVariables.LoggedInUser = matchingusers.First
You can use the MessageBox function and its many variables. Here's an example of what it can do:
MessageBox.Show("The Displayed text in the messagebox", _
"the text displayed in the title bar", MessageBoxButtons.YesNoCancel, _
MessageBoxIcon.Error, MessageBoxDefaultButton.Button2)
Or you can still use MsgBox and use its variables, though it gives you fewer options. Here's an example:
MsgBox("message text", MsgBoxStyle.Information, "title bar text")
You are using a reference to the MessageBox class without specifying the Method you want to call. You cannot create an instance of MessageBox and therefore cannot pass a string as parameter to try and create one.
Use MessageBox.Show(string messageText) to display the MessageBox with the desired message.
As for your question, you should create your own MessageBox class. With this solution, you get all the options you want and can customize it completely. The call will be a little different :
//C#
var myCustomMessageBox = new CustomMessageBox();
myCustomMessageBox.ShowDialog();
//Vb
Dim myCustomMessageBox As New CustomMessageBox()
myCustomMessageBox.ShowDialog()
The ShowDialog() will be used to create the effect of a messagebox.
In Visual Basic 2008 I had a requirement for a message box that would only stay on for short times and that the time it was on to be variable. I also had the problem that when using extended screen that the msgbox showed up on the extended screen (which was a different form) instead of on the main computer screen. To overcome these problems I created a custom Message Box using a panel on the form I had on the main screen.
To call the message panel DoMessage("The message", Seconds, Buttons to show 1 = Ok only 2 = Yes(ok) and No, 3 = Yes, No and Cancel. If seconds is 0 or not specified then the time to show the panel is set to a long time (10000 seconds) If Buttons to show is not specified it is set to Ok button only. If seconds and buttons are both specified, If no button is clicked the panel will just hide after the timeout.
The responses are 1 if Ok or Yes is clicked, 2 if No is clicked, 3 if Cancel is clicked
It is put into DoMsgResp so you see what is in it to handle the response.
Create the message panel when opening the form by calling MakeMsgPanel()
Dim MessagePanel As New Panel 'The panel
Dim MessageLabel As New Label 'The message
Dim MsgYes As New Button 'Yes or OK button
Dim MsgNo As New Button 'no button
Dim MsgCcl As New Button 'Cancel button
Dim Sleepsecs As Integer 'How long panel shows for
Dim DoMsgResp As Integer 'response 1, 2 or 3 depending which button clicked
Private Sub MakeMsgPanel()
Me.Controls.Add(MessagePanel)
Me.MessagePanel.Controls.Add(MessageLabel)
Me.MessagePanel.Controls.Add(MsgYes)
Me.MessagePanel.Controls.Add(MsgNo)
Me.MessagePanel.Controls.Add(MsgCcl)
MessagePanel.Location = New System.Drawing.Point(Me.Width / 2 - 200, Me.Height / 2 - 100)
MessagePanel.BackColor = Color.PaleGreen
MessageLabel.BackColor = Color.PeachPuff
MessagePanel.BorderStyle = BorderStyle.FixedSingle
MessageLabel.Font = New Font("Arial", 12, FontStyle.Regular, GraphicsUnit.Point)
MessageLabel.AutoSize = True
MessagePanel.AutoSize = True
MessagePanel.AutoSizeMode = Windows.Forms.AutoSizeMode.GrowOnly
MessagePanel.Hide()
MsgYes.Location = New System.Drawing.Point(205, 5)
MsgNo.Location = New System.Drawing.Point(115, 5)
MsgCcl.Location = New System.Drawing.Point(25, 5)
MsgYes.Text = "Yes"
MsgNo.Text = "No"
MsgCcl.Text = "Cancel"
AddHandler MsgYes.Click, AddressOf MsgYes_Click
AddHandler MsgNo.Click, AddressOf MsgNo_Click
AddHandler MsgCcl.Click, AddressOf MsgCcl_Click
End Sub
Private Sub MsgYes_Click()
DoMsgResp = 1
Sleepsecs = 0
End Sub
Private Sub MsgNo_Click()
DoMsgResp = 2
Sleepsecs = 0
End Sub
Private Sub MsgCcl_Click()
DoMsgResp = 3
Sleepsecs = 0
End Sub
Private Sub DoMessage(ByVal Msg As String, Optional ByVal Secs As Integer = 0, _
Optional ByVal Btns As Integer = 0)
'Information messages that can be timed
Dim TheHeight As Integer
Dim TheWidth As Integer
Dim Labelx As Integer
Dim Labely As Integer
DoMsgResp = 0
MessageLabel.Text = Msg
If MessageLabel.Height < 90 Then
TheHeight = 100
Labely = (100 - MessageLabel.Height) / 2
Else
TheHeight = MessageLabel.Height + 10
Labely = 5
End If
If MessageLabel.Width < 140 Then
TheWidth = 150
Labelx = (150 - MessageLabel.Width) / 2
Else
TheWidth = MessageLabel.Width + 10
Labelx = 5
End If
MessagePanel.Size = New System.Drawing.Size(TheWidth, TheHeight)
MessageLabel.Location = New System.Drawing.Point(Labelx, Labely)
MessageLabel.Show()
MessagePanel.Show()
MessagePanel.BringToFront()
MsgYes.BringToFront()
MsgNo.BringToFront()
MsgCcl.BringToFront()
MessagePanel.Focus()
If Btns = 0 Or Btns > 3 Then Btns = 1 'Make ok button if none specified or number too high
If Btns = 1 Then
MsgYes.Text = "Ok"
MsgNo.Hide()
MsgCcl.Hide()
Else 'is 2 or 3
MsgYes.Text = "Yes"
MsgNo.Show()
If Btns = 2 Then MsgCcl.Hide() Else MsgCcl.Show()
End If
If Secs = 0 Then Secs = 10000 'make a long time
If Secs > 0 Then
Sleepsecs = Secs * 2
Do Until Sleepsecs < 1
Threading.Thread.Sleep(500)
Application.DoEvents()
Application.RaiseIdle(New System.EventArgs)
Sleepsecs = Sleepsecs - 1
Loop
End If
MessagePanel.Hide()
End Sub
Private Sub ButtonTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonTest.Click
DoMessage("This is To see what happens with my message" & vbCrLf & _
"see if it works good", 0, 3)
If DoMsgResp = 1 Then
MsgBox("Ok was hit")
End If
If DoMsgResp = 2 Then
MsgBox("No was hit")
End If
If DoMsgResp = 3 Then
MsgBox("Cancel was hit")
End If
End Sub

Trouble with Timer_tick not stopping

I'm very new to programming and vb.net, trying to self teach more so as a hobby, as I have an idea for a program that I would find useful, but I am having trouble getting past this issue and I believe it is to do with the timer.
I have a form of size.(600,600) with one button of size.(450,150) that is set location(100,50) on the form. When clicked I want to move down it's own height, then add a new button in it's place. The code included below works as desired for the first two clicks, but on the third click the button keeps moving and the autoscroll bar extends. I initially thought it was the autoscroll function or the location property, but realised that as the button keeps moving, the timer hasn't stopped. I am aware that the code is probably very clunky in terms of achieving the outcome, and that there are a few lines/variables that are currently skipped over by the compiler (these are from older attempts to figure this out).
I have looked around and can't find the cause of my problem. Any help would be greatly appreciated. Apologies if the code block looks messy - first go.
Public Class frmOpenScreen
Dim intWButtons, intCreateButtonY, intCreateButtonX 'intTimerTick As Integer
Dim arrWNames() As String
Dim ctrlWButtons As Control
Dim blnAddingW As Boolean
Private Sub btnCreateW_Click(sender As System.Object, e As System.EventArgs) Handles btnCreateW.Click
'Creates new Button details including handler
Dim strWName, strWShort As String
Dim intCreateButtonY2 As Integer
Static intNumW As Integer
Dim B As New Button
strWName = InputBox("Please enter the name name of the button you are creating. Please ensure the spelling is correct.", "Create W")
If strWName = "" Then
MsgBox("Nothing Entered.")
Exit Sub
End If
strWShort = strWName.Replace(" ", "")
B.Text = strWName
B.Width = 400
B.Height = 150
B.Font = New System.Drawing.Font("Arial Narrow", 21.75)
B.AutoSizeMode = Windows.Forms.AutoSizeMode.GrowAndShrink
B.Anchor = AnchorStyles.Top
B.Margin = New Windows.Forms.Padding(0, 0, 0, 0)
'Updates Crucial Data (w name array, number of w buttons inc Create New)
If intNumW = 0 Then
ReDim arrWNames(0)
Else
intNumW = UBound(arrWNames) + 1
ReDim Preserve arrWNames(intNumW)
End If
arrWNames(intNumW) = strWShort
intNumW = intNumW + 1
intWButtons = WButtonCount(intWButtons) + 1
'updates form with new button and rearranges existing buttons
intCreateButtonY = btnCreateW.Location.Y
intCreateButtonX = btnCreateW.Location.X
‘intTimerTick = 0
tmrButtonMove.Enabled = True
‘Do While intTimerTick < 16
‘ 'blank to do nothing
‘Loop
'btnCreateW.Location = New Point(intCreateButtonX, intCreateButtonY + 150)
B.Location = New Point(intCreateButtonX, intCreateButtonY)
Me.Controls.Add(B)
B.Name = "btn" & strWShort
intCreateButtonY2 = btnCreateW.Location.Y
If intCreateButtonY2 > Me.Location.Y Then
Me.AutoScroll = False
Me.AutoScroll = True
Else
Me.AutoScroll = False
End If
'MsgBox(intCreateButtonY)
End Sub
Function WButtonCount(ByRef buttoncount As Integer) As Integer
buttoncount = intWButtons
If buttoncount = 0 Then
Return 1
End If
Return buttoncount
End Function
Public Sub tmrButtonMove_Tick(sender As System.Object, e As System.EventArgs) Handles tmrButtonMove.Tick
Dim intTimerTick As Integer
If intTimerTick > 14 Then
intTimerTick = 0
End If
If btnCreateW.Location.Y <= intCreateButtonY + 150 Then
btnCreateW.Top = btnCreateW.Top + 10
End If
intTimerTick += 1
If intTimerTick = 15 Then
tmrButtonMove.Enabled = False
End If
End Sub
End Class
So my current understanding is that the tick event handler should be increasing the timertick variable every time it fires, and that once it has hits 15 it should diable the timer and stop the button moving, but it is not doing so.
Thanks in advance.
IntTimerTick is initialized to 0 at the beginning of every Tick event. This won't happen if you declare it to be static:
Static Dim intTimerTick As Integer