passing variable into contextmenustrip - vb.net

winforms app with 5 labels.
Each label is a variable pulled from an XML file.
I would like a right-click event to pull data into a contextmenustrip.
Right now I have it half successful. I can copy the url with a right click:
Private Sub Label1_Click(sender As Object, e As MouseEventArgs) Handles Label1.MouseClick
Dim x As String = Label1.Text
If e.Button = Windows.Forms.MouseButtons.Right Then
Dim var As String = xmlinteraction.returnLink(x)
Clipboard.SetText(var, TextDataFormat.Text)
ElseIf e.Button = Windows.Forms.MouseButtons.Left Then
xmlinteraction.appCall(x)
End If
End Sub
I would much prefer the user be prompted to see if they want to copy to clipboard, and if I can make that work I have three other menu item ideas I can easily use.
Can I could pass the copied variable into the first menu item of a contextmenustrip. So then I could create one contextmenustrip for all 5 labels and depending which label you right-click, it gives the correct value if you right-click then "Copy Link" from the contextmenustrip?

You don't need to have a separate ContentMenuStrip for each label. You can apply the one ContextMenuStrip to every label. Once you've done that you can easily determine which control it was that opened the menu from the menu's Opening event or from the Click event of the ToolStripMenuItem
For example: if you were in the Opening event and wanted to find out which Label was right-clicked on so that you could add different menu items you just need to cast the ContextMenuStrip.SourceControl property:
Label clickedLabel = (Label)contextMenuStrip.SourceControl;
Once you're in the Click event and you want to find out which Label was right-clicked on you can just do it like this:
ContextMenuStrip contextMenu = (ContextMenuStrip)((ToolStripMenuItem)sender).Owner;
Label clickedLabel = (Label)contextMenu.SourceControl;

Related

Refer to a determinate created button

So In my program I’ve putted a flowlayourpanel and with a button I add to the layout infinite new buttons
(button.name = “button” + i.tostring) [i = i + 1]
With another button I want to hide the button with the i=3 so button3.hide(), but it doesn’t work beacause it doesn’t exist yet so How can I refer to the button created when the i was 3?
Name is a property of the Control class so every control has that property. That doesn't mean that it needs to be set though, so some controls don't have a name. When you add a control to your form in the designer, what happens is that the Name property is set AND a field is declared with that name. That's why, if you name a Button control Button1, you are able to use the field Button1 to refer to it.
When you create controls at run time, of course there is no field dedicated to that control so of course you can use such a dedicated field to refer to it. It's up to you to get a reference to that control from wherever you put it when you created it. Where that is is up to you but, if you added it to the Controls collection of a container, that is one option for getting it back. If you set the Name property, you can index the Controls collection with that name to get it, e.g.
Dim myButton = DirectCast(myFlowLayoutPanel.Controls("Button" & i), Button)
There may be other ways of getting that reference too. For instance, if you created your own List(Of Button) and set the Tag of each Button to the number, you could do this:
Dim myButton = myButtonList.Single(Function(btn) CInt(btn.Tag) = i)
When adding your buttons to the FlowLayoutPanel you have correctly assinged a name. You can use this name to refer to the button as shown in Button2.Click.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i = 1 To 5
Dim b As New Button
b.Name = "btn" & i
b.Text = "btn" & i
FlowLayoutPanel1.Controls.Add(b)
Next
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
FlowLayoutPanel1.Controls("btn3").Visible = False
End Sub

Show ContextMenuStripItem without clicking off the current cell

