I am using WindowsForms and I am trying to put a Textbox into my main menu and add an Image. However I cannot get the image to appear. What am I missing here.
txtRequestEdit.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText
txtRequestEdit.Image = Properties.Resources.Wrench16 'This is a valid image.
txtRequestEdit is a System.Windows.Forms.ToolStripTextBox
EDIT- Here is an image of the menu. The item at the bottom of this menu is the txtRequestEdit control.
Strangely the no matter how you apply the image, it won't show on ToolStripTextBox. I think this is a bug because I found nothing about this behavior in the documents or on the web.
I will try doing this with a hack like owner-drawing the item or something else and will put the wrong answer here so the next person with this issue avoids this wrong path.
This is wrong:
Apparently the Image property "supports the .NET Framework infrastructure and is not intended to be used directly from your code.", so if (as you say) everything's OK and you've checked the image itself to be valid, using ImageList and ImageIndex may solve the problem (assuming the item lies inside a MenuStrip named menuStrip1:
txtRequestEdit.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText
menuStrip1.ImageList = New ImageList()
menuStrip1.ImageList.Images.Add(Properties.Resources.Wrench16)
txtRequestEdit.ImageIndex = 0
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 am trying to implement a 'Flag' w/ a menustrip.
I have an image list w/ a red flag and a black flag.
When I click the menu item, i want to toggle the image.
The problem I have is that once I change the image, it wants to do SizeToFit which makes the Icon roughly the height of text. I dont want that. I want the Image to be its actual size.
I have tried putting the statements in different orders, nothing i have tried seems to work.
Currently, I have the button set to the black flag at design time.
This code shows me trying to change to a Red flag.
tsbFlagPatient.Image = ilFlags.Images(1)
tsbFlagPatient.ImageScaling = ToolStripItemImageScaling.None
tsbFlagPatient.DisplayStyle = ToolStripItemDisplayStyle.Image
[If there is some better way to approach toggling the image, im open to that. That seems like a separate question. ]
I was unable to make ImageScrolling work.
Since I dont need text here, I used BackGround Image and set
BackgroundImageLayout = Stretch
This allows the images to be the right size.
You can turn AutoSize = false and then manually set Height, Width to fine tune your menu's size.
I did reconsider the ImageList and changed the images to being stored in the project Resources. This is the code to toggle the image
Public Sub SetFlagImage(flagSet As Boolean)
If flagSet = False Then
btnFlag.BackgroundImage = My.Resources.appbar_flag_wavy_black
btnFlag.BackgroundImage.Tag = "black"
Else
btnFlag.BackgroundImage = My.Resources.appbar_flag_wavy_red
btnFlag.BackgroundImage.Tag = "red"
End If
End Sub
This had no effect on the question posed here. In general, probably better than having an image list because all forms can access it.
Working with VB.NET in Visual Studio 2015. During debugging, is there any way to view the value of a variable inside a 'With' block by hovering with the mouse?
For example, in the code below, if I hover over lSection or lSection.MomentZ or lSection.MomentY or others, VS displays information about the object and/or the value of the variable (as expected).
However, if I hover over any object starting with a period (.SectionIndex or .SectionLocation or .Mx or .M_y or .Vx), VS will NOT show any tooltip at all.
NOTE: I am aware that I can add the the variable to a Watch window. I know there are other ways to get the value of the variable, but I want to be able to just hover with the mouse because it is literally (yes, literally) 900 million times faster.
I could only find one other question similar to this one, but the answer does not explain whether or not there is ANY possible way to enable this feature (VS setting, registry hack, 3rd party add-on, i don't care how)
How to view VB.NET object properties in the debugger within a "With object" block?
With mCSIDetails
.ClearCSIResults()
' Section Index and Location
.SectionIndex = lSection.ID
.SectionLocation = Round(lSection.FracLen * mMember.Length, 3)
' Section forces and moments
.P = lSection.AxialForce
If liAxis = MemberOrientation.Strong Then
.Mx = lSection.MomentZ
.M_y = lSection.MomentY
.Vx = lSection.ShearForce_CSI_12
.Vy = lSection.ShearForceZ_CSI_12
ElseIf liAxis = MemberOrientation.Weak Then
.Mx = lSection.MomentY
.M_y = lSection.MomentZ
.Vx = lSection.ShearForceZ_CSI_12
.Vy = lSection.ShearForce_CSI_12
End If
End With
I posted the same question on the Visual Studio forum and the moderator was able to replicate the issue.
Apparently this is just the new (terrible) way the debugger works. So if you want to be able to mouse-hover and get a tooltip with the object/variable value, then don't use WITH blocks...
Background:
Hello everyone, I'm not new to visual basic or programming. I just find it the fastest to make a GUI in visual basic for various programs; however, I'm having a recent issue.
I have a form with a ListView control that I populate using a textbox and a button.
the code I use for the button is:
Code:
listview1.items.add(textbox2.text)
Pretty simple code that has always worked. I changed a few properties on my ListView as well, and I will list them. I just don't know what is going on.Here's a list of the properties changed (everything else is default):
Properties:
BackColor = DarkGray
BorderStyle = FixedSingle
HeaderStyle = None
MultiSelect = False
View = Details
All properties were changed via the properties tab and not through code. I want to be able to add text to the control. I'm not adding any subitems either. I'm just confused because this code has always worked, and I've never had this issue. I'm also using Visual Basic 2010 Express Edition if that helps.
If the added item in ListView is not displaying then check the ListView.View property. if this property has set to Detail view then Columns must be added in that listView control otherwise added item will not display.
Make sure item is added correctly.
Dim item As ListViewItem = listView1.Items.Add("Item Text") ' This text will be displayed in first column. The column index will be 0
' Further values will be added in SubItem
item.SubItems.Add("Sub Item1")
item.SubItems.Add("Sub Item2")
item.SubItems.Add("Sub Item3")
I have searched many times here and on Google looking for a solution that did not envolve utilizing someone's class.
This context menu is pops up where the user right clicks inside a dataGridView
When adding the items the VB code is
Dim m As New ContextMenu()
m.MenuItems.Add(New MenuItem("Disassociate *A* Device"))
m.MenuItems.Add(New MenuItem("Purge Device Assosciations"))
Is there no simple way to reference a resource to add an icon to said menuItems?
Pseudo
m.MenuItem(0).Icon.Source = ....
?
Assuming that this is for a Windows Forms application.
Why not use the ContextMenuStrip?
Example:
Dim m As New ContextMenuStrip()
Dim item As New ToolStripMenuItem("Click Me!")
item.Image = My.Resources.image
m.Items.Add(item)
DataGridView1.ContextMenuStrip = m
You will need to set Owner Draw to true and actually draw the menu item yourself
Here is a good detailed example
I use the image propery and assign a system.drawing.image object to it.
You wont be able to do it in one line, you do the add once all the properties of the newmenu is set.