Hi I am using visual studio 8, vb.net. I have a picture box on my form, picturebox1
I have a number of image files name image1.gif image2.gif image3.gif etc upto image52.gif
I want to display an image in a picturebox dependant upon a value wich is generated elsewhere in the app, so that if '3' is generated I a picturebox to display image3.gif
if 2 is generated image2.gif. However I have 52 such images to be displayed in 16 picture boxes so I was wondering if there is a more efficient way than a select case method for each image and each picture box.
All and anyhelp is much appreciated thank you.
Whenever i have a large number of images i always use an imagelist. You could add the images to the list in the correct order and access the image at the desired index position.But it will help you get rid of one of the select statement but you will still need to have one for the different picture box. This is really only to keep things clean. Performance wise i am not sure you would get that much of a hit by doing multiple select statement.
You can make a custom url to the image:
string ImageUrl = startofurl + GeneratedNumber + ".gif";
So if you use for example these values:
startofurl = "C:\Users\Wiebren\Desktop\image"
generatednumber = 4
You will get this:
url = "C:\Users\Wiebren\Desktop\image3.gif"
Do this for the 16 boxes and you are done.
Hope this will work
Related
I am trying to change the Image attribute associated with a button when the button is clicked.
I have added the image I want to display (called "Black Pawn.png") as a resource within the solution and (from searching around and looking at similar questions) am trying to set the image attribute to it as shown below:
Private Sub boardButtonA2_Click(sender As Object, e As EventArgs) Handles boardButtonA2.Click
Dim sprites As Object = My.Resources.ResourceManager
boardButtonA2.Image = sprites.GetObject("Black Pawn.png")
End Sub
But when I click the button, the default image on it just disappears and is replaced with nothing instead of Black Pawn.png.
I am pretty new to using Visual Basic and Visual Studio so I may have failed to have added the image as a resource properly but I can see the image in the Solution Explorer above the form the button is in.
Any help or advice would be a great help.
Thanks.
EDIT:
Having thought more about the potential scenario, I'm wondering whether this answer is actually relevant. Your example code uses a hard-coded String but I'm wondering whether the actual String really would be a user selection. I'll leave it here anyway.
ORIGINAL:
There's no reason to use a hard-coded String to get a resource. If the String was from user entry then maybe, depending on the circumstances. As it is though, you should be using the dedicated property for that resource. That means using this:
boardButtonA2.Image = My.Resources.Black_Pawn
Just be aware that a new object is created every time you get a resource from My.Resources. For that reason, don't keep getting the same property over and over. If you need to use a resource multiple times, get it once and assign it to a field, then use that field multiple times, e.g.
Private blackPawnImage As Image
Private Function GetBlackPawnImage() As Image
If blackPawnImage Is Nothing Then
blackPawnImage = My.Resources.Black_Pawn
End If
Return blackPawnImage
End Function
and then:
boardButtonA2.Image = GetBlackPawnImage()
Also, I suggest that you change the name of the property to BlackPawn rather than Black_Pawn. You can change it to whatever you want on the Resources page of the project properties.
EDIT:
If this application is a chess game then you definitely will need every resource image for the playing pieces so you probably ought to get all of them at load and assign them to variables, then use them from those variables over the course of the game.
(I am assuming the question is for WinForms, if it isn't I will remove this answer)
When adding images as resources to a project, you have to pay attention to the name given to the resource after it is imported - the name can be changed if you want.
The image below is from one of my projects:
The name of the files on disk are:
CSV - Excel.png
GreenDot.png
RedDot.png
To fix your problem change the line:
boardButtonA2.Image = sprites.GetObject("Black Pawn.png")
to be:
boardButtonA2.Image = sprites.GetObject("Black_Pawn")
I don't think adding the image by going into Project > Add Existing Item properly added the image as a resource.
I instead went into *Project > (Solution Name) Properties > Resources > Images > Add Existing Item * and added it that way and was then able to get it working using jmcilhinney's method (I think my original method/a variation of it would work too but theirs is better).
I'm trying to make (using Visual Basic) a rudimentary questionnaire that measures multiple attributes and stores their values in an array and afterwards measures the attributes on a scale of 1 to 10.Now I've had the dumb idea to use a label for every single value of every attribute, and highlighting the right number by changing the label's image to yellow, instead of white(basically making the area behind the number a different color).
Now here's the issue: I can't seem to find out how to change a label's background image with code, which is what I'm asking. (I'm guessing the command should look something like "Label1.image = >image path<")
Pretty simple.
If text = No then
Label.Text = No
Else
Label.Text = Yes
End IF
You just have to change the text value of the label which is .Text = Text you want
And if your trying to show images, you should use the image control in the tool box, not a label
I ' ve a combobox with too long multiple Items . There is any method to set a horizontal scroll in combobox or set a multiline properties for each item ?
Hi Mattia,
There are a couple of methods that are usable as well as effective for this problem you are having, here is the concept that I've created that you may implement in your project;
Original text: C:\path1\path2\path3\path4\file.exe
Combo Box text: C:\path1\p...file.txt
To do this you will 1st have to specify how many characters you want to shown in the start of the text (1st 5 or so) then you must specify how much you would like to leave off at the end (last 6 or so).
I realize this causes a problem, "I wont be able to see the full path!", however adding a 2nd form or a msgbox to display the full text which should initiate the 1st 2-4 secs of display time, this should not be a challenge nor a problem and will make you look like a Pro!
I hope this helped you with your problem or motivated you to go over and beyond!
Is there any way to populate the report viewer Dynamically (only labels)?, i what to make a little process where the users can define the label and position on the report, for example:
A)User pick this 2 fields:
1)Beneficiary Name
2) Amount of the payment
B) User pick the position of each label (or better with a drag/drop)
1)Beneficiary: Heigth = 30, width = 200, x = 20, y = 60
2)Amount: Heigth = 30, width = 80, x = 400, y = 60
C) We populate the report viewer so the user can see how his report is going to print.
Once the user save is report and want to print a Check/Payment, by code we only are going to take the label and position and send it to the print directly ("p.print()") not with a ReportViewer control, i only want the report viewer so the user can see how their report will see before printing it (position preview), or there another way to do this?.
I hope you get my idea of what a want to do here, thanks in advance.
(i use an example of 2 fields, the user will select between 30 to 40 different field)
The report definition is simply a XML file (text). As part of your report generate/run process, you could alter the contents of the .rdl file
Start by making a report template that has your fields on the report (with default positions).
Before you run the report, read template, replace (reposition or hide) those fields, save copy under a temp name.
Use your report viewer to run the (modified) copy.
I'm not saying it will be easy though. Your requirements sound tough.
The drag/drop resizing and add/removing controls (that you described) would probably have to be done using some pretty slick JQuery. There are plenty of nice examples of that kind of thing (goog).
Take a look at ReportBuilder. It allows user to create adhock reports on the fly.
-->VB.Net VS2010
If this is a stupid question, then let me know. I can't just figure it out.
First of all, this is my question. How do I detect the changed value on those text boxes?
I have an idea that I create a another string values that have the original values and compare them right before saving or form closing event. This sample just have 3 text boxes, but I have 50 + text boxes on my one form. I can do it that way, but that is not really smart idea.
Can I use abcDB.SubmitChanges() to detect any data changes on text boxes?
Dim sqlQuery = (From obj in abcDB.HelloWorld
Select obj.Fname, obj.Lname, obj.PhoneNumber).FirstOrDefault()
me.textboxFname.text = sqlQuery.Fname
me.textboxLname.text = sqlQuery.Lname
me.textboxPhoneNumber.text = sqlQuery.PhoneNumber
What you really want to do is called 'data binding'. Lots of tutorials about that aroud the web and any book about VB.NET will have at least 1 chapter dedicated to it.