I'm using an UltraGrid which has a ContextMenuStrip with 2 items. These are shown when right-clicking an UltraGridCell.
However, in order to show them, the user has to first click off the cell to take it out of edit mode, then right click on it to show the ContextMenuStripItems.
This has become confusing and irritating to the user, so I was wondering if there is any way that it can be changed to show them when right clicking whilst still in edit mode?
I've tried this to take it out of edit mode after a key is pressed, but it doesn't work.
Private Sub ugComm_keyup(sender As Object, e As KeyEventArgs) Handles ugComm.KeyUp
ugComm.UpdateData()
If ugComm.ActiveCell.IsInEditMode = True Then
ugComm.ActiveCell.Row.Update()
End If
End Sub
I also tried something in the MouseClick that was suggested on the Infragistics forums, but again it didn't work.
Is there any way that a user right-clicking a cell that is in edit mode can bring up the ContextMenuStripItems rather than this menu?
The above image shows what is currently show when right-clicking a cell in edit mode (The cell is the bottom right white cell). I don't want this to appear, but the CMS instead.
EDIT
I've tried the suggestions in the current answers, but neither of those worked for me. Possibly because the grids are a slightly older version?
My most recent effort was done with the following code:
Private Sub ugComm_MouseDown(sender As Object, e As MouseEventArgs) Handles ugComm.MouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
Me.cmCommRate.Show(mouseX, mouseY)
End If
End Sub
But this wasn't triggered until the cell was no longer in edit mode.
NEITHER OF THE ANSWERS BELOW RESOLVE THE ISSUE. STILL NEEDS AN ANSWER
When any cell of the grid enters in edit mode a TextBox is drawn over the cell. The nice part here is this text box is reused for all the cells in the grid. When you right click on the cell in edit mode the default context menu, that comes from MS, shows. What you need to do is get this text box and assign it your context menu strip. You can do this by handling ControlAdded event of the grid like this:
' create a field to store the TextBox
Private cellTextBox As TextBox
Private Sub grid_ControlAdded(sender As Object, e As ControlEventArgs) Handles grid.ControlAdded
' Check if added control is TextBox
If TypeOf e.Control Is TextBox Then
' If added control is TextBox store it in your private field and set its ContextMenuStrip
If Me.cellTextBox Is Nothing Then
Me.cellTextBox = DirectCast(e.Control, TextBox)
Me.cellTextBox.ContextMenuStrip = Me.ctx
End If
End If
End Sub
I have tried to write an event handler for the MouseUp event with this code
Private Sub grid_MouseUp(sender As Object, e as MouseEventArgs) Handles grid.KeyUp
grid.PerformAction(UltraGridAction.ExitEditMode)
grid.ContextMenuStrip.Show()
End Sub
and it works.
The ContextMenuStrip was added in code with this text (as example)
ContextMenuStrip ctx = new ContextMenuStrip()
ctx.Items.Add("Test1")
ctx.Items.Add("Test2")
ctx.Items.Add("Test3")
grid.ContextMenuStrip = ctx

Remove textbox that added dynamically (VB.Net)

I am trying to delete a TextBox control (that added dynamically) in my form through Button_Click event (that I added dynamically too) but I cannot find the exact way to do it. My TextBox will added together with Button control (delete button) when a LinkLabel is clicked. So when added dynamically my textbox.name will be like textbox_1,textbox_2,textbox_3 and along with them is a Button control like btnDel1,btnDel2,btnDel3 (all placed in a Panel control).
My coding goes like this :
Private Sub Button_Click(sender As Object, e As EventArgs)
Dim button As Button = TryCast(sender, Button)
Dim textbox As TextBox = TryCast(sender, TextBox)
'In this case when btnDel1 is clicked, textbox_1 will be removed as well
If button.Name = "btnDel1" Then
PanelOthers.Controls.Remove(button)
End If
End Sub
Button is removed successfully but how do I remove the textbox too? Thanks in advance.
There are a couple ways to do this:
Attach all the relevant controls to the delete button's Tag property.
Create a user control that encapsulates the button and textbox.
Option 1: the Tag property
When you create your controls, add the associated controls to the button's .Tag property:
Dim button As Button = New Button
Dim textbox As TextBox = New TextBox
button.Tag = {textbox}
' Add the button and textbox to the UI surface
Now when the button is clicked you can loop over the associated controls and remove them too:
For Each item As Control In button.Tag
item.Dispose()
Next
button.Dispose()
Option 2: A user control
So isn't a tutorial site.. but you can do your own research on this one.
Here's a place to start: https://msdn.microsoft.com/en-us/library/c316f119%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

Determine an object's runtime X-location

