Visual Studio - How i can use a variable into a object name? - arrays

i'm learning Visual, and i'm tryind to do this:
For Each folder In Dir.Subfolders
list = list + 1
C1CheckBox(list).Text = folder.Name
Next
I have a lot of checkboxex named C1CheckBox1, C1CheckBox2, C1CheckBox3, etc... then i want to change the text of each checkbox by the folder name (using the list var to reference the object)...
How i can do this?
thankyou for read

You can find controls by name with Controls.Find :
For Each folder In Dir.Subfolders
list = list + 1
Dim cb As CheckBox = Me.Controls.Find("C1CheckBox" & list, True)(0)
cb.Text = folder.Name
Next
This will search the entire form including its child containers. If you know all your checkboxes are in say, panel1, you could be more specific:
Dim cb As CheckBox = Me.Panel1.Controls.Find("C1CheckBox" & list, False)(0)

You can loop through all of the checkboxes setting their text as you go. See this answer for an example for how to enumerate the checkboxes

Related

I need to pull the text of an image file name from an array but am only getting "pyimage#" or "tkinter.PhotoImage object at X" as replies

I'm trying to create an array with an assortment of different randomized image files in it to display on a set of buttons in Tkinter. When a given button is clicked I'd like to add the text of that file's name to a new array. Basically, when button with imageX is clicked add "imageX" to a new array.
Unfortunately, I always get a return that isn't the image's filename, or the variable that I've set to correspond to that image, but instead either:
"tkinter.PhotoImage object at X" (where is X is a location like "0x0000020FC894D2E0") if the command is populationbeta.append (population[0])
or
"pyimage#" (where # is an integer that seems to relate to the number of images in the source file), if I change the command to populationbeta.append (str(population[0]))
I feel like there should be a simple way of doing this and I've tried every work around I can think of but I'm not getting it to work. Any help would be very much appreciated! Thanks!
Here's a shortened/simplified version of the code in question:
master=tkinter.Tk()
master.title("Not working")
a1b1c1 = PhotoImage(file = r"C:/users/jdavis319/documents/bushesoflove/BoLdraw/a1b1c1.png")
a1b1c2 = PhotoImage(file = r"C:/users/jdavis319/documents/bushesoflove/BoLdraw/a1b1c2.png")
a1b1c3 = PhotoImage(file = r"C:/users/jdavis319/documents/bushesoflove/BoLdraw/a1b1c3.png")
a1b2c1 = PhotoImage(file = r"C:/users/jdavis319/documents/bushesoflove/BoLdraw/a1b2c1.png")
a1b2c2 = PhotoImage(file = r"C:/users/jdavis319/documents/bushesoflove/BoLdraw/a1b2c2.png")
population = [a1b1c1, a1b1c2, a1b1c3, a1b2c1, a1b2c2]
populationbeta = []
populationbeta.append(population[0])
print(populationbeta)
This gives the result: "[<tkinter.PhotoImage object at 0x000001A419D4F070>]"
This gives the result: "[<tkinter.PhotoImage object at 0x000001A419D4F070>]"
Correct. That shows that you have a list of PhotoImage objects. If you want the filenames you can use .cget('file') on the objects. cget is a common tkinter method for getting the value of a configured option.
filenames = [image.cget('filename') for image in population]
Or, if you don't want to use a list comprehension to create a list of filenames, you can do it on an individual image like so:
populationbeta.append(population[0].cget("file"))

Covert a String to an Object which has a variable name

I searched for two hours, here on Stackoverflow and on other forums. But i can't find a solution for my problem. I have to be able to change the properties of an object that has a name that I don't know. Now i try to explain better:
The user drag some files in a form, and i get in a array() all the paths of the dragged files. For each path in files() i add to a panel a usercontrol that is the interface of an uploader. (My application it's kind of an uploader). Good, imagine that the user dragged 4 files, i have 4 different usercontrols, named "Uploader1", "Uploader2", "Uploader3" and "Uploader4". I need to change the text of a label in the uploader1, but i can't write:
Uploader1.LabelExample.Text = "Example"
Becouse it doesn't exist! (Not yet!)
So i tryed this method.
Dim UploadCounter as Integer = 1
Dim CurrentUploader = CType(Panel.Controls("Uploader" & UploaderCounter.ToString), UploadBanner)
CurrentUploader.LabelExample.Text = "Example"
I write the same with DirectCast and TryCast, but nothing.
I try also:
For Each Uploader With{.Name = "Uploader1"} as UploaderControl in Panel.Controls
Uploader.LabelExample.Text = "Example"
Next
I searched everywhere for "convert string to an object in vb.net" but i can't find anything that work! They all return "System.NullReferenceException: 'Object reference not set to an instance of an object.'"
Sorry for my bad bad english, thanks for all that will help me! need really!

Allow edits from different users appear in different color inside memo box : Microsoft Access 2010

I have a memo field which contains rich text. I am able to identify a user and change all the text in the box instead of just the text they added.
I am looking to write code which allows the text to be edited and after update , the edited text will appear a different color than the original text in the memo field.
I have tried :
Dim strNew As String
Dim strOld As String
If Me.txt_username_id = "grant" Then
strOld = Me.Form!txtnotesaboutproduct1.OldValue.ForeColor = vbBlack<br/>
strNew = Me.Form!txtnotesaboutproduct1.ForeColor = vbRed
End If
I have also tried
Dim ctlOld As TextBox<br/>
Set ctlOld = Me.Form!txtnotesaboutproduct1
If Me.txt_username_id = "grant" Then
ctlOld = Me.Form!txtnotesaboutproduct1.OldValue.ForeColor = vbRed
End If
Generally, I do this with a continuous subform for Notes, so that I can hold the data, date and user, rather than just one formatted text box. Though I do realize this might be a lot more real estate that you might have, you can use a conditional format within the subform. I do agree that if it is possible, you'll likely need to use HTML and not .Forecolor, which will change the entire box.

Null reference when using a variable with findcontrol

I have a .aspx page with several textboxes, including textboxes with IDs of txtID1, txtID2, txtID3... and so on.
I am attempting populate the textboxes with a data from an XML file by looping through a node list. With each loop, I want to use the FindControl method to locate txtID1 and set its .Text to the value of the id attribute of the first node; then locate txtID2 and its .Text to the value of the id attribute of the second node, and so on.
When the following line of code is run, I get a null reference error for TextBox txtID, so it appears that I am doing something wrong with the FindControl method. Is my syntax incorrect? Do I need to use a different method?
int x = 1;
XmlNodeList getAuthors = getItem.SelectNodes("item/authors");
foreach (XmlNode getAuthor in getAuthors)
{
TextBox txtID = (TextBox)Page.FindControl("txtID" + x.ToString());
txtID.Text = getAuthor.Attributes["id"].Value.ToString();
x = x + 1;
}
After further research (that is, lots of Googling), it appears that I may be running into this problem because I am using a master page. Neither (TextBox)FindControl nor (TextBox)Page.FindControl was working so I have abandoned this approach. Here is an old article that seems to explain my problem.
http://weblog.west-wind.com/posts/2006/Apr/09/ASPNET-20-MasterPages-and-FindControl

Visual Basic Referencing .Net Objects In An Array

I have a big list of objects in the Design view of Visual Basic 2010 that I need to change a bunch of properties for, so of course I tried using an array rather than taking 50-60 lines for a repetitive task. But there seems to be an issue referencing to the object and it seems to be just taking the information from it. I know that was a shitty explanation, but maybe you'll understand it when you see it.
Dim objectsToClear As Array = _
{lblDailyRoundTrip, lblDaysWorked, lblFillBoxes, lblMilesPerGallon, lblMonthlyInsurance, _
lblMonthlyMaintenance, lblMonthlyParking, tbDailyRoundTrip, tbDaysWorked, tbMilesPerGallon, _
tbMonthlyInsurance, tbMonthlyMaintenance, tbMonthlyParking}
For i = LBound(objectsToClear) To UBound(objectsToClear)
objectsToClear(i).Text = ""
objectsToClear(i).Visible = False
Next
Try this instead:
Dim objectsToClear As Array = { lblDailyRoundTrip,
lblDaysWorked,
lblFillBoxes,
lblMilesPerGallon,
lblMonthlyInsurance,
lblMonthlyMaintenance,
lblMonthlyParking,
tbDailyRoundTrip,
tbDaysWorked,
tbMilesPerGallon,
tbMonthlyInsurance,
tbMonthlyMaintenance,
tbMonthlyParking }
For Each item In objectsToClear
item.Text = String.Empty
item.Visible = False
Next item
P.S. - you REALLY should have Option Strict On, and you should have strongly typed your array.
Since you seem to be interested in changing only .Text and .Visible properties, then you can just find the control by name, like this:
Dim returnValue As Control()
returnValue = Me.Controls.Find(objectsToClear(i), True)
Note: The True argument is for whether or not to search all children, which it sounds like you want to do. Read Control.ControlCollection.Find Method documentation for more information.
Now that you have a collection of controls that match the name you specified, loop through the controls in that collection and set the property values, like this:
For Each c As Control In returnValue
c.Text = ""
c.Visible = False
Next

Resources