How do I use prevHoveredNode property handling event of TreeView?
Event is fired when I click on a node of a TreeView (TSTViewAnalisi)
In output window I read the correct value but when I compile a project, VS2019 marks one error of non existing property.
Private Sub TSTViewAnalisi_MouseUp(sender As Object, e As MouseEventArgs) Handles TSTViewAnalisi.MouseUp
Dim a As String = sender.SelectedNode.prevHoveredNode.Text
The basic EventHandler Delegate passes you an Object as the first parameter. Object does not have a definition of SelectedNode. But this is expected and common, and you just need to cast the sender to the appropriate type,
Dim s = DirectCast(sender, TreeView)
Dim a = s.SelectedNode.prevHoveredNode.Text
or you could directly access the control
Dim a = TSTViewAnalisi.SelectedNode.prevHoveredNode.Text
Related
I Intended to display an PictureBox in my form when the mouse hovered over another control. I then wanted to use a separate event for when the mouse left the control. This event would remove the displayed PictureBox from controls. However, because my events are private subs, I can't directly access the name of the control in the latter event. A solution to this would be a method that removes the most recently added control. If no such method exists, or there is an alternative way of approaching this problem, any help would be appreciated.
I tried simply using Controls.Remove(), but this requires a parameter. The name of the control as a string did not work either, as the parameter must be a control itself.
Private Sub Tile_MouseEnter(Sender As Object, e As EventArgs)
Dim CloseUpPic As New PictureBox With {Properties}
CloseUpPic.Image = Sender.Image
Controls.Add(CloseUpPic)
Refresh()
End Sub
Private Sub Tile_MouseLeave(Sender As Object, e As EventArgs)
Me.Controls.Remove()
End Sub
The program won't compile due to .Remove() needing a parameter
I expected for the Control to be created and displayed when the mouse entered the tile, and to cease to exist when the mouse left the tile.
For future reference, controls have Tag property which allows you to store whatever you like. In this case, you can store a reference to the newly created PictureBox. Furthermore, the "Sender" parameter tells you which control was the source of the event. You can cast sender to a control, then store the reference. Then, in the leave event, you can cast sender to a control, cast the .Tag to a control, and finally remove it:
Private Sub Tile_MouseEnter(Sender As Object, e As EventArgs)
Dim ctl As Control = DirectCast(Sender, Control)
Dim CloseUpPic As New PictureBox With {Properties}
CloseUpPic.Image = Sender.Image
Controls.Add(CloseUpPic)
ctl.Tag = CloseUpPic
Refresh()
End Sub
Private Sub Tile_MouseLeave(Sender As Object, e As EventArgs)
Dim ctl As Control = DirectCast(Sender, Control)
Dim ctlToRemove As Control = DirectCast(ctl.Tag, Control)
Me.Controls.Remove(ctlToRemove)
End Sub
I ended up using the following code to solve my issue:
For Each Closeup In Controls.OfType(Of CloseUp)
Controls.Remove(Closeup)
Next
I created a new class of my own called Closeup, that inherits PictureBox. I then looped through each Closeup in controls (There was only one but this code works for multiple controls), and removed them.
I have a control on a form UserNameCtrl and that control has a sub called LoadCtrl
I essentially have loads of these subs for clicks, so I want to put them all into one event handler
Private Sub NewsletterBtn_Click(sender As Object, e As EventArgs) Handles NewsletterBtn.Click, NewsletterImage.Click
If Not MainNewsletterCtrl.Loaded() Then MainNewsletterCtrl.Load()
End Sub
However within each of the subs the control names are hardcoded to call the .loaded and .load functionality.
I've wrote a new version of this
Private Sub GenericNavItem_Click(sender As Object, e As EventArgs)
Dim ctrl As Control = Controls.Find(sender.tag, True).FirstOrDefault
'Want to do the Controlname.Load here
End Sub
Using the tag (which I named as the control name) I got the corresponding control. But it's bringing back it as a control rather than of the type I want it to be.
I know I declare it as Control, but I don't know how I can cast it to be the ControlName.Load rather than the generic control.
If they are all the same class (or base class), then just cast to that class. If they are all different class but have the same method Load and Loaded, then I suggest you create an interface.
Interface ISomeName
Sub Load()
Function Loaded() As Boolean()
End Interface
Make sure all your class implement it and then just cast to that interface.
Private Sub GenericNavItem_Click(sender As Object, e As EventArgs)
Dim ctrl As Control = Controls.Find(sender.tag, True).FirstOrDefault
Dim ctrlInterface As ISomeName = CType(ctrl, ISomeName)
If Not ctrlInterface.Loaded() Then ctrlInterface.Load()
End Sub
I am adding buttons through code. So there is a container control and every time the user clicks "Add button", a button gets added to the container control this way:
Dim btnItem As New Button
btnItem.Text = strName
btnItem.Name = "btn-" & strName
AddHandler btnItem.Click, AddressOf f_ViewNameInfo
ButtonContainer.Controls.Add(btnItem)
Problem is inside f_ViewNameInfo function, how do I know which button send the command. In this function I want to get the strName part of the button's name and then work from that. I tried to give parameters to the function, but then the AddHandler part of the above code threw errors, as I am not supposed to give parameters there.
Take a look at the definition of the Click event:
Public Event Click(ByVal sender As Object, ByVal e As System.EventArgs)
Define your function f_ViewNameInfo with these two parameters, and the button that raises it can be obtained from the sender parameter (you will need to cast it to a button first with DirectCast(sender, Button)):
Public Sub f_ViewNameInfo(ByVal sender As Object, ByVal e As System.EventArgs)
Dim str As String = DirectCast(sender, Button).Name
End Function
Note that you should add more safety etc around this, such as checking that sender really is a button before attempting to cast it.
I have a problem similar to Variable with Value of a Label Name
But instead of a label, I am trying to use a ListBox
Private Sub processLog(ByVal logFileName As String, ByVal logCateory As String)
Dim variableListBox As New ListBox
variableListBox = DirectCast(Me.Controls(logCateory), ListBox)
variableListBox.Items.Add("HELLO")
End Sub
What could possible be wrong with the above code, it return NullReferenceException was unhandled Object reference not set to an instance of an object. on the line, variableListBox.Items.Add("HELLO").
I have also a timer to call the above Sub:
Private Sub tmrProcessLogs_Tick(sender As Object, e As EventArgs) Handles tmrProcessLogs.Tick
processLog(fileGeneral, lbxGeneral.Name.ToString)
End Sub
The most likely reason is that the parent of the given control is not the Main Form and, as far as Me.Controls("name") only looks for controls whose parent is the Main Form, variableListBox is Nothing and thus you trigger the error while intending to access Items.Add("HELLO"). Replace
variableListBox = DirectCast(Me.Controls(logCateory), ListBox)
variableListBox.Items.Add("HELLO")
With:
Dim ctrls() As Control = Me.Controls.Find(logCateory, True)
If (ctrls.Count = 1 AndAlso TypeOf ctrls(0) Is ListBox) Then
variableListBox = DirectCast(ctrls(0), ListBox)
variableListBox.Items.Add("HELLO")
End If
All this by assuming that logCateory contains the name of one of the controls in the form (a parent or a child at any level).
When using flash, I was able to get to the focus of an event by accessing the event's "target" attribute.
so if I remember, it was something similar to.
button1.addEventListener(mouse_click, doSomething);
doSomething(e: Event){
e.target.size = 50000;
}
And I'm looking for the equivalent in VB.
If you can give me it's common name across all languages, I'd be doubly grateful. I don't quite know what to search for aside from "event.target VB.net equivalent, and that's not returning anything.
Thanks in advance.
edit: for those new to flash. By focus, I mean the physical object that was clicked on. So the example given would be accessing the clicked button's size.
In VB you can wire up event handlers declaratively using the WithEvents keyword or imperatively using AddHandler.
Private WithEvents myButton
' OR
Public Sub New
Dim newButton = New Button()
AddHandler newButton.Click, AddressOf MyClickHandler
End New
'To consume it you declare a method as follows:
' The Handles clause is used when declaring WithEvents
Private Sub MyClickHandler(sender As Object, e As EventArgs) Handles myButton.Click
' The sender has a handle on the object that raised the event (aka the button)
Dim btn = DirectCast(sender, Button)
btn.Size = New Size(500, 500)
End Sub
Got it!
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclick.aspx#Y0
Sub GreetingBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
'When the button is clicked,
'change the button text, and disable it.
Dim clickedButton As Button = sender
clickedButton.Text = "...button clicked..."
clickedButton.Enabled = False
End Sub
The first parameter (sender by default) references the focused object. You can access it as you would any other normal variable, but it's information won't appear in the auto complete list unless you set it "As" that specific data type.
So I ended up with this
Private Sub nw_btn_Click(ByVal sender As System.Windows.Forms.Button, ByVal e AsSystem.EventArgs) Handles nw_btn.Click
sender.Hide()
End Sub