Converting C# to VB.net is not working - vb.net

I have translated some code from C# to VB.net for the purpose of getting Folder Browser functionality. The link to the code is here.....
http://www.codeproject.com/KB/aspnet/DirectoryBrowsing.aspx
My issue is that I have not been able to correcly translate these two lines of code to VB.net.
TreeView1.TreeNodeExpanded +=new TreeNodeEventHandler(TreeView1_TreeNodeExpanded);
TreeView1.SelectedNodeChanged += new EventHandler(TreeView1_SelectedNodeChanged);
Every translator I have used has simply dropped the semicolon from the end of each line. But the editor still does not like them.
I could some help with this as it seems this effects the refresh of the selected folder in the tree view control.
I don't get to see the C drive folder unless I type the path in the text box, and the folder will still not expand.
thank you,

Use this:
AddHandler TreeView1.TreeNodeExpanded, AddressOf TreeView1_TreeNodeExpanded
AddHandler TreeView1.SelectedNodeChanged, AddressOf TreeView1_SelectedNodeChanged
Edit:
A different way to do this would be to apply it at the method level:
Protected Sub TreeView1_TreeNodeExpanded(ByVal sender as Object, ByVal e as TreeNodeEventArgs) Handles TreeView1.TreeNodeExpanded
' Some code
End Sub
Protected Sub TreeView1_SelectedNodeChanged(ByVal sender as Object, ByVal e as EventArgs) Handles TreeView1.SelectedNodeChanged
' Some code
End Sub
You should run this in debug to find out what exactly is going on. I find a lot of times when events of this nature are run in asp.net, you have a conflicting event that "resets" the controls you are attempting to change.

Related

Why does my subroutine, which handles a double-click event on a listbox, not work?

I have declared a Sub that is meant to trigger when the listbox 'lstStudents' is double-clicked. However, it does not trigger when this happened. There can't be an error in the code itself as it is auto-generated. Why does the code not function as expected? The code is below:
Private Sub lstStudents_DoubleClick(sender As Object, e As EventArgs) Handles lstStudents.DoubleClick
Msgbox("test")
End Sub
The message box is only present for testing purposes.
Could You try to delete that previous "lstStudents" and add new one then apply the "ListBox1_DoubleClick" on it again to make sure it works.
Otherwise let us know what is going there because I think your code is normally and it should be working 100%.

Visual Basic Detect Mouse Position

So i want to make a maze game in Visual Basic and if the cursor reaches a certain panel, it will show a message box ONCE and then the Form closes.
The question is How?
I've tried
Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
If Cursor.Position = Panel1 Then
MsgBox("Completed")
Application.Exit()
End If
End Sub
And didn't work. I got
Error 1 Overload resolution failed because no accessible '=' can be
called with these arguments:
'Public Shared Operator =(left As System.Drawing.Point, right As System.Drawing.Point) As Boolean': Value of type
'System.Windows.Forms.Panel' cannot be converted to
'System.Drawing.Point'. C:\Documents and Settings\Admin\Local
Settings\Application Data\Temporary
Projects\WindowsApplication1\Form1.vb 4 12 WindowsApplication1
Remember that i want the message box to only appear once, because sometimes when the cursor is on the panel, it shows multiple msgbox until the cursor is outta there.
I want the mouse inside the panel and run a code.
I believe there is an event called 'mouse enter' event you can use so if you type the code for the messagebox in that even for the control you want them to mouseover it will pop up everytime they do that.
For it to to nly pop up once make a counter that adds 1 and dont execute that code if the counter is already on 1.
I had a little search and found: Determine whether the mouse is over a control? (over a Control pixel range)
I just knocked up a test with a button and seemed to work fine. Please adapt to your own needs.
Private Sub Button1_Paint(sender As Object, e As PaintEventArgs) Handles Button1.Paint
Debug.WriteLine(MouseIsOverControl(Button1))
End Sub
Public Function MouseIsOverControl(ByVal c As Control) As Boolean
Return c.ClientRectangle.Contains(c.PointToClient(Control.MousePosition))
End Function
In this example I've just output "true or false" to determine detection. You can test and change it to your own needs to determine what you want to do depending on 'true/false'. Hope this helps.

How to save a changed label when you exit

In my program I have a preview, and edit side.
When you edit using the text boxes on the edit side(right side) and click "save", It should change the label on the right side (preview side). Although when you exit the program and re-open, all the data you entered has disappeared!,
I have tried the below code and had no luck as my result.
Public Class Form1
Private Shared NameBasic As Integer
Public Sub New()
InitializeComponent()
lblNameBasic.Text = Convert.ToString(NameBasic)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
NameBasic = txtFirstBasic.Text
lblNameBasic.Text = Convert.ToString(NameBasic)
End Sub
End Class
Also my goal is to have it be able to take it on a flashdrive onto any computer and have that data still saved within the exe. Is this even manageable? (I am more of a web based programmer so I am a bit new to this)
You need to write values to a text file at the same location of the exe. and read it. you will need both exe and the textfile.
OR
You will need to write a dll on run-time and write some values while closing the application. this will be more secure then the text file. here you will need both exe and dll to be copied into flash drive
OR
the application setting as spoken by others..

Can't remove event handler, adding multiple times

I have a piece of code where I add a handler everytime the form is open, I'm working in visual basic, but the first time I enter the form everything works fine, but for the second time I have 2 handlers, if I enter a third I have 3 handlers and so on. I don't know why is this happenning.
Here is what I tried so far.
I have stored all my machines in another class but I'm sending to myForm to show them, but to add them I use this code:
Private Sub add_machine(ByRef machine As Machine)
RemoveHandler machine.imgBox.Click, AddressOf Me.imgBox_Click
AddHandler machine.imgBox.Click, AddressOf Me.imgBox_Click
Me.Controls.Add(machine.get_imgMachine)
Private Sub imgBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
'Some code
End Sub
Everytime I open the form I call the sub add_machine to add dinamically my machines, as you can see I send them ByRef to simplify my code I tried to put that RemoveHandler since I'm sending ByRef to avoid having more than one handler, but it's not working please help
Thanks in advance.
Two things, DONT pass machine ByRef. You are not returning a new object. Please read this link for more information on when you should use what Byval vs ByRef
Second, add your handlers during form load (or initialization) and remove them in form closing. This will help ensure you are working with references to the same object.

VB 2013 Persistent User Settings

On my form I have a menu with "file-save". When I click save I want to save particular settings to restore when the form is closed and re-opened. I've done this successfully for text in text-boxes and the checked states of check-boxes, but I'm failing when trying to loop through the items in a list-box. Please see below for what I've tried...
When I click save:
Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs)
Handles SaveToolStripMenuItem.Click
For Each i In ListBox1.Items()
My.Settings.ListBox1.Add(i)
Next
My.Settings.Save()
End Sub
When my form loads:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
For Each i In My.Settings.ListBox1()
ListBox1.Items.Add(i)
Next
End Sub
I've only been using VB for three days, so apologies if I am missing something simple ha! Thanks for any help!!!
There is one small glitch with the StringCollection in settings. If you do not seed it with a fake variable then it starts out as Nothing and you cannot add to Nothing. in your form load add this:
' if the collection has not been initialized, do so
If My.Settings.ListBox1 Is Nothing Then
My.Settings.ListBox1= New System.Collections.Specialized.StringCollection
End If
' now it is safe to use: load strings from Setting -> form listbox
For Each s As String In My.Settings.ListBox1()
ListBox1.Items.Add(s)
Next
The very first time it runs, there are likely no saved settings, so we have to create the container for them, basically.
Option Strict can be implemented by file, by adding this at the top:
Option Strict On
Or for the project: Project => Properties => Compile: Option Strict is likely to the right (I have 2012). You can also set it as a permanent option (recommended).
Among other things, this will prevent you from plucking variables out of the air and use them without declaring a type (which will lead to errors). For instance:
For Each i In My.Settings.ListBox1()
becomes
For Each s As String In My.Settings.ListBox1() ' tell the compiler the Type