Dynamically swap variables - vb.net

I'm working in Visual Basic 2010
Hitpoints1 -= Player1HandGroup(Number).DamageInteger
This is an example line of code in a game I'm designing that removes a given amount of hitpoints from player 1 depending on the item used in the array.
For each player I have different variables. They are
Hitpoints2, Hitpoints3, Hitpoints4, Hitpoints5 respective for each given player.
What I want to do is be able to dynamically change the variable to another variable so I do not have to copy my giant masses of decision logic.
The way the variable will be dynamically changed is via a custom dialog box with Player 1 through Player 5 as the buttons to decide who the player will attack. (I've already made the dialog box)
So if I hit player 2 in the dialog box Hitpoints1 will instead be Hitpoints2.
I'd like to dynamically change the variable, or be able to concatenate the last letter (numbers 1-5) onto "HitPoints" in the code, in effect being the same as dynamically changing the variable.
One person tried to have me do for each loop...
for n = 1 to 5
HitPoints = HitPoints & n
( your code in here with the HitPoints bit, which will now be HitPoints1)
next n
but that didn't work.

Related

Determine the Cell of a Table Layout Panel Controls are Contained in

I am working on a project in VB.Net, and am using a Table Layout Panel to allow for multiple windows to be open side by side with one another.
The issue I am dealing with right now is figuring out exactly which Column of the Table Layout Panel components are placed in at run time.
For example, let's say I have two Windows open, with 3 Columns. So there are controls in Columns 1 & 2, and Column 3 is empty. If I close the Window in Column 1, I want to detect the Column it was in, so that I can shift the Window in Column 2 over to Column 1. I'm trying to do this so I can resize the windows based on how many windows are being opened side by side.
However I can't seem to find a way to determine exactly while Column is the 'parent' Column. The parent container is the Table Layout Panel itself, but I don't know how to get the information I am looking for.
You can determine the position of a child control inside a TableLayoutPanel using its GetPositionFromControl() method, which will return a TableLayoutPanelCellPosition structure, identifying the Column and Row of the cell that a control is occupying:
Dim Position As TableLayoutPanelCellPosition =
TableLayoutPanel1.GetPositionFromControl([ControlName])
Position reports Position.Column and Position.Row as Integer values.
You can also detemine which child control is occupying a specified position, using the GetControlFromPosition() method:
Dim MyControl As Control = TableLayoutPanel1.GetControlFromPosition(0, 0)

Using varying labels in Userform code

I have a simulator with two userforms that transfer information back and forth between each other (one is MainForm, and the other is CreatureFinder).
When one of the deck slot buttons on MainForm is pushed (all 20 of which correspond to a creature in either your or the enemy's deck), it opens CreatureFinder so that you can choose which creature goes in the slot (and it also lets you edit certain parameters, like the creature's level).
The code shortened for simplicity:
If CreatureNumberLabel.Text = "1" Then
MainForm.YourCreature1Skill1Label.Text = Skill1Label.Text
MainForm.YourCreature1Skill2Label.Text = Skill2Label.Text
MainForm.YourCreature1Skill3Label.Text = Skill3Label.Text
End If
I want to remove the If Then statement, and to change "YourCreature1" to "YourCreature" & whatever value CreatureNumberLabel.Text is set to.
Should it be VBA, you can skip the
If CreatureNumberLabel.Text = "1" Then
And just type:
MainForm.Controls("YourCreature" & CreatureNumberLabel.Text & "Skill1Label").Text = Skill1Label.Text
And so on with Skill2Label.Text, Skill3Label.Text, etc...
You can use some kind of control arrays. On form load, create array(s) for controls and assign its elements (this is 20 lines of code per array), afterwards you can use them like next:
Dim CreatureNum As Integer = CInt(CreatureNumberLabel.Text) 'don't forget validation
MainFormYourCreatureSkillLabelArray(CreatureNum).Text = SkillLabel(CreatureNum).Text

Number Picker in Access / VBA

I am trying to put a number picker in a form in MS Access 2007. Here's an example of what I am trying to make:
I cannot find this in the default form controls, and have tried to make one myself using a listbox. Listboxes can be modified to look just like the number picker above, however the arrows only change the view, of the list, and not the actual selection (that is the value). For example, with the list box, if I have it range from 1 to 3, and default at 1 - when I change it to 2 via the arrows, the value of the listbox does not change, and is still one.
Does anyone know how to get a number picker in Access?
So you want to create a list of numbers and allow users to change the value displayed (AND stored as the control's value) using up and down arrows, such that they select the next or previous in the list.
I would suggest creating a text box and two buttons. Populate an array with the list of values. When a button is clicked it would:
A. Find the position in the array of any value already entered into the text box (eg loaded from a database)
B. Get the next or previous item from the array.
The array is populated as required (probably when the form is opened).
If you just need to allow the user to enter a whole integer number (ie a number spinner) you would do as follows:
Create one using a (locked) textbox and two buttons. Just add a textbox (name it something like txtValue) and two buttons (btnUp and btnDown), then add code like this to the Click event of those buttons:
Private Sub btnUp_Click()
Me.txtValue = Nz(Me.txtValue, 0) + 1
End Sub
Private Sub btnDown_Click()
Me.txtValue = Nz(Me.txtValue, 0) - 1
End Sub
You could add if statements to limit the data being entered
Or you can use a 3rd party control.
http://www.fmsinc.com/microsoftaccess/controls/components/spin-button/index.html
There are probably more, but be aware that using these sorts of controls in Access is unsupported, and there is no guarantee moving forward that they will work in Access. You're far better off using the native methods described earlier.

Wildcards and VBA on Rockwell FactoryTalk View SE

I'm extremely new to VBA (I did 3 weeks of it in grade 9, but that's about it). I'm an industrial PLC programmer by trade, but one of my applications has required me to unearth the painful memories of grade 9 IT for a Rockwell FactoryTalk View SE SCADA system.
My application is that there is a server, which stores graphic display, and multiple clients, which display these graphic displays. Depending on which client is being used, I want to make certain items visible or invisible to the user. I can do that in a basic sense, but I'm wondering if there's a more efficient way. Here's the code as it stands:
Private Sub Display_AnimationStart()
Dim HostName As String
HostName = Environ$("computername")
Select Case HostName
Case "CCSPE1X2"
Elements.Item("VBAControl_X2Only").Visible = True
Case "CCSPE1X3"
Elements.Item("VBAControl_X3Only").Visible = True
End Select
End Sub
This all works; I just group everything that should only be visible to the X2 client into a group called "VBAControl_X2Only", etc. But that then makes the graphic editor painful to work with, as there are bits and pieces from all over the display piled into one group, separated from the rest of the groups they should really belong to. I've decided that I'm better to take them out of the one big group and just add each item individually to the VBA code.
Then I thought, what if there's a way to use wildcards here? So any item that should only be visible on CCSPE1X2, I name "StartButton_X2Only" or "StopButton_X2Only", etc. And then I run a search for any items whose name ends with "X2Only", and make it invisble.
Im envisioning something like:
Dim ElementName As String
If Elements.Item("*").Name Like "*X2Only" Then
ElementName = Elements.Item("*").Name
Elements.Item(ElementName).Visible = True
End If
That doesn't work (I didn't really expect it to honestly); when it gets to If Elements.Item("*") it gives error 91 - object variable or with block variable not set.
Can anyone tell me if I'm close to the mark? Or if it's impossible? I don't even know if there's a way to make it search every item on the display, as it's not just an excel spreadsheet where I could tell it to search a column.
Thanks!
You will need to iterate through all of the individual Items. Try something like this:
For Each Item In Elements
If Item.Name Like "*X2Only" Then
Item.Visible = True
End If
Next Item

Computer-generated Variables VB.Net

This probably will seem like a simple process, but I'm new to programming, so I don't know how to do it.
I'm programming in VB.Net and I want the computer to generate a new set of global variables every time the user presses Button X. Naturally we don't know how many times the button will be pressed, which is why I need the computer to create the variables. Also, how do I tell the computer to assign a new number to each variable? For example:
ButtonX clicked first time:
NewVariable1 created
ButtonX clicked a second time:
NewVariable2 created.
I hope this makes sense. I can provide more details if necessary. I need the variables generated to be accessible from many different classes, and maybe even from one load to the next, if possible.
Have a look at the List(Of T) class. Create a list that is globally accessible and add another value each time the button is pressed.
globaList.Add(newValue)
This assumes that all the variables are of the same type.
Later on, you can access the list items (=variables) by their index. This retrieves the value that was added last:
Dim value = globalList(globalList.Count - 1)
You can also change values in the list:
globalList(index) = newValue