Better way to accomplish simple task in VB.Net - vb.net

So, I have a small test for school where I need to DISABLE three radio buttons which are in a panel in the least amount of possible code WHILE the contents of two text boxes are empty. As soon as both textboxes are filled, I enable the panel.
The below solution obviously works with one single text box, but what happens with two?
I know I can override each button KeyPress and check both text boxes at the same time. But I wanted to go fancy. The problem is I think there is no way I can solve this problem like this.
Or is it?
Public Class Form1
Public Sub vacios(sender As Object, e As System.Windows.Forms.KeyPressEventArgs)
Panel1.Enabled = (sender.Text <> "")
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler txtNombre.KeyPress, AddressOf vacios
AddHandler txtApellido.KeyPress, AddressOf vacios
End Sub
End Class
Note - I know I can do this But I wanted to be fancy.
Public Class Form1
Private Sub txtNombre_TextChanged(sender As Object, e As EventArgs) Handles txtNombre.TextChanged
Panel1.Enabled = (txtNombre.Text <> "" And txtApellido.Text <> "")
End Sub
Private Sub txtApellido_TextChanged(sender As Object, e As EventArgs) Handles txtApellido.TextChanged
Panel1.Enabled = (txtNombre.Text <> "" And txtApellido.Text <> "")
End Sub
End Class

in VB you can have multiple handles for the same sub just by adding "Handles Object1.Event,Object2.Event ..." at the end.
here an example of what i would do in this case
Public Class Form1
Private Sub panel1TextBoxes_TextChanged(sender As Object, e As EventArgs) Handles txtApellido.TextChanged,txtNombre.TextChanged
Panel1.Enabled = (txtNombre.Text <> String.Empty And txtApellido.Text <> String.Empty)
End Sub
End Class

Related

VB.net One sub for many events

I'm working on a function in VB.Net where a user can select a supplier from a list.
The idea is that the user will filter the list until the right supplier is visible in a datagridview
the user can then either double click on the row header, the cell content or select a supplier and then click an OK button
I am wondering though, how do I avoid building one Sub for each of the above three events, Can I create one sub that catches all three events?
Private Sub supplierSearchOkButton_Click(sender As Object, e As EventArgs) Handles supplierSearchOkButton.Click
initiativeForm.supplierConcatTextBox.Text = supplierSearchDataGridView.SelectedRows(0).Cells(3).Value.ToString()
Me.Close()
End Sub
Private Sub supplierSearchDataGridView_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles supplierSearchDataGridView.CellContentDoubleClick
initiativeForm.supplierConcatTextBox.Text = supplierSearchDataGridView.SelectedRows(0).Cells(3).Value.ToString()
Me.Close()
End Sub
Private Sub supplierSearchDataGridView_RowHeaderMouseDoubleClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles supplierSearchDataGridView.RowHeaderMouseDoubleClick
initiativeForm.supplierConcatTextBox.Text = supplierSearchDataGridView.SelectedRows(0).Cells(3).Value.ToString()
Me.Close()
End Sub
Personally, I would go with the extra method option mentioned in the comments but, if you want to, you should be able to do this:
Private Sub SetSupplier(sender As Object, e As EventArgs) Handles supplierSearchOkButton.Click,
supplierSearchDataGridView.CellContentDoubleClick,
supplierSearchDataGridView.RowHeaderMouseDoubleClick
initiativeForm.supplierConcatTextBox.Text = supplierSearchDataGridView.SelectedRows(0).Cells(3).Value.ToString()
Me.Close()
End Sub
or even this:
Private Sub SetSupplier() Handles supplierSearchOkButton.Click,
supplierSearchDataGridView.CellContentDoubleClick,
supplierSearchDataGridView.RowHeaderMouseDoubleClick
initiativeForm.supplierConcatTextBox.Text = supplierSearchDataGridView.SelectedRows(0).Cells(3).Value.ToString()
Me.Close()
End Sub
If you're not using any properties of the other e parameters then you can use the most general EventArgs for all three events and if you're not using the parameters at all then you can ditch them altogether. I didn't test this specifically but I'm fairly sure both will work.

How to use array when declaring sub VB

I was wondering is there was a way to use an array when declaring a sub.
Example:
Public Class Form1
Private PictureBox(2) as PictureBox
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PictureBox(0) = PictureBox1
PictureBox(1) = PictureBox2
PictureBox(2) = PictureBox3
Private Sub PictureBox_mouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox(1).MouseDown
'do stuff here
end sub
This is the method Plutonix suggested in the comments applied to your code. Note that no array is needed.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each ctrl As Control In Me.Controls
If TypeOf (ctrl) Is PictureBox Then
AddHandler DirectCast(ctrl, PictureBox).MouseDown, AddressOf PictureBox_MouseDown
End If
Next
End Sub
Private Sub PictureBox_MouseDown(sender As Object, e As MouseEventArgs)
'Code here will apply to every PictureBox in your form
End Sub
End Class
Also note that instead of DirectCast(ctrl, PictureBox).MouseDown, you could just use ctrl.MouseDown. Using DirectCast simply helps you avoid making mistakes by cluing IntelliSense and the compiler in on what you're doing.

Textbox Delete Button not Undoing

Basically the code below deletes the selected text from a textbox, although you cannot undo it. Any help on making it undo?
TextBox1.SelectedText = ""
Try a variation of the following...
Public Class Form1
Dim selText as String
Private Sub Button1_Click(sender as object, e as EventArgs) Handles Button1.Click
selText = TextBox1.SelectedText
TextBox1.SelectedTexxt = ""
End Sub
Private Sub Button2_Click(sender as object, e as EventArgs) Handles Button2.Click
TextBox1.AppendText(selText)
End Sub
End Class
Found an answer. Just use this code below
SendKeys.Send("{BS}")

Changing button name if file exists?

I'm working on a custom GUI, my last step is for the button to change automatically check if the file exists or not on startup. The method below is my download/open button. Any help is appreciated!
Private Sub Button7_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button7.Click
If Dir("DownloadedFile.zip") <> "" Then
Process.Start("DownloadedFile.zip")
Else
WC.DownloadFileAsync(New Uri("http://download852.mediafire.com/3a688rz1a6ig/dk71cs34ihs3v6x/Devil+went+down+to+georgia.rar"), "DownloadedFile.zip")
MsgBox("Starting Download")
End If
End Sub
A subroutine is a seperate method that does not return a value. I would take the If statement out of your click eventhandler and put it in its own method as I stated like this.
Private Sub CheckForFile()
If Dir("DownloadedFile.zip") <> "" Then
Process.Start("DownloadedFile.zip")
Else
WC.DownloadFileAsync(New Uri("http://download852.mediafire.com/3a688rz1a6ig/dk71cs34ihs3v6x/Devil+went+down+to+georgia.rar"), "DownloadedFile.zip")
MsgBox("Starting Download")
End If
End Sub
Call it from your button like this.
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
CheckForFile()
End Sub
Then handle the Shown Event and call it from there. It will run as soon as the initial form is shown
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
CheckForFile()
End Sub
Responding to your comment You need to use the WebClient DownloadFileCompleted Event and the WebClient DownloadProgressChanged Event

TextBox Highlighted Text Visual Basic

I have a problem with my TextBox. I'm trying to make a small program like memo. When you write something in the textbox it saves it for next time for you to open it again. But the problem is when you open the program once again, the old text is highlighted. So how I remove that?
Public Class Form1
Private Sub
Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
My.Settings.memo = TextBox1.Text My.Settings.Save()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = My.Settings.memo + Environment.NewLine
End Sub
End Class
See the TextBox.SelectionStart property