Check if a range of PictureBox's was clicked, then do the same code with different integers? Visual Basic - vb.net

This is going to be rather hard to explain I expect, but I'll try the best I can.
Okay, so, I was wondering if it is possible to check if the user clicked on a specific PictureBox, and then perform an action with a few different integers? I'll give you an example.
PictureBox2.ImageLocation = "images/image.png"
However change the PictureBox2 part to the picturebox that was clicked. I'd like to know this so that I don't have to write out 100+ lines of code every time I want something like this done.
Thanks in advance!

Related

Context menu to delete items from a ComboBox

Currently, I've designed my own combobox from a ListBox/Button/TextBox to accomplish this, but I'm finding this to be way to much work to try and get a hacked together ComboBox to look like an actual ComboBox control.
This does however give me the desired effect however. Sort of ...
But my question is, is there not a simpler way to do this? Is there a way to simply right click on an item in a ComboBox, and get a context menu that says "Delete" for that specific entry?
Here is an example of the desired effect with my hacked together ComboBox:
I've tried simply linking a ContextMenu to a ComboBox, but that only allows for right click on the actual ComboBox itself, not the drop down, which renders this useless to me.
Any help or advice would be greatly appreciated.

Access cell color value of datagridview when it has been set via the cellpainting event args

My searches regarding this have only turned up how to set the color of a datagridview cell, whether that be in the formatting, cell painting, or databound events.
The code that I'm trying to work with is currently setting the cellstyle.backcolor through the datagridview event args in the cellpainting event:
Private Sub dgvAncientCode_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles dgvResults.CellPainting
If dgvAncientCode.Columns(e.ColumnIndex).Name = "Blah" Then
e.CellStyle.BackColor = Color.Red
End If
This works--the specified cells do show up as red--but how can I access this value elsewhere in code? When I check the cell.style it's set to the default, yet it displays on the screen as red.
I know there is a lot wrong with this code, but I'm still relatively new and I'm supporting a lot of legacy stuff that's comprised of every worst practice you can think of. I did come up with a workaround for this by setting the cell.style.backcolor instead of the e.CellStyle.BackColor, which lets me access the value later, but I'm also trying to understand what was happening with the original code. I hate making things work without understanding why, mainly because I know I might be missing something important.
I apologize if this isn't clear or if it's been asked before. I've searched for a couple hours here and elsewhere, so hopefully I didn't miss it.
Edit for additional information: There is a button click event that iterates through the datagridview using a for each loop. That is being used to set the cell color of a custom excel export class. This did not work because none of the cells in the datagridview had a value on the cell.style.backcolor property, even though they show as red in the form. If the cell displays as Red in the form, I should be able to access that somehow--at least, I think I should.
Try one of this. Maybe it can help you. "yourdatagrid.ControlStyle", "yourdatagrid.ItemStyle".

MS Word RibbonX How can I dynamically populate a combobox when a dotm file opens?

I'm a little stumped here. I'm diving deeper into designing ribbons for MS Word 2010, and I came across something new: populating comboboxes on the fly. In the image below, you can see...
...that I'm a dude who likes music while he works, just like any other dude. Problem is my list of playlists changes from time to time, so I don't want to hard-code that list into my ribbon's combobox. I can easily hard-code it, but I want this thing to be dynamic. And so, in my ribbon code:
<comboBox id="cmbPLaylist" label="Playlist" getItemLabel="Document_Open">
<item id="none" label="None"/>
</comboBox>
I have left only one item, "none," which is fine if I want the music player to launch with no playlist loaded. But what if I want a playlist to automatically load?
First, from my Google and book research, I've determined that I need to have a getItemLabel callback to populate the control. Is this the right way to go? But how do I run that automatically when my Normal.dotm loads? I'm having problems running this thing in the Document_Open event, and I've been reading online that I'm not alone.
My problem is a bit threefold: first, I'm really new at using these predefined callbacks like getEnabled, getItemLabel, etc. The callback territory is a very new territory for me. Second, I've never used a combobox in a ribbon before. Three, I've never dynamically populated a combobox in a ribbon before. I might be trying to bite off more than I can chew at once, but can anyone point me in the right direction?
My code so far, inserted into my Normal.dotm Document_Open event, is such:
Private Sub Document_Open(control As IRibbonControl, ByRef label)
Dim ListOfPlaylists() As String
ListOfPlaylists = GetPlaylists()
ListOfPlaylists(UBound(ListOfPlaylists)) = "Random"
End Sub
After this, I'm stumped. As you can see, I'm not sure how to tell MS Word, "Hey, MS Word, insert this value into the combobox list!"
Maybe it's my newbness at this whole thing, but when I Google for an answer, I'm not seeing it in the code. So any help is appreciated. Thanks!
I actually did some fiddling and almost stumbled on the answer. I put this in my code and it seems to work just fine now:
Sub drpPlaylists_getItemCount(Control As IRibbonControl, ByRef drpPlaylists_itemCount)
drpPlaylists_itemCount = UBound(ReadDirectoryContents(MusicDirectory, "*.m3u"))
End Sub
I guess this gets launched every time the ribbon has to reload itself. But it's solved for now. Still have some things to study up on on when these callbacks get called, but I'll figure this out. Thanks for the help!

VBA Status Bar

