For my program it would be a whole lot easier to create the objects (buttons) in my form_load sub. I have 200+ buttons I need to create in a sequence. I have them creating themselves, but I can't figure out how to have the click sub handle them, I do have the handles statement for every button and a friend declaration for every button.
In my testing I found that if I created a single button with the same .name atribute as one of my self-generated ones it will work with the handles statement, so somewhere the .name attribute is different than the name of the object that you access it by.
I have it setup to create a "Temporary1" button object, store the needed info into the attributes, then change the name to the name that I want it to be (btnMine0_0, btnMine0_1, etc.), then finally add it to the form's controls. All of this is in a double loop, one for the first number in the name, the second loop for the second number.
I will post my code, if necessary, ASAP. It's just on my laptop and not with me right now.
Object nameThe actual name of an object
Page 1 of 1
12 Replies - 3891 Views - Last Post: 27 May 2010 - 05:40 AM
Replies To: Object name
#2
Re: Object name
Posted 27 April 2010 - 05:37 PM
What are you doing that needs 200 buttons on single form?
#3
Re: Object name
Posted 27 April 2010 - 06:08 PM
With that explanation it is quite confusing what you are trying to do. With the first part:
And the second part:
Which part of the 200 button objects are you trying to control with the one button and having a problem with?
Quote
I have them creating themselves, but I can't figure out how to have the click sub handle them
And the second part:
Quote
In my testing I found that if I created a single button with the same .name atribute as one of my self-generated ones it will work with the handles statement
Which part of the 200 button objects are you trying to control with the one button and having a problem with?
#4
Re: Object name
Posted 27 April 2010 - 08:37 PM
All the buttons can point to a single sub/event. You use the Addhandler and point them all to this sub. In the sub you cast out the sender object to tell which one is being used. This sub's signature must match the click event sub's signature. Otherwise enjoy your 200+ buttons. There might be a better idea than having 200+ buttons BTW. Also post what you are doing/attempting and we may come up with a different plan or maybe not.
This post has been edited by hawkvalley1: 27 April 2010 - 08:37 PM
#5
Re: Object name
Posted 28 April 2010 - 09:01 AM
Sorry for the delay, had to get my code. It will make more sense when you see the code.
Form1 code:
designer code:
Sorry I had removed all my comments (this is a group project, and one of our parners can't stand comments, so I removed them when I sent it to him, but forgot to make a duplicate first.)
I am trying to make a "BattleShip" game. I know there is an easier way of doing it, but with it being a group project I kindof have to do it the way they want. Each button is one space on the game grid. I haven't put in any coding for the actual game yet, just trying to establish the grid. It would be better for me if I could instanciate them myself (like or similar to how I have it set up) since they are in sequencial order. But after checking it more I found that only the last (far bottom-left) button actually worked, but it only works if I add a Friend WithEvents Temporari ... and a sub that handles the Temporari button, even though I changed the name of the button to "btnMine0_0", etc.
Does this make more sense?
Form1 code:
Public Class Form1
#Region "Friend Statements"
Friend WithEvents btnMine1_1 As New System.Windows.Forms.Button
Friend WithEvents btnMine2_1 As New System.Windows.Forms.Button
Friend WithEvents btnMine3_1 As New System.Windows.Forms.Button
Friend WithEvents btnMine4_1 As New System.Windows.Forms.Button
Friend WithEvents btnMine5_1 As New System.Windows.Forms.Button
Friend WithEvents btnMine6_1 As New System.Windows.Forms.Button
Friend WithEvents btnMine7_1 As New System.Windows.Forms.Button
Friend WithEvents btnMine8_1 As New System.Windows.Forms.Button
Friend WithEvents btnMine9_1 As New System.Windows.Forms.Button
Friend WithEvents btnMine0_1 As New System.Windows.Forms.Button
Friend WithEvents btnMine1_2 As New System.Windows.Forms.Button
Friend WithEvents btnMine2_2 As New System.Windows.Forms.Button
Friend WithEvents btnMine3_2 As New System.Windows.Forms.Button
Friend WithEvents btnMine4_2 As New System.Windows.Forms.Button
Friend WithEvents btnMine5_2 As New System.Windows.Forms.Button
Friend WithEvents btnMine6_2 As New System.Windows.Forms.Button
Friend WithEvents btnMine7_2 As New System.Windows.Forms.Button
Friend WithEvents btnMine8_2 As New System.Windows.Forms.Button
Friend WithEvents btnMine9_2 As New System.Windows.Forms.Button
Friend WithEvents btnMine0_2 As New System.Windows.Forms.Button
Friend WithEvents btnMine1_3 As New System.Windows.Forms.Button
Friend WithEvents btnMine2_3 As New System.Windows.Forms.Button
Friend WithEvents btnMine3_3 As New System.Windows.Forms.Button
Friend WithEvents btnMine4_3 As New System.Windows.Forms.Button
Friend WithEvents btnMine5_3 As New System.Windows.Forms.Button
Friend WithEvents btnMine6_3 As New System.Windows.Forms.Button
Friend WithEvents btnMine7_3 As New System.Windows.Forms.Button
Friend WithEvents btnMine8_3 As New System.Windows.Forms.Button
Friend WithEvents btnMine9_3 As New System.Windows.Forms.Button
Friend WithEvents btnMine0_3 As New System.Windows.Forms.Button
Friend WithEvents btnMine1_4 As New System.Windows.Forms.Button
Friend WithEvents btnMine2_4 As New System.Windows.Forms.Button
Friend WithEvents btnMine3_4 As New System.Windows.Forms.Button
Friend WithEvents btnMine4_4 As New System.Windows.Forms.Button
Friend WithEvents btnMine5_4 As New System.Windows.Forms.Button
Friend WithEvents btnMine6_4 As New System.Windows.Forms.Button
Friend WithEvents btnMine7_4 As New System.Windows.Forms.Button
Friend WithEvents btnMine8_4 As New System.Windows.Forms.Button
Friend WithEvents btnMine9_4 As New System.Windows.Forms.Button
Friend WithEvents btnMine0_4 As New System.Windows.Forms.Button
Friend WithEvents btnMine1_5 As New System.Windows.Forms.Button
Friend WithEvents btnMine2_5 As New System.Windows.Forms.Button
Friend WithEvents btnMine3_5 As New System.Windows.Forms.Button
Friend WithEvents btnMine4_5 As New System.Windows.Forms.Button
Friend WithEvents btnMine5_5 As New System.Windows.Forms.Button
Friend WithEvents btnMine6_5 As New System.Windows.Forms.Button
Friend WithEvents btnMine7_5 As New System.Windows.Forms.Button
Friend WithEvents btnMine8_5 As New System.Windows.Forms.Button
Friend WithEvents btnMine9_5 As New System.Windows.Forms.Button
Friend WithEvents btnMine0_5 As New System.Windows.Forms.Button
Friend WithEvents btnMine1_6 As New System.Windows.Forms.Button
Friend WithEvents btnMine2_6 As New System.Windows.Forms.Button
Friend WithEvents btnMine3_6 As New System.Windows.Forms.Button
Friend WithEvents btnMine4_6 As New System.Windows.Forms.Button
Friend WithEvents btnMine5_6 As New System.Windows.Forms.Button
Friend WithEvents btnMine6_6 As New System.Windows.Forms.Button
Friend WithEvents btnMine7_6 As New System.Windows.Forms.Button
Friend WithEvents btnMine8_6 As New System.Windows.Forms.Button
Friend WithEvents btnMine9_6 As New System.Windows.Forms.Button
Friend WithEvents btnMine0_6 As New System.Windows.Forms.Button
Friend WithEvents btnMine1_7 As New System.Windows.Forms.Button
Friend WithEvents btnMine2_7 As New System.Windows.Forms.Button
Friend WithEvents btnMine3_7 As New System.Windows.Forms.Button
Friend WithEvents btnMine4_7 As New System.Windows.Forms.Button
Friend WithEvents btnMine5_7 As New System.Windows.Forms.Button
Friend WithEvents btnMine6_7 As New System.Windows.Forms.Button
Friend WithEvents btnMine7_7 As New System.Windows.Forms.Button
Friend WithEvents btnMine8_7 As New System.Windows.Forms.Button
Friend WithEvents btnMine9_7 As New System.Windows.Forms.Button
Friend WithEvents btnMine0_7 As New System.Windows.Forms.Button
Friend WithEvents btnMine1_8 As New System.Windows.Forms.Button
Friend WithEvents btnMine2_8 As New System.Windows.Forms.Button
Friend WithEvents btnMine3_8 As New System.Windows.Forms.Button
Friend WithEvents btnMine4_8 As New System.Windows.Forms.Button
Friend WithEvents btnMine5_8 As New System.Windows.Forms.Button
Friend WithEvents btnMine6_8 As New System.Windows.Forms.Button
Friend WithEvents btnMine7_8 As New System.Windows.Forms.Button
Friend WithEvents btnMine8_8 As New System.Windows.Forms.Button
Friend WithEvents btnMine9_8 As New System.Windows.Forms.Button
Friend WithEvents btnMine0_8 As New System.Windows.Forms.Button
Friend WithEvents btnMine1_9 As New System.Windows.Forms.Button
Friend WithEvents btnMine2_9 As New System.Windows.Forms.Button
Friend WithEvents btnMine3_9 As New System.Windows.Forms.Button
Friend WithEvents btnMine4_9 As New System.Windows.Forms.Button
Friend WithEvents btnMine5_9 As New System.Windows.Forms.Button
Friend WithEvents btnMine6_9 As New System.Windows.Forms.Button
Friend WithEvents btnMine7_9 As New System.Windows.Forms.Button
Friend WithEvents btnMine8_9 As New System.Windows.Forms.Button
Friend WithEvents btnMine9_9 As New System.Windows.Forms.Button
Friend WithEvents btnMine0_9 As New System.Windows.Forms.Button
Friend WithEvents btnMine1_0 As New System.Windows.Forms.Button
Friend WithEvents btnMine2_0 As New System.Windows.Forms.Button
Friend WithEvents btnMine3_0 As New System.Windows.Forms.Button
Friend WithEvents btnMine4_0 As New System.Windows.Forms.Button
Friend WithEvents btnMine5_0 As New System.Windows.Forms.Button
Friend WithEvents btnMine6_0 As New System.Windows.Forms.Button
Friend WithEvents btnMine7_0 As New System.Windows.Forms.Button
Friend WithEvents btnMine8_0 As New System.Windows.Forms.Button
Friend WithEvents btnMine9_0 As New System.Windows.Forms.Button
Friend WithEvents btnMine0_0 As New System.Windows.Forms.Button
Friend WithEvents btnYours1_1 As New System.Windows.Forms.Button
Friend WithEvents btnYours2_1 As New System.Windows.Forms.Button
Friend WithEvents btnYours3_1 As New System.Windows.Forms.Button
Friend WithEvents btnYours4_1 As New System.Windows.Forms.Button
Friend WithEvents btnYours5_1 As New System.Windows.Forms.Button
Friend WithEvents btnYours6_1 As New System.Windows.Forms.Button
Friend WithEvents btnYours7_1 As New System.Windows.Forms.Button
Friend WithEvents btnYours8_1 As New System.Windows.Forms.Button
Friend WithEvents btnYours9_1 As New System.Windows.Forms.Button
Friend WithEvents btnYours0_1 As New System.Windows.Forms.Button
Friend WithEvents btnYours1_2 As New System.Windows.Forms.Button
Friend WithEvents btnYours2_2 As New System.Windows.Forms.Button
Friend WithEvents btnYours3_2 As New System.Windows.Forms.Button
Friend WithEvents btnYours4_2 As New System.Windows.Forms.Button
Friend WithEvents btnYours5_2 As New System.Windows.Forms.Button
Friend WithEvents btnYours6_2 As New System.Windows.Forms.Button
Friend WithEvents btnYours7_2 As New System.Windows.Forms.Button
Friend WithEvents btnYours8_2 As New System.Windows.Forms.Button
Friend WithEvents btnYours9_2 As New System.Windows.Forms.Button
Friend WithEvents btnYours0_2 As New System.Windows.Forms.Button
Friend WithEvents btnYours1_3 As New System.Windows.Forms.Button
Friend WithEvents btnYours2_3 As New System.Windows.Forms.Button
Friend WithEvents btnYours3_3 As New System.Windows.Forms.Button
Friend WithEvents btnYours4_3 As New System.Windows.Forms.Button
Friend WithEvents btnYours5_3 As New System.Windows.Forms.Button
Friend WithEvents btnYours6_3 As New System.Windows.Forms.Button
Friend WithEvents btnYours7_3 As New System.Windows.Forms.Button
Friend WithEvents btnYours8_3 As New System.Windows.Forms.Button
Friend WithEvents btnYours9_3 As New System.Windows.Forms.Button
Friend WithEvents btnYours0_3 As New System.Windows.Forms.Button
Friend WithEvents btnYours1_4 As New System.Windows.Forms.Button
Friend WithEvents btnYours2_4 As New System.Windows.Forms.Button
Friend WithEvents btnYours3_4 As New System.Windows.Forms.Button
Friend WithEvents btnYours4_4 As New System.Windows.Forms.Button
Friend WithEvents btnYours5_4 As New System.Windows.Forms.Button
Friend WithEvents btnYours6_4 As New System.Windows.Forms.Button
Friend WithEvents btnYours7_4 As New System.Windows.Forms.Button
Friend WithEvents btnYours8_4 As New System.Windows.Forms.Button
Friend WithEvents btnYours9_4 As New System.Windows.Forms.Button
Friend WithEvents btnYours0_4 As New System.Windows.Forms.Button
Friend WithEvents btnYours1_5 As New System.Windows.Forms.Button
Friend WithEvents btnYours2_5 As New System.Windows.Forms.Button
Friend WithEvents btnYours3_5 As New System.Windows.Forms.Button
Friend WithEvents btnYours4_5 As New System.Windows.Forms.Button
Friend WithEvents btnYours5_5 As New System.Windows.Forms.Button
Friend WithEvents btnYours6_5 As New System.Windows.Forms.Button
Friend WithEvents btnYours7_5 As New System.Windows.Forms.Button
Friend WithEvents btnYours8_5 As New System.Windows.Forms.Button
Friend WithEvents btnYours9_5 As New System.Windows.Forms.Button
Friend WithEvents btnYours0_5 As New System.Windows.Forms.Button
Friend WithEvents btnYours1_6 As New System.Windows.Forms.Button
Friend WithEvents btnYours2_6 As New System.Windows.Forms.Button
Friend WithEvents btnYours3_6 As New System.Windows.Forms.Button
Friend WithEvents btnYours4_6 As New System.Windows.Forms.Button
Friend WithEvents btnYours5_6 As New System.Windows.Forms.Button
Friend WithEvents btnYours6_6 As New System.Windows.Forms.Button
Friend WithEvents btnYours7_6 As New System.Windows.Forms.Button
Friend WithEvents btnYours8_6 As New System.Windows.Forms.Button
Friend WithEvents btnYours9_6 As New System.Windows.Forms.Button
Friend WithEvents btnYours0_6 As New System.Windows.Forms.Button
Friend WithEvents btnYours1_7 As New System.Windows.Forms.Button
Friend WithEvents btnYours2_7 As New System.Windows.Forms.Button
Friend WithEvents btnYours3_7 As New System.Windows.Forms.Button
Friend WithEvents btnYours4_7 As New System.Windows.Forms.Button
Friend WithEvents btnYours5_7 As New System.Windows.Forms.Button
Friend WithEvents btnYours6_7 As New System.Windows.Forms.Button
Friend WithEvents btnYours7_7 As New System.Windows.Forms.Button
Friend WithEvents btnYours8_7 As New System.Windows.Forms.Button
Friend WithEvents btnYours9_7 As New System.Windows.Forms.Button
Friend WithEvents btnYours0_7 As New System.Windows.Forms.Button
Friend WithEvents btnYours1_8 As New System.Windows.Forms.Button
Friend WithEvents btnYours2_8 As New System.Windows.Forms.Button
Friend WithEvents btnYours3_8 As New System.Windows.Forms.Button
Friend WithEvents btnYours4_8 As New System.Windows.Forms.Button
Friend WithEvents btnYours5_8 As New System.Windows.Forms.Button
Friend WithEvents btnYours6_8 As New System.Windows.Forms.Button
Friend WithEvents btnYours7_8 As New System.Windows.Forms.Button
Friend WithEvents btnYours8_8 As New System.Windows.Forms.Button
Friend WithEvents btnYours9_8 As New System.Windows.Forms.Button
Friend WithEvents btnYours0_8 As New System.Windows.Forms.Button
Friend WithEvents btnYours1_9 As New System.Windows.Forms.Button
Friend WithEvents btnYours2_9 As New System.Windows.Forms.Button
Friend WithEvents btnYours3_9 As New System.Windows.Forms.Button
Friend WithEvents btnYours4_9 As New System.Windows.Forms.Button
Friend WithEvents btnYours5_9 As New System.Windows.Forms.Button
Friend WithEvents btnYours6_9 As New System.Windows.Forms.Button
Friend WithEvents btnYours7_9 As New System.Windows.Forms.Button
Friend WithEvents btnYours8_9 As New System.Windows.Forms.Button
Friend WithEvents btnYours9_9 As New System.Windows.Forms.Button
Friend WithEvents btnYours0_9 As New System.Windows.Forms.Button
Friend WithEvents btnYours1_0 As New System.Windows.Forms.Button
Friend WithEvents btnYours2_0 As New System.Windows.Forms.Button
Friend WithEvents btnYours3_0 As New System.Windows.Forms.Button
Friend WithEvents btnYours4_0 As New System.Windows.Forms.Button
Friend WithEvents btnYours5_0 As New System.Windows.Forms.Button
Friend WithEvents btnYours6_0 As New System.Windows.Forms.Button
Friend WithEvents btnYours7_0 As New System.Windows.Forms.Button
Friend WithEvents btnYours8_0 As New System.Windows.Forms.Button
Friend WithEvents btnYours9_0 As New System.Windows.Forms.Button
Friend WithEvents btnYours0_0 As New System.Windows.Forms.Button
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
InstanciateButtonGrid(25, 25, "btnMine")
InstanciateButtonGrid(400, 25, "btnYours")
End Sub
Private Sub InstanciateButtonGrid(ByVal xroot As Integer, ByVal yroot As Integer, ByVal startname As String)
Dim clm As Integer = 0
Dim rownum As Integer = 0
Dim x As Integer = xroot
Dim y As Integer = yroot
For clm = 0 To 10
For rownum = 0 To 10
Dim strname As String
If rownum = 10 And clm = 10 Then
strname = startname & "0_0/>"
ElseIf rownum = 10 Then
strname = startname & "0_" & clm
ElseIf clm = 10 Then
strname = startname & rownum & "_0"
Else
strname = startname & rownum & "_" & clm
End If
Dim Temporari As New System.Windows.Forms.Button
Temporari.Location = New System.Drawing.Point(x, y)
Temporari.Size = New System.Drawing.Size(25, 25)
Temporari.TabIndex = 1
Temporari.Text = ""
Temporari.UseVisualStyleBackColor = True
Temporari.Visible = True
Temporari.Enabled = True
Temporari.Tag = rownum & "_" & clm
Temporari.Name = strname
Me.Controls.Add(Temporari)
x += 25
Next rownum
y += 25
x = xroot
rownum = 0
Next clm
End Sub
Private Sub MineButtons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMine1_1.Click, btnMine1_2.Click, btnMine1_3.Click, btnMine1_4.Click, btnMine1_5.Click, btnMine1_6.Click, btnMine1_7.Click, btnMine1_8.Click, btnMine1_9.Click, btnMine1_0.Click, _
btnMine2_1.Click, btnMine2_2.Click, btnMine2_3.Click, btnMine2_4.Click, btnMine2_5.Click, btnMine2_6.Click, btnMine2_7.Click, btnMine2_8.Click, btnMine2_9.Click, btnMine2_0.Click, _
btnMine3_1.Click, btnMine3_2.Click, btnMine3_3.Click, btnMine3_4.Click, btnMine3_5.Click, btnMine3_6.Click, btnMine3_7.Click, btnMine3_8.Click, btnMine3_9.Click, btnMine3_0.Click, _
btnMine4_1.Click, btnMine4_2.Click, btnMine4_3.Click, btnMine4_4.Click, btnMine4_5.Click, btnMine4_6.Click, btnMine4_7.Click, btnMine4_8.Click, btnMine4_9.Click, btnMine4_0.Click, _
btnMine5_1.Click, btnMine5_2.Click, btnMine5_3.Click, btnMine5_4.Click, btnMine5_5.Click, btnMine5_6.Click, btnMine5_7.Click, btnMine5_8.Click, btnMine5_9.Click, btnMine5_0.Click, _
btnMine6_1.Click, btnMine6_2.Click, btnMine6_3.Click, btnMine6_4.Click, btnMine6_5.Click, btnMine6_6.Click, btnMine6_7.Click, btnMine6_8.Click, btnMine6_9.Click, btnMine6_0.Click, _
btnMine7_1.Click, btnMine7_2.Click, btnMine7_3.Click, btnMine7_4.Click, btnMine7_5.Click, btnMine7_6.Click, btnMine7_7.Click, btnMine7_8.Click, btnMine7_9.Click, btnMine7_0.Click, _
btnMine8_1.Click, btnMine8_2.Click, btnMine8_3.Click, btnMine8_4.Click, btnMine8_5.Click, btnMine8_6.Click, btnMine8_7.Click, btnMine8_8.Click, btnMine8_9.Click, btnMine8_0.Click, _
btnMine9_1.Click, btnMine9_2.Click, btnMine9_3.Click, btnMine9_4.Click, btnMine9_5.Click, btnMine9_6.Click, btnMine9_7.Click, btnMine9_8.Click, btnMine9_9.Click, btnMine9_0.Click, _
btnMine0_1.Click, btnMine0_2.Click, btnMine0_3.Click, btnMine0_4.Click, btnMine0_5.Click, btnMine0_6.Click, btnMine0_7.Click, btnMine0_8.Click, btnMine0_9.Click, btnMine0_0.Click
End Sub
Private Sub YoursButtons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnYours1_1.Click, btnYours1_2.Click, btnYours1_3.Click, btnYours1_4.Click, btnYours1_5.Click, btnYours1_6.Click, btnYours1_7.Click, btnYours1_8.Click, btnYours1_9.Click, btnYours1_0.Click, _
btnYours2_1.Click, btnYours2_2.Click, btnYours2_3.Click, btnYours2_4.Click, btnYours2_5.Click, btnYours2_6.Click, btnYours2_7.Click, btnYours2_8.Click, btnYours2_9.Click, btnYours2_0.Click, _
btnYours3_1.Click, btnYours3_2.Click, btnYours3_3.Click, btnYours3_4.Click, btnYours3_5.Click, btnYours3_6.Click, btnYours3_7.Click, btnYours3_8.Click, btnYours3_9.Click, btnYours3_0.Click, _
btnYours4_1.Click, btnYours4_2.Click, btnYours4_3.Click, btnYours4_4.Click, btnYours4_5.Click, btnYours4_6.Click, btnYours4_7.Click, btnYours4_8.Click, btnYours4_9.Click, btnYours4_0.Click, _
btnYours5_1.Click, btnYours5_2.Click, btnYours5_3.Click, btnYours5_4.Click, btnYours5_5.Click, btnYours5_6.Click, btnYours5_7.Click, btnYours5_8.Click, btnYours5_9.Click, btnYours5_0.Click, _
btnYours6_1.Click, btnYours6_2.Click, btnYours6_3.Click, btnYours6_4.Click, btnYours6_5.Click, btnYours6_6.Click, btnYours6_7.Click, btnYours6_8.Click, btnYours6_9.Click, btnYours6_0.Click, _
btnYours7_1.Click, btnYours7_2.Click, btnYours7_3.Click, btnYours7_4.Click, btnYours7_5.Click, btnYours7_6.Click, btnYours7_7.Click, btnYours7_8.Click, btnYours7_9.Click, btnYours7_0.Click, _
btnYours8_1.Click, btnYours8_2.Click, btnYours8_3.Click, btnYours8_4.Click, btnYours8_5.Click, btnYours8_6.Click, btnYours8_7.Click, btnYours8_8.Click, btnYours8_9.Click, btnYours8_0.Click, _
btnYours9_1.Click, btnYours9_2.Click, btnYours9_3.Click, btnYours9_4.Click, btnYours9_5.Click, btnYours9_6.Click, btnYours9_7.Click, btnYours9_8.Click, btnYours9_9.Click, btnYours9_0.Click, _
btnYours0_1.Click, btnYours0_2.Click, btnYours0_3.Click, btnYours0_4.Click, btnYours0_5.Click, btnYours0_6.Click, btnYours0_7.Click, btnYours0_8.Click, btnYours0_9.Click, btnYours0_0.Click
End Sub
End Class
designer code:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
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.SuspendLayout()
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(726, 429)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
End Class
Sorry I had removed all my comments (this is a group project, and one of our parners can't stand comments, so I removed them when I sent it to him, but forgot to make a duplicate first.)
I am trying to make a "BattleShip" game. I know there is an easier way of doing it, but with it being a group project I kindof have to do it the way they want. Each button is one space on the game grid. I haven't put in any coding for the actual game yet, just trying to establish the grid. It would be better for me if I could instanciate them myself (like or similar to how I have it set up) since they are in sequencial order. But after checking it more I found that only the last (far bottom-left) button actually worked, but it only works if I add a Friend WithEvents Temporari ... and a sub that handles the Temporari button, even though I changed the name of the button to "btnMine0_0", etc.
Does this make more sense?
This post has been edited by higgsch: 28 April 2010 - 09:03 AM
#6
Re: Object name
Posted 28 April 2010 - 09:22 AM
Not really. And I don't want to confuse you with anything, but if I were to make something like that, drawing it with controls, I would use a data grid view with check boxes. Then each time something is checked it would be easier to validate the column and row index against the other player.
#7
Re: Object name
Posted 28 April 2010 - 10:17 AM
yes that would be easier, but then you have only 2 states (checked and unchecked)
I need 3 (hit, miss, unhit section of a boat)
I need 3 (hit, miss, unhit section of a boat)
#8
Re: Object name
Posted 28 April 2010 - 10:20 AM
No, the check is for if you have tried to use it or not. The back color of each item is the same. But when it is checked, it lets you know if it is a hit or a miss by setting the back color for that cell to red for hit, or white for miss.
The check box would only be for marking if you are attempting to "fire" at that spot. I hope the redneck explanation makes sense to someone besides me
The check box would only be for marking if you are attempting to "fire" at that spot. I hope the redneck explanation makes sense to someone besides me
#9
Re: Object name
Posted 28 April 2010 - 11:02 AM
I would use GDI+ and draw the objects. You would make a class object that could be a rectangle or ellipsis with custom properties and store these in a collection. Then you draw these on the control you want to use and when you are looking for a hit - it is as simple as testing the coordinates. Pseudo-code:
You will be able to draw Images and other stuff as well.
For Each r As Rectangle In rectCollection
If r.Contains(e.x,e.y) Then
' a hit
Else
' a miss
End If
Next
You will be able to draw Images and other stuff as well.
#10
Re: Object name
Posted 28 April 2010 - 04:17 PM
Ok, I see where you are comming from, but like I said this is a preassigned group project, I cannot change the way it is set up. I just need to set it up the way they want it set up. They want buttons, but preferably not 50 pages of instanciation code, which is why I am even attepting it this way.
Did a little bit more testing, I actually have 400 buttons, but only 200 on the form. I know the friend statements will "dim" the object, but I thought that if I change the .name property, it would act as that button. Appearently not. The word that you use in your code (to call an object) is different than the .name attribute of that object.
Example:
I need a way (if possible) to make the .name attribute the calling word in my loop, as posted in previous posts.
Worst case sinerio; I just hard code all 200 buttons, and they will have do deal with the 50 pgs of instanciation code.
Did a little bit more testing, I actually have 400 buttons, but only 200 on the form. I know the friend statements will "dim" the object, but I thought that if I change the .name property, it would act as that button. Appearently not. The word that you use in your code (to call an object) is different than the .name attribute of that object.
Example:
dim bob as new system.windows.forms.button 'bob is the calling word bob.name = "bill" 'bill is the .name attribute 'you would still use "bob" to call the button named "bill" bob.text = "I am Bob."
I need a way (if possible) to make the .name attribute the calling word in my loop, as posted in previous posts.
Worst case sinerio; I just hard code all 200 buttons, and they will have do deal with the 50 pgs of instanciation code.
#11
Re: Object name
Posted 29 April 2010 - 05:16 AM
Sorry I didn't get back yet, my computer locked up
I think to get anywhere with this you might start off smaller, perhaps only 5 or 10 buttons on the form, trying to see if you can't call the .Name somehow that way. I'm still having problems simply understanding what you are wanting to do...but that's cause I'm an idiot.
Looking at it another way, is there any way you could:
No, I have no clue if this works, but I think you are trying to loop through each of your buttons getting the next button each time, then doing something with it.
I think to get anywhere with this you might start off smaller, perhaps only 5 or 10 buttons on the form, trying to see if you can't call the .Name somehow that way. I'm still having problems simply understanding what you are wanting to do...but that's cause I'm an idiot.
Looking at it another way, is there any way you could:
Dim i As Integer = 1
For Each bob.name on the form
bob.name = CStr("bill_" & i)
i += 1
Next
No, I have no clue if this works, but I think you are trying to loop through each of your buttons getting the next button each time, then doing something with it.
#12
Re: Object name
Posted 25 May 2010 - 10:48 AM
maybe an explanation in a more round about way:
from now on I will refer to the .name property as "name" (i.e. "the name of the object" refers to "the .name property of the object")
I will also refer to the code that we use to call an object as the title.
Because these two things are diferent and not interchange able.
Example:
rdoanswer is the title (it cannot be accessed as a string).
"rdoreply" becomes the name.
now to call the radiobutton, I would use the title:
but the name still remains the same:
form1.text would then be the name: rdoreply
but you cannot call the radiobutton with the name:
this would give you an error, unless you have another radiobutton titled rdoreply. because rdoreply doesn't exist. even though rdoreply is the name of the radiobutton, you can't call the radiobutton with the name. you have to use the title.
For my problem, I need a way to modify the title of the object. not the name.
personally, I don't think this is even possible.
here is an example to think about. If you could modify the title with the .name property. check it out.
example:
if you could modify the title with the .name property, it would enter the btntry_click sub not the btntest_click sub.
but if you can't (which is true), it would enter the btntest_click sub.
so, I have officially decided:
the .name property is only a string property of the object's class. exactly like the .tag property. It's only use is differentiating between two object. You can use the .name property in replacement for the .tag property (if you want), but you can still use the .tag property instead. the .name property is in no way tied to the title of the object. If you want to change the title, you have to find a diferent way of doing it. Don't bother with the .name property.
but I still need a way to change the title. (I've just removed the .name property from my list of "try this's")
from now on I will refer to the .name property as "name" (i.e. "the name of the object" refers to "the .name property of the object")
I will also refer to the code that we use to call an object as the title.
Because these two things are diferent and not interchange able.
Example:
Dim rdoanswer as radiobutton
rdoanswer is the title (it cannot be accessed as a string).
rdoanswer.name = "rdoreply"
"rdoreply" becomes the name.
now to call the radiobutton, I would use the title:
if [u]rdoanswer[/u].checked = true then
but the name still remains the same:
form1.text = rdoanswer.[b]name[/b]
form1.text would then be the name: rdoreply
but you cannot call the radiobutton with the name:
if rdoreply.checked = true then
this would give you an error, unless you have another radiobutton titled rdoreply. because rdoreply doesn't exist. even though rdoreply is the name of the radiobutton, you can't call the radiobutton with the name. you have to use the title.
For my problem, I need a way to modify the title of the object. not the name.
personally, I don't think this is even possible.
here is an example to think about. If you could modify the title with the .name property. check it out.
example:
dim btntest as button
sub form1_load...
btntest.name = "btntry"
end sub
sub btntest_click...
msgbox("btntest")
end sub
sub btntry_click...
msgbox("btntry")
end sub
if you could modify the title with the .name property, it would enter the btntry_click sub not the btntest_click sub.
but if you can't (which is true), it would enter the btntest_click sub.
so, I have officially decided:
the .name property is only a string property of the object's class. exactly like the .tag property. It's only use is differentiating between two object. You can use the .name property in replacement for the .tag property (if you want), but you can still use the .tag property instead. the .name property is in no way tied to the title of the object. If you want to change the title, you have to find a diferent way of doing it. Don't bother with the .name property.
but I still need a way to change the title. (I've just removed the .name property from my list of "try this's")
This post has been edited by higgsch: 25 May 2010 - 10:49 AM
#13
Re: Object name
Posted 27 May 2010 - 05:40 AM
So my question is, did you drag the buttons on the form or did you create them at run time?
If you created them yourself, you will need to create a handler for them and use the sender to determine which button is clicked.
If you created them using the IDE, you will need to append the handles to the sub and then use the sender to determine which button is clicked.
Example from creating at runtime.
Example of the same thing only created from dragging the button onto the form
Notice in the second example the Sub is called Button1_Click (which you could name anything), but it is calling this sub regardless of which button you click because both buttons are being specified in the Handles Private Sub Button1_Click.....Handles Button1.Click, Button2.Click
If you created them yourself, you will need to create a handler for them and use the sender to determine which button is clicked.
If you created them using the IDE, you will need to append the handles to the sub and then use the sender to determine which button is clicked.
Example from creating at runtime.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim BTN As Button
For x As Integer = 1 To 2
BTN = New Button
BTN.Name = "Button" & x
BTN.Top = 10 + (20 * x)
BTN.Left = 10
AddHandler BTN.Click, AddressOf ClickedBTN
Me.Controls.Add(BTN)
Next
End Sub
Private Sub ClickedBTN(ByVal sender As System.Object, ByVal e As System.EventArgs)
'MsgBox("You Clicked: " & CType(sender, Button).Name)
Select Case CType(sender, Button).Name
Case "Button1"
MessageBox.Show("Here is where you put code for button 1")
Case "Button2"
MessageBox.Show("Here is where you put code for button 2")
End Select
End Sub
Example of the same thing only created from dragging the button onto the form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click
Select Case CType(sender, Button).Name
Case "Button1"
MessageBox.Show("Here is where you put code for Button 1")
Case "Button2"
MessageBox.Show("Here is where you put code for Button 2")
End Select
End Sub
Notice in the second example the Sub is called Button1_Click (which you could name anything), but it is calling this sub regardless of which button you click because both buttons are being specified in the Handles Private Sub Button1_Click.....Handles Button1.Click, Button2.Click
This post has been edited by CharlieMay: 27 May 2010 - 05:41 AM
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote






|