This might be a stupid question, but I'm attempting to move a button to the left by roughly 160 pixels each time the button is pressed. However, I need to know what the x-location is of the object at runtime so I can dynamically add those 160 pixels to it. A real world example of this would be right above (if you happen to be using chrome/firefox-which who isn't?) when the new tab button moves every time a new tab is opened (additionally subtracting those pixels too which is harder because I have to figure out how to handle the tab close event within a QTab control in the QIOS devsuite).
You could do this:
Button1.Location = New Point(Button1.Location.X - 160, Button1.Location.Y)
or this:
Dim pt As Point = Button1.Location
Button1.Location = New Point(pt.X - 160, pt.Y)
or maybe this:
Dim pt As Point = Button1.Location
pt.Offset(-160, 0)
Button1.Location = pt
When you use the WinForm designer, each control on your form is assigned a unique name. When you place a control on the form, the designer automatically assigns a unique name (e.g. Button1), but you can change it to whatever you want. The designer automatically creates a class-level variable (i.e. field) for each control. The name of the variable matches the name of the control. So, for instance, if you call your control Button1, then you can access the X-location of that button via the Button1 variable, like this:
Dim x As Integer = Button1.Left
If you are writing code that is intended to handle events from multiple controls, so you wouldn't know which variable to use, you can use the event handler's sender parameter. Every event handler has a sender As Object parameter which points to the control that is raising the event.
So, for instance, in a the click event, you could do something like this:
Private Sub ClickHandler(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
Dim clickedButton As Button = DirectCast(sender, Button)
Dim x As Integer = clickedButton.Left
End Sub

Stop items in a list view from being selected on right click

I have a ListView on a Windows Form in VB 2010.
I have set the MultiSelect property of the ListView to False so that only one item can be selected at any time.
I have configured a context menu for the ListView and it shows up correctly when the ListView is right clicked.
[Added a ContextMenuStrip control in the Designer and set the ContextMenuStrip property of the ListView to this.]
Consider these 2 scenarios:
A user right clicks on an item that is already selected in the ListView. Then the context menu is displayed and there are no issues.
A user right clicks on an item other than the item that is already selected in the ListView, Then before the context menu is displayed, the item that the user right clicked is selected.
In scenario 2, I need to stop the item that the user right clicks on from being selected automatically. Need to context menu to be displayed but the item that was previously selected should remain selected.
How can I achieve this?
I noticed that on the ListView's MouseDown event, the SelectedItems.Item(0).Index property is still at the old index. However, on the MouseUp event, this property changes to the new index.
In the MouseDown event handler, or anywhere else, how can I stop the SelectedItems from changing? Or how can I change it back to the previous selected item (without the user noticing it is being changed and then changed back)?
I can catch a right click on the MouseDown or MouseUp using the code below. However, I am not sure what I need to put inside this condition to stop the SelectedItems from changing.
If e.Button = Windows.Forms.MouseButtons.Right Then
...
End If
Note: I am able to use the following code for this. However, when I use this with scenario (2), it selects the item that the user right clicked on and then changes it back to the previous item and this change back is seen by the user. Therefore this solution cannot be used.
Dim intPrevSelectedIndex As Integer = -1
Dim boolCancel As Boolean = False
Private Sub ListView1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
If ListView1.SelectedItems.Count > 0 AndAlso e.Button = Windows.Forms.MouseButtons.Right Then
boolCancel = True
intPrevSelectedIndex = ListView1.SelectedItems(0).Index
End If
End Sub
Private Sub ListView1_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseUp
If boolCancel Then
lstWalkResults.Items(intPrevSelectedIndex).Selected = True
boolCancel = False
End If
End Sub
Please let me know any solutions you might have. Thanks for your time!
In the code behind you should be able to handle the right click event. In that method you would display the context menu manually and then ignore the click event preventing the item from being selected.
If e.Button = Windows.Forms.MouseButtons.Right Then
//display context menu because you're handling the click event manually.
...context menu code...
Dim ee As New System.Windows.Forms.MouseEventArgs(Forms.MouseButtons.None, e.Clicks, e.X, e.Y, e.Delta)
e = ee
End If