I am working on a Word VBA macro app for 80 or so users. The office has high staff turnover, so training suffers, and so one of the self imposed requirements for this project is comprehensive, friendly documentation. However, to supplement this, and to save newbies having to open up a 100 page document when they want to try something new, I want a status bar on every userform (there are five) that provides contextual help. I find tooltips annoying.
I don't have a lot of experience, so I was wanting to
Essentially, I have a file containing every status string. (This is currently a text file, but I was wondering if I should use a spreadsheet or csv for ease of editing by other staff in future.) Every control has a MouseMove event which refers to a function: getStatus(cID) that opens the file, grabs the line and displays it in the status label. It also grabs a few parameters from the same line in the file, such as whether the label is clickable (to link to a page in the help file), and what colour the label should be.
So a few questions really:
Will the application be slow if a userform is constantly referring to a file? It feels fine to me, but I've been in it far too long, and I'm the only user accessing that file. There will be 80 constantly accessing it.
Is MouseMove over a control the best way? Should I instead use co-ordinates?
Most importantly (in terms of me having to do as little work as possible) is there some way to do this so that I do not have to have a MouseMove event on every single control? I have a good few hundred or so controls, each with their own identifier (well, not yet, but they will if this is the only way to do it). Maybe when the form loads I could load ALL the possible status lines so they're ready for whenever the control is moused over. But then, maybe the loading time is negligible?
Appreciate any ideas or thoughts - especially if VBA already has a whole range of functions to do this already and I'm just trying to reinvent the wheel. I can't use the application status bar, because the user rarely sees the application itself.
Thanks!
EDIT:
It is for both data entry, clicking around and a bit of document generation.
It is a controlled environment so macro security issues aren't a big concern for me - and if something goes wrong it's someone else's fault or problem :)
Is this data entry app or do they just click stuff? Because often the field with focus is different to the item the mouse is hovering over, this can cause a lot of confusion.
Constantly reading from a file is a huge waste of time and resources - it is much better to load them only once into an array or collection when the form is loaded.
On MouseMouse event is better than coordinates because you can move things around without worrying. It's a lot of code but you should be able to generate most of that if you have a list of control names because the code should be identical.
ie
Sub Control_MouseMove()
DisplayStatus(Control)
End sub
I would consider the StatusText property and ControlTipText property of controls for this kind of help.
StatusText
This example sets the status bar help text for the form field named "Age."
With ActiveDocument.FormFields("Age")
.OwnStatus = True
.StatusText = "Type your current age."
End With
ControlTipText
This can be assigned from the property sheet for the control.
Private Sub UserForm_Initialize()
MultiPage1.Page1.ControlTipText = "Here in page 1"
MultiPage1.Page2.ControlTipText = "Now in page 2"
CommandButton1.ControlTipText = "And now here's"
CommandButton2.ControlTipText = "a tip from"
CommandButton3.ControlTipText = "your controls!"
End Sub

WinForms Multiline Textbox, Enter key insists on moving focus out of textbox

I've got a multiline textbox and a button below it.
VB.NET, WinForms, .NET 2.0
System.Windows.Forms.Textbox
Multiline = True
AcceptsReturn = True
AcceptsTab = False
CausesValidation = False
No Events explicitly coded.
I'd like the Enter key to insert line-feeds and never move the focus to the next control (the button).
I'd like the Tab key to always move the focus to the next control in the tab order (the button).
What happens instead is that the Enter key inserts one line-feed and then moves focus to the next control (the button). It also does this with Ctrl-Enter, which really baffles me!
By my reading of the help files and extensive googling, it should work the way I need it to.
But obviously I'm missing something. What am I doing wrong?
A method I often use for this sort of problem is to iteratively add and subtract code until I can narrow it down to the one thing that caused the problem.
For instance, you might start by making a very simple project with just one edit box and one other control, and see what it does. If this code behaves the way you want it to, then you can start adding code bit by bit, getting the simple project closer and closer to intended final product, until the bug appears. Then look at the last bit of code you added, and see if you can subtract bits of that until the bug goes away. Iterating this a few times might help you find the culprit.
Alternatively, you could start with your existing (misbehaving) code, and start simplifying it until the bug goes away. Then you add back part of the last thing you deleted, and iterate as above.
Lastly, in this case you could also try adding an event handler for the edit control's Leave event, and put a breakpoint in the handler. When the BP hits, check the callstack and see if you can get an idea of what code precipitated the focus change. For this to work, your debugger will probably need to be configured to display code that you don't have source for (i.e. disable the Just My Code option in the debugger). You could even paste a (trimmed) callstack into the question if you want to get the group's help in deciphering it.
p.s. Does anybody have a name for the iterative debugging method described above? If not, may I propose calling it Newton's Method (or perhaps Newtoning), since it resembles Newton's Method for iteratively finding roots of mathematical functions.
It definitely shouldn't do that. The only thing I can think is it's not got enough height to accomodate multiple lines. Try adding...
textBox1.ScrollBars = ScrollBars.Vertical
If not I don't know. Try creating a blank project and creating a form with one text box, one button set the properties and see what happens...
Turns out that I had forgotten that I had done this (below) elsewhere on the same form:
'http://duncanmackenzie.net/blog/Enter-Instead-of-Tab/default.aspx
Protected Overrides Sub OnKeyUp(ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyCode = Keys.Enter Then
e.Handled = True
Me.ProcessTabKey(Not e.Shift)
Else
e.Handled = False
MyBase.OnKeyUp(e)
End If
End Sub