Beginner question - VB - change a button based on an integer - vb.net

I have a form with a large number of buttons on it, each named btn1 through btn25. I have another button that is generating a random number and saving it to an integer variable intDrawn.
I'd like to know if there's a simple way to alter a particular button based on the result in intDrawn; if intDrawn = 5, then I want to change the font in btn5, for example.
Is there a way to alter a control programmatically like this? I'm using Visual Basic Express 2008.

It sounds like you'd be better to use a control array. Give your buttons the same name and then use the integer result to change the font for that particular control number in the array.
http://msdn.microsoft.com/en-us/library/kxt4418a%28VS.80%29.aspx - VB6
http://msdn.microsoft.com/en-us/library/aa289500%28VS.71%29.aspx - VB.Net

Create a control array of buttons, and then use the index into this array to alter a particular button.
Control Arrays

There is also a "stupid" way to do this. Add an invisible textbox and after getting your random number you can just text1.text = "btn" + randomnumber, and then change the color or whatever you wish using text1.text.

Control Array is the better choice, but you could also achieve it with reflection.

Related

Fill a boolean array from checkbox control array

My program creates an array of checkboxes at runtime as shown below:
For Looper = 0 To 36
Dim Ex1ConfigCheck As New CheckBox
frmSetup.Controls.Add(Ex1ConfigCheck) ' Add Control to from
Ex1ConfigCheck.Top = (Looper + 45) + (Looper * 18) ' Set Location
Ex1ConfigCheck.Left = 210
Ex1ConfigCheck.Text = Setup.ExCheckName(Looper) ' Set Text property from strArray
Next
This is where I don't know how to proceed.
I would like to fill a boolean array (ex. MyBoolean(37)) with the value of Ex1configCheck().Checked. The reason I would like to fill another array is because I need to be able to reference the value of the checkboxes in other parts of the code but can't access them until they are created. Also, I plan on saving the array out to a binary file.
Could someone point me in the right direction please?
If there are no other CheckBoxes in the same container as those ones then you can do this:
Dim flags = Me.Controls.OfType(Of CheckBox)().
Select(Function(cb) cb.Checked).
ToArray()
If the controls are in a different container than the form itself, replace Me with that container.
As suggested by #Jimi, you could also create a List(Of CheckBox) and assign that to a field, populating it when you create the controls. You can then use that list instead of creating one on demand:
Dim flags = myCheckBoxList.Select(Function(cb) cb.Checked).
ToArray()
Of course, if you know exactly how many CheckBoxes you are going to be adding, why do you need to wait until run time to create them? Why can't you create them at design time and then modify them at run time? You usually only create controls at run time if you don't know how many there will be until run time, but that seems not to be the case here.
Thanks all for your answers and comments. I always have a fear of being roasted when I ask what some may consider a simple question online.
I have found an alternative way of accomplishing my task. Instead of creating 8 "Arrays" of checkboxes, I have learned of a very simple control available called "CheckedListBox".
I really didn't need to create the checkboxes at runtime but was trying to find an easier way to create 8 groups of 37 checkboxes without having to manually name and set the properties of each one during design. I also wanted to be able to index them in my code to be able to update and read the value using simple loops. I could have done this by creating arrays of CheckBox but again, I would have had to manually initialize the arrays.
Once I found the CheckedListBox, I was able to accomplish what I want very quickly. I only had to set the properties of the 8 "groups" (CheckedListBox's) and fill them using the items property. The ListBox essentially created a List like Jimi suggested automatically and I can index thru each list with a loop as desired. Jimi's suggestion actually lead me to finding the CheckedListBox while I was searching for more information on using "List(of CheckBox)".
Sometimes talking to others helps me find the right questions to ask. Google was able to figure out what I wanted when I searched for "List(of CheckBox)". (:

How to use a string as a visual basic object?

I have a form with lots of Picture boxes, is there a way to call them using a string? Something like
PctBox+str(number) would give me the name of the box on the form, so I can loop through and change them all without having a huge block of code?
Are we talking about Microsoft Access?
If so, you can use this sample, assuming your picture box controls are named PctBox1 - PctBox3 for example and you place the code in a procedure in the form:
For index = 1 To 3
MsgBox(Me.Controls("PctBox" & index).Name)
Next
This sample just shows up the name of the each of these controls.

How to use custom code to add Excel-like "color scale" conditional formatting to SSRS report

I am trying to use this custom code in SSRS
public function ColorScaleRYG(value, minValue, maxValue) as string
in a custom code in ssrs
and then in a Fill expression
=Code.ColorScaleRYG(Sum(Fields!SalesAmount.Value), 0, 100000)
which should break my values in a group and assign shades of colors from red(0) to green(max valer).
But for some reason nothing happens/
What am I missing?
I need something like that:
I wont be able to give you the specific answer as it is with your code, however this is how I go about it.
In Design Mode I Right click the required Cell and Select "Text Box Properties.
I then go to the Fill Tab and click on the expression button next to Fill Color.
I then User something like the following code
=IIF(Fields!Total_Eligible.Value>100,"MidnightBlue","Silver")
You should be able to Stack this IIF Commands.
It is just a matter of changing the Fields! field to the approriate variable and then the conditions.
This Returns the following Values
Hope this Helps.
Post Note - Probably dont use these colors as they aren't easy to read , I just grabbed two at random off an existing report to demonstrate.

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.

Is their any CharacterUpDown Control in vb.net

In VB Win-forms Application a NumericUpDown Control is used for only to scroll numbers. Its definition is:
The Windows Forms NumericUpDown control looks like a combination of a text box and a pair of arrows that the user can click to adjust a value. The control displays and sets a single numeric value from a list of choices. As Shown here.
I want to use strings instead of numeric values. For example, Grades: "A","B","C", etc. I think I will need to use UserContorl to code another custom "StringUpDown" Control.
You can't add String values to Numeric Drop down. instead for this you can use DomainUpDown here is the code to place a DomainUpDown in your form that contains A - Z characters
Dim domainUpDown1 As DomainUpDown
domainUpDown1 = New System.Windows.Forms.DomainUpDown()
For i As Integer = 65 To 90
domainUpDown1.Items.Add(Chr(i))
Next
Controls.Add(domainUpDown1)
Note : This control is available in Tool box also
You can change values using the ByRef=""
e.g. RefValues="Red;Blue;Green;Brown"
Hope this is what you were looking for
Link: mindStick