I need to create an edit control that takes each line, as entered, and goes off and does an action according to what was entered. I don't want the user to be able to delete what they typed into the box via their mouse and the delete key. It would look something like this:
Power On (user entered this)
Power: On Level:50 (what the power on command returned)
Laser On (user entered this )
Laser: On Power:60 ( what the command returned)
So I don't want them to go back and delete the lines already entered, they can just keep appending items and sending off these commands. Any ideas on how to accomplish this? Thanks.
I think your best bet would be creating a hybrid UserControl consisting of 2 Textbox's one which is readonly the other being writable. Enter your data in the one, append it to the other when the enter key is pressed, then append the response. Since you haven't stated a programming language here is an example in vb.net.
Public Class UserControl1
Private Sub TextBox2_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox2.KeyDown
If e.KeyCode = Keys.Enter Then
ScrollToEnd(TextBox1)
TextBox1.Text += TextBox2.Text + vbCrLf
TextBox1.Text += GetResponse(TextBox2.Text) + vbCrLf
TextBox2.Text = ""
ScrollToEnd(TextBox1)
e.SuppressKeyPress = True
End If
End Sub
Private Sub ScrollToEnd(tb As TextBox)
tb.SelectionStart = tb.TextLength
tb.ScrollToCaret()
End Sub
Private Function GetResponse(command As String) As String
Select Case command
Case "Power On"
Return "On Level: 50"
Case "Laser On"
Return "Laser: On Power:60"
Case Else
Return "I do not understand"
End Select
End Function
End Class
UserControl1.Designer.VB's InitializeComponent Method (I am only putting this in so you can see the properties of my Controls)
Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.TextBox2 = New System.Windows.Forms.TextBox()
Me.TableLayoutPanel1 = New System.Windows.Forms.TableLayoutPanel()
Me.TableLayoutPanel1.SuspendLayout()
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.BackColor = System.Drawing.Color.Black
Me.TextBox1.BorderStyle = System.Windows.Forms.BorderStyle.None
Me.TextBox1.Dock = System.Windows.Forms.DockStyle.Fill
Me.TextBox1.ForeColor = System.Drawing.Color.Lime
Me.TextBox1.Location = New System.Drawing.Point(3, 3)
Me.TextBox1.Multiline = True
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.ReadOnly = True
Me.TextBox1.Size = New System.Drawing.Size(253, 181)
Me.TextBox1.TabIndex = 0
Me.TextBox1.TabStop = False
'
'TextBox2
'
Me.TextBox2.BackColor = System.Drawing.Color.Black
Me.TextBox2.BorderStyle = System.Windows.Forms.BorderStyle.None
Me.TextBox2.Dock = System.Windows.Forms.DockStyle.Fill
Me.TextBox2.ForeColor = System.Drawing.Color.Lime
Me.TextBox2.Location = New System.Drawing.Point(3, 190)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(253, 17)
Me.TextBox2.TabIndex = 1
'
'TableLayoutPanel1
'
Me.TableLayoutPanel1.BackColor = System.Drawing.Color.Black
Me.TableLayoutPanel1.ColumnCount = 1
Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50.0!))
Me.TableLayoutPanel1.Controls.Add(Me.TextBox1, 0, 0)
Me.TableLayoutPanel1.Controls.Add(Me.TextBox2, 0, 1)
Me.TableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill
Me.TableLayoutPanel1.Location = New System.Drawing.Point(0, 0)
Me.TableLayoutPanel1.Name = "TableLayoutPanel1"
Me.TableLayoutPanel1.RowCount = 2
Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 89.09953!))
Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 10.90047!))
Me.TableLayoutPanel1.Size = New System.Drawing.Size(259, 211)
Me.TableLayoutPanel1.TabIndex = 2
'
'UserControl1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(7.0!, 16.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.Controls.Add(Me.TableLayoutPanel1)
Me.Name = "UserControl1"
Me.Size = New System.Drawing.Size(259, 211)
Me.TableLayoutPanel1.ResumeLayout(False)
Me.TableLayoutPanel1.PerformLayout()
Me.ResumeLayout(False)
End Sub
Related
I am doing my homework but stuck on a part. Problem is, How can i populate seat number in array of controls(labels) using database. I already created labels and a class to retrieve all rows from database but how can i apply it in main form and populate labels.
--------------------------Class---------------------------------------
Public Shared Function getOneRow(PK As Integer) As datMovieTimes
Dim returnRow As New datMovieTimes(0)
Dim connDB As New SqlConnection
connDB.ConnectionString = Conn.getConnectionString
Dim command As New SqlCommand
command.Connection = connDB
command.CommandType = CommandType.Text
command.CommandText = SQLStatements.SELECT_1_BY_ID
command.Parameters.AddWithValue("#Key", PK)
Try
connDB.Open()
Dim dR As IDataReader = command.ExecuteReader
If dR.Read() Then
returnRow.showingID = PK
If Not IsDBNull(dR(Fields.movieID)) Then returnRow.movieID = dR(Fields.movieID)
If Not IsDBNull(dR(Fields.dateTime)) Then returnRow.dateTime = dR(Fields.dateTime)
If Not IsDBNull(dR(Fields.isActive)) Then returnRow.isActive = dR(Fields.isActive)
End If
Catch ex As Exception
Console.WriteLine(Err.Description)
End Try
Return returnRow
End Function
Public Shared Function getAllRows() As Generic.List(Of datMovieTimes)
Dim returnRows As New Generic.List(Of datMovieTimes)
Dim connDB As New SqlConnection
connDB.ConnectionString = Conn.getConnectionString
Dim command As New SqlCommand
command.Connection = connDB
command.CommandType = CommandType.Text
command.CommandText = SQLStatements.SELECT_ALL
Try
connDB.Open()
Dim dR As IDataReader = command.ExecuteReader
Do While dR.Read()
Dim Row As New datMovieTimes(0)
If Not IsDBNull(dR(Fields.showingID)) Then Row.showingID = dR(Fields.showingID)
If Not IsDBNull(dR(Fields.movieID)) Then Row.movieID = dR(Fields.movieID)
If Not IsDBNull(dR(Fields.dateTime)) Then Row.dateTime = dR(Fields.dateTime)
If Not IsDBNull(dR(Fields.isActive)) Then Row.isActive = dR(Fields.isActive)
returnRows.Add(Row)
Loop
Catch ex As Exception
Console.WriteLine(Err.Description)
End Try
Return returnRows
End Function
-----------------------------main form-----------------------------------------
Public Sub createSeat()
Dim S1 As Label
For X As Integer = 1 To _MAX_X
For Y As Integer = 1 To _MAX_Y
S1 = New Label
S1.Height = 25
S1.Width = 25
S1.BackColor = Color.LightGreen
S1.Top = 100 + (X - 1) * (S1.Height + 5)
S1.Left = 200 + (Y - 1) * (S1.Width + 5)
S1.TextAlign = ContentAlignment.MiddleCenter
S1.Text = Y.ToString
AddHandler S1.Click, AddressOf GenericLabel_Click
Me.Controls.Add(S1)
_SeatArray(X, Y) = S1
Next
Next
For X As Integer = 0 To 9
_AlphaLabel(X) = New Label
_AlphaLabel(X).Height = 25
_AlphaLabel(X).Width = 25
_AlphaLabel(X).BackColor = Color.Transparent
_AlphaLabel(X).Top = 130 + (X - 1) * (_AlphaLabel(X).Height + 6)
_AlphaLabel(X).Left = 170
_AlphaLabel(X).Text = _AlphaName(X)
Me.Controls.Add(_AlphaLabel(X))
Next
End Sub
Private Sub GenericLabel_Click(sender As Object, e As EventArgs)
Dim L As New Label
L = DirectCast(sender, Label)
If L.BackColor = Color.LightGreen Then
L.BackColor = Color.Orange
clickLess -= 1
ElseIf L.BackColor = Color.Orange Then
L.BackColor = Color.LightGreen
clickLess += 1
End If
clickCount += 1
Me.lblRemainingCount.Text = clickLess.ToString
Me.nudTicketsCount.Value = clickCount
If clickLess <= 0 Then
MsgBox("No more seats left.", MsgBoxStyle.OkOnly, "House Full")
End If
End Sub
Database pic
When creating labels, insert one more line:
S1.Name = "MyLabel" & X & Y
When accessing the label:
Dim MyCurrentLabel as Label
MyCurrentLabel = CType("MyLabel" & X & Y, Label)
Then you can do things with the current label:
MyCurrentLabel.Text = "Hello World"
current image
Now it is something like this, so it want to change the colour to red if it is paid according to database.
Thanks
I found the answer, sorry i forgot to mention it because i was busy in completing the project
----------------------Seat creation----------------------------------------
Public Sub createSeat()
Dim S1 As Label
Dim numValue As Integer = 1
For X As Integer = 1 To _MAX_X
For Y As Integer = 1 To _MAX_Y
S1 = New Label
S1.Height = 25
S1.Width = 25
S1.BackColor = Color.LightGreen
S1.Top = 180 + (X - 1) * (S1.Height + 5)
S1.Left = 200 + (Y - 1) * (S1.Width + 5)
S1.TextAlign = ContentAlignment.MiddleCenter
S1.Text = Y.ToString
' S1.Text = numValue
S1.Name = "Label" & numValue
AddHandler S1.Click, AddressOf GenericLabel_Click
Me.Controls.Add(S1)
_SeatArray(X, Y) = S1
numValue += 1
Next
Next
For X As Integer = 0 To 9
_AlphaLabel(X) = New Label
_AlphaLabel(X).Height = 25
_AlphaLabel(X).Width = 25
_AlphaLabel(X).BackColor = Color.Transparent
_AlphaLabel(X).Top = 210 + (X - 1) * (_AlphaLabel(X).Height + 6)
_AlphaLabel(X).Left = 170
_AlphaLabel(X).Text = _AlphaName(X)
Me.Controls.Add(_AlphaLabel(X))
Next
End Sub
-------------------------------populate seat number----------------------------------
Public Sub populateSeatNumber()
Dim connectionString As String = DBL.Conn.getConnectionString
Dim connection As New SqlConnection(connectionString)
connection.Open()
Dim selectStatement As String = "SELECT * FROM datTicketsSold"
Dim selectCommand As New SqlCommand(selectStatement, connection)
Dim daSoldTickets As New SqlDataAdapter(selectCommand)
Dim dsSoldTickets As DataSet = New DataSet
daSoldTickets.Fill(dsSoldTickets, "datTicketsSold")
connection.Close()
Dim dtTickets As DataTable = dsSoldTickets.Tables("datTicketsSold")
Dim row As DataRow
For Each row In dtTickets.Rows
If row(3) = True Then
CType(Controls("Label" & row(2)), Label).BackColor = Color.Red
redCounter += 1
Else
CType(Controls("Label" & row(2)), Label).BackColor = Color.Yellow
yellowCounter += 1
End If
Next
Me.lblReservedCount.Text = yellowCounter.ToString
Me.lblSoldCount.Text = redCounter.ToString
End Sub
Thanks everyone
I need to loop through an array of user controls that were programmatically created.
Where My problem is later I need to loop through the array of user controls created and use the controls of the user control
My sudo code. The line I cannot figure out is
MyCtrl.tbQtyM.Text = Format(60 / Val(MyCtrl.tbMPU.Text) / 55 * NumEmployees, "0.00")
Dim MyProds As New MyMPUProduct
For iLoop2 = 1 To Val(sender.Text)
MyProds = New MyMPUProduct
Me.TabControl1.SelectedTab.Controls.Add(MyProds)
With MyProds
.Name = "MyProd" & iLoop2
.Location = New Point(6, MyProdSpace)
.VerifyTMPUProductsTblApt()
.TMPUProductsBindingSource.Filter = "ID = '-1'"
.tbMix.Text = "0"
.Label1.Text = iLoop2
End With
Next
Dim CtrlName As String
For i = 1 To NumProds
CtrlName = "MyProd" & i
For Each MyCtrl As Control In Me.TabControl1.SelectedTab.Controls
If TypeOf MyCtrl Is MyMPUProduct Then
MyCtrl.tbQtyM.Text = Format(60 / Val(MyCtrl.tbMPU.Text) / 55 * NumEmployees, "0.00")
End If
Next
Next
I want to run the fetching of a Database into a background worker but it seems to be crashing without Visual Studio returning me an error in my code.
Here's the code in the DoWork:
Private Sub ITSM_Fetch_BW_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles ITSM_Fetch_BW.DoWork
Dim sql As String = "xxxx"
Dim ConnString As String = "DRIVER={AR System ODBC Driver};ARServer=xxxx;ARServerPort=xxxx;ARPrivateRpcSocket=xxxx;UID=xxxx;PWD=xxxx;ARAuthentication=;ARUseUnderscores=1;SERVER=NotTheServer"
Dim connection As New Odbc.OdbcConnection(ConnString)
connection.Open()
Dim ODBC_Command As New Odbc.OdbcCommand(sql, connection)
Dim ODBC_reader As Odbc.OdbcDataReader
'Load the Data into the local Memory
ODBC_reader = ODBC_Command.ExecuteReader
e.Result = ODBC_reader
ODBC_reader.Close()
End Sub
Private Sub ITSM_Fetch_BW_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles ITSM_Fetch_BW.RunWorkerCompleted
Data = New DataTable
Data.Load(e.Result)
Dim Count_ToDo(5) As String
Count_ToDo(0) = "Product_Name"
Count_ToDo(1) = "Status"
Count_ToDo(2) = "Language"
Count_ToDo(3) = "Assigned_Group"
Count_ToDo(4) = "Priority"
Count_ToDo(5) = "Company"
For Each Item As String In Count_ToDo
Dim i As Integer = 0
Dim ITEM_Count(0, 1) As String
For Each Ticket As DataRow In Data.Rows
'PART FOR THE CI
If IsDBNull(Ticket.Item(Item)) = False Then
Dim IsInIndex As Integer = -1
If i = 0 Then
ITEM_Count(0, 0) = Ticket.Item(Item)
ITEM_Count(0, 1) = 1
Else
For x As Integer = 0 To ITEM_Count.GetLength(0) - 1
If ITEM_Count(x, 0) = Ticket.Item(Item) Then
IsInIndex = x
End If
Next
If IsInIndex = -1 Then
Dim ITEM_Count_Temp(ITEM_Count.GetLength(0), ITEM_Count.GetLength(0)) As String
ITEM_Count_Temp = ITEM_Count
ReDim ITEM_Count(ITEM_Count.GetLength(0), 1)
For x As Integer = 0 To ITEM_Count_Temp.GetLength(0) - 1
For y As Integer = 0 To ITEM_Count_Temp.GetLength(1) - 1
ITEM_Count(x, y) = ITEM_Count_Temp(x, y)
Next
Next
ITEM_Count(ITEM_Count.GetLength(0) - 1, 0) = Ticket.Item(Item)
ITEM_Count(ITEM_Count.GetLength(0) - 1, 1) = 1
Else
ITEM_Count(IsInIndex, 1) = ITEM_Count(IsInIndex, 1) + 1
End If
End If
Else
'IF NULL
End If
i = i + 1
Next
'CI_COUNT FILLING
'ORDERING BY COUNT
Dim ITEM_obj = New List(Of obj)
Dim ITEM_ToObj As String = ""
Dim ITEMCount_ToObj As String = ""
For x As Integer = 0 To ITEM_Count.GetLength(0) - 1
ITEM_ToObj = ITEM_Count(x, 0)
ITEMCount_ToObj = ITEM_Count(x, 1)
ITEM_obj.Add(New obj(ITEM_ToObj, ITEMCount_ToObj))
Next
ITEM_obj = OrderItem(ITEM_obj)
Dim Item_Count_listview As ListViewItem
For Each Itemobj As obj In ITEM_obj
Dim Transfer_Array(2) As String
Transfer_Array(0) = Itemobj.Item
Transfer_Array(1) = Itemobj.Item_Count
Item_Count_listview = New ListViewItem(Transfer_Array)
Select Case Item
Case "Product_Name"
CI_Count_Table.Items.Add(Item_Count_listview)
Case "Status"
Status_Count_Table.Items.Add(Item_Count_listview)
Case "Language"
Language_Count_Table.Items.Add(Item_Count_listview)
Case "Assigned_Group"
AssignedGroup_Count_Table.Items.Add(Item_Count_listview)
Case "Priority"
Priority_Count_Table.Items.Add(Item_Count_listview)
Case "Company"
LOB_Count_Table.Items.Add(Item_Count_listview)
Case Else
MsgBox("No Category Of this type exist. Programming Issue. Item is: " & Item)
End Select
Next
Next
End Sub
What is not possible to run into a background worker like this?
regards,
For several projects now I've had this error flash up and completely ground my projects. In most cases, I've simply had to leave them and start a new, different one. Basically, I just want to be able to refer to every object on the form that matches certain criteria. In this case it's an arcade style game with four different game modes. There is a timer that creates the objects to be shot at and it works fine, either a PictureBox or a label depending on which mode. on creation it changes the tag of each object to "Obj" & whatever shape/maths number it is with a space in-between. But for some reason it won't create an array of sorts on a different timer tick event to move them.
I just need it to be able to constantly add and delete objects to shoot at, the adding and moving and deleting was working fine, but as in all my other cases with arrays this error just worms its way in and me and my seriously qualified IPT teacher can't figure it out. Why does it suddenly start doing it and how can I fix it? Attached is some of the code for the object move event.
Dim NumofObjsLeft As Integer = 0
For Each obj As Object In Me.Controls
If obj.Tag.Contains("Obj") Then
NumofObjsLeft += 1
If NumofObjsLeft <= 0 Then
Wav += 1
WaveStart = True
End If
Select Case GameMode
Case "Protector"
Select Case Curry
Case "Shapes"
If TypeOf obj Is PictureBox AndAlso obj.Tag.Contains("Obj") Then
obj.Left += 15
End If
Case "Maths"
If TypeOf obj Is Label AndAlso obj.tag.Contains("Obj") Then
obj.Left += 15
End If
End Select
EDIT: Just thought I'd add all the code for Shape Spawn and Move. I haven't finished the other half of the code for Shape Move cause I couldn't get the first part to work.
Private Sub tmrShapeSpawn_Tick(sender As System.Object, e As System.EventArgs) Handles tmrShapeSpawn.Tick
If WaveStart = True Then
Select Case Wav
Case 1
NumofObjs = Wave1
Case 2
NumofObjs = Wave2
Case 3
NumofObjs = Wave3
Case 4
NumofObjs = Wave4
Case 5
NumofObjs = Wave5
Case 6
NumofObjs = Wave6
End Select
If F < NumofObjs Then
Dim newPic As PictureBox = New PictureBox
Dim newLab As Label = New Label
Dim CorrectLabel As Label = New Label
Dim CorrectPictureBox As PictureBox = New PictureBox
CorrectPictureBox.Height = 75
CorrectPictureBox.Width = 100
CorrectLabel.AutoSize = True
CorrectLabel.BackColor = Color.Transparent
CorrectPictureBox.BackColor = Color.Transparent
CorrectPictureBox.SizeMode = PictureBoxSizeMode.Zoom
CorrectLabel.ForeColor = Color.Blue
CorrectLabel.Font = New Font("Goudy Stout", 60, FontStyle.Regular)
Select Case GameMode
Case "Protector"
Randomize()
If CorrectMade = False Then
CorrectLabel.Left = CorrectLabel.Width
CorrectPictureBox.Left = CorrectPictureBox.Width
CorrectLabel.Top = Int(Rnd() * (Me.Height - CorrectLabel.Height))
CorrectPictureBox.Top = Int(Rnd() * (Me.Height - CorrectPictureBox.Height))
End If
newLab.Left = newLab.Width
newPic.Left = newPic.Width
newLab.Top = Int(Rnd() * (Me.Height - newLab.Height))
newPic.Top = Int(Rnd() * (Me.Height - newPic.Height))
Case "Catcher"
Randomize()
If CorrectMade = False Then
CorrectLabel.Top = CorrectLabel.Height
CorrectPictureBox.Top = CorrectPictureBox.Height
CorrectLabel.Left = Int(Rnd() * (Me.Width - CorrectLabel.Width))
CorrectPictureBox.Left = Int(Rnd() * (Me.Width - CorrectPictureBox.Width))
End If
newLab.Top = newLab.Height
newPic.Top = newPic.Height
newLab.Left = Int(Rnd() * (Me.Width - newLab.Width))
newPic.Left = Int(Rnd() * (Me.Width - newPic.Width))
End Select
Select Case Curry
Case "Maths"
lblCriteria.Text = lstNum1.Items.Item(Wav - 1) & " " & lstOp.Items.Item(Wav - 1) & " " & lstNum2.Items.Item(Wav - 1) & " ="
If CorrectMade = False Then
CorrectLabel.Text = lstAns.Items.Item(Wav - 1)
CorrectLabel.Tag = "Obj " & lstAns.Items.Item(Wav - 1)
Me.Controls.Add(CorrectLabel)
CorrectLabel.BringToFront()
End If
Randomize()
newLab.Text = Int(Rnd() * 100)
newLab.Tag = "Obj " & newLab.Text
Me.Controls.Add(CorrectLabel)
CorrectLabel.BringToFront()
Case "Shapes"
Dim epahs As Integer
Dim sap As String
lblCriteria.Text = lstShape.Items.Item(Wav - 1)
If CorrectMade = False Then
CorrectPictureBox.ImageLocation = "Shapes\" & lstShape.Items.Item(Wav - 1) & ".png"
CorrectPictureBox.Tag = "Obj " & lstShape.Items.Item(Wav - 1)
Me.Controls.Add(CorrectPictureBox)
CorrectPictureBox.BringToFront()
End If
Randomize()
epahs = Int(Rnd() * 9)
Select Case epahs
Case 0
sap = "Square"
Case 1
sap = "Circle"
Case 2
sap = "Triangle"
Case 3
sap = "Rectangle"
Case 4
sap = "Oval"
Case 5
sap = "Hexagon"
Case 6
sap = "Star"
Case 7
sap = "Diamond"
Case 8
sap = "Trapezium"
Case 9
sap = "Rhombus"
End Select
newPic.ImageLocation = "Shapes\" & sap & ".png"
newPic.Tag = "Obj " & sap
newPic.SizeMode = PictureBoxSizeMode.Zoom
Me.Controls.Add(newPic)
newPic.BringToFront()
End Select
CorrectMade = True
F += 1
Else
WaveStart = False
End If
End If
End Sub
Private Sub tmrShapeMove_Tick(sender As System.Object, e As System.EventArgs) Handles tmrShapeMove.Tick
Dim CBA As Integer
Dim NumofObjsLeft As Integer = 0
For Each obj As Object In Me.Controls
If obj.Tag.Contains("Obj") Then
NumofObjsLeft += 1
If NumofObjsLeft <= 0 Then
Wav += 1
WaveStart = True
End If
Select Case GameMode
Case "Protector"
Select Case Curry
Case "Shapes"
If TypeOf obj Is PictureBox AndAlso obj.Tag.Contains("Obj") Then
obj.Left += 15
End If
Case "Maths"
If TypeOf obj Is Label AndAlso obj.tag.Contains("Obj") Then
obj.Left += 15
End If
End Select
For CBA = 0 To ABC Step 1
If Collision(obj, Missile(CBA)) Then
Missile(CBA).Visible = False
Missile(CBA).Enabled = False
If picExplosion1.Visible = True And picExplosion2.Visible = False And picExplosion3.Visible = False Then
picExplosion2.Visible = True
tmrExplosion2.Start()
CentreOn(obj, picExplosion2)
ElseIf picExplosion1.Visible = True And picExplosion2.Visible = True And picExplosion3.Visible = False Then
picExplosion3.Visible = True
tmrExplosion3.Start()
CentreOn(obj, picExplosion3)
ElseIf picExplosion1.Visible = False Then
picExplosion1.Visible = True
tmrExplosion1.Start()
CentreOn(obj, picExplosion1)
Else
End If
If obj.Tag.Contains(lblCriteria.Text) Then
lblScore.Text += 1
Else
lblLives.Text -= 1
End If
Me.Controls.Remove(obj)
End If
Next CBA
Case "Catcher"
Select Case Curry
Case "Shapes"
If TypeOf obj Is PictureBox AndAlso obj.Tag.Contains("Obj") Then
obj.Top += 15
End If
Case "Maths"
If TypeOf obj Is Label AndAlso obj.Tag.Contains("Obj") Then
obj.Top += 15
End If
End Select
If Collision(obj, picChar) Then
If obj.Tag.Contains(lblCriteria.Text) Then
Score += 1
Else
Lives -= 1
End If
Me.Controls.Remove(obj)
End If
End Select
End If
Next obj
End Sub
1) Use background worker instead of timer. Its built to interact correctly with the main form thread and handle control interactions a bit more cleanly. Timers are notoriously problematic for both main UI thread sync issues and exiting unexpectedly.
2) You have to handle the obj.Tag or obj being nothing. The easiest way is to add
If obj is Nothing OrElse String.IsNullOrWhitespace(obj.Tag) Then
Continue For
End If
...as the first statement after the For loop.
Righto, thanks to everyone who commented, replied. It seems to be working now with just that extra condition. Adding an if statement to check to see if the tag is empty before comparing the text.
Dim NumofObjsLeft As Integer = 0
For Each obj As Object In Me.Controls
If obj.Tag > "" Then
If obj.Tag.Contains("Obj") Then
NumofObjsLeft += 1
If NumofObjsLeft <= 0 Then
Wav += 1
WaveStart = True
End If
Select Case GameMode
Case "Protector"
Select Case Curry
ETC...................................................
In the form, everytime I click te button, a new textbox has to appear.
I want to make an Array with all of these textboxes.
The problem is, that al these textboxes have a dynamic name.
How do I have to put them in the Array?
Here's my code:
Set nieuwtxtingredient = Me.Controls.Add("Forms.Textbox.1", "Ingredient", True)
With nieuwtxtingredient
.Width = Me.txtIngredient0.Width
.Height = Me.txtIngredient0.Height
.Left = Me.txtIngredient0.Left
.Top = Me.txtIngredient0.Top + 30 * aantalBoxes
.Name = "txtIngredient" + CStr(aantalBoxes)
End With
Dim naam As String
Dim ingredientArray() As String
ReDim ingredientArray(1 To aantalBoxes)
ingredientArray(aantalBoxes) = **Me.txtIngredient0.Value**
In your code, you save a reference to your new textbox in nieuwtxtingredient.
You could save this reference in an array of Textbox and later read the name and value of each one.
This is how I suggest to revise your code :
Dim aantalBoxes As Integer
Dim ingredientArray() As Control
Private Sub btnVoegToe_Click()
Dim aantalRecepten As Integer
Dim Teller As Integer
aantalRecepten = Cells(2, Columns.Count).End(xlToLeft).Column
Cells(2, aantalRecepten + 2).Value = Me.txtNaamRecept.Value
For Teller = 1 To aantalBoxes ' <-- starts at 1, formula below adjusted
Cells(2 + Teller, aantalRecepten + 2).Value = ingredientArray(aantalBoxes).Value
Next Teller
End Sub
Private Sub btnVolgendeIngredient_Click()
aantalBoxes=aantalBoxes + 1 ' <-- must calculate the total
ReDim Preserve ingredientArray(aantalBoxes) ' <-- you had this in the earlier version
Set nieuwtxtingredient = Me.Controls.Add("Forms.Textbox.1", "Ingredient", True)
With nieuwtxtingredient
.Width = Me.txtIngredient0.Width
.Height = Me.txtIngredient0.Height
.Left = Me.txtIngredient0.Left
.Top = Me.txtIngredient0.Top + 30 * aantalBoxes
.Name = "txtIngredient" + CStr(aantalBoxes)
End With
' first Textbox is numbered 1
set ingredientArray(aantalBoxes) = nieuwtxtingredient ' <-- you had this in the earlier version
End Sub
See this example, http://jsfiddle.net/7zkzttpr/2/
$(document).ready(function() {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div><input type="text" name="mytext[]"/>Remove</div>'); //add input box
}
});
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
When ever you create a new textbox, give the same name to the textbox as an array so that you can get the values of all textboxes in an array.