ToolStripTextBox action on pressing Enter key - vb.net

I've got a similar issue - i've created a Windows Form Application which basically looks up data from a SQL server based on a code entered into the ToolStripTextBox. This works great when the button is clicked however I'd like to add the function of hitting enter after typing and that brings the results through.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'DataSet.Table' table. You can move, or remove it, as needed.
Me.TableTableAdapter.Fill(Me.DataSet.Table)
End Sub
Private Sub FillBySearchToolStripButton_Click(sender As Object, e As EventArgs) Handles FillBySearchToolStripButton.Click
Try
Me.TableTableAdapter.FillBySearch(Me.DataSet.Table, TYPEToolStripTextBox.Text)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
End Class
Appologies as I am a newbie.

Just use the KeyDown event:
Private Sub ToolStripTextBox1_KeyDown(sender As Object, e As KeyEventArgs) _
Handles ToolStripTextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
'run your code
End If
End Sub
The "run your code" block could just call the click event:
FillBySearchToolStripButton.PerformClick()

Related

Cannot access a disposed object in VB.NET

Having a small issue.
I am receiving a Cannot access a disposed object error for Form1
Upon clicking a menu item on the main form - the below Sub is called, which opens another form Form1
Private Sub ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem.Click
If (Not Form1.Visible) Then
Form1.Show(Me)
End If
End Sub
Within Form1, there is a Try block. If it isn't passed, Form1 should show a message box, before closing down. The message appears, but it's then that I receive the error (where it says Form1.Show(Me))
Private Sub Form1_Load(ByVal sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Try
'DO STUFF
Catch
MsgBox("Error loading in data. Please contact an administrator")
Me.Close()
Return
End Try
End Sub
I am quite new to this type of programming, and struggling to fix the problem even after searching similar problems. Could someone please assist or point me in the right direction?
EDIT: So looks like this is due to trying to close the form during the Load event. So my question now is, are there any simple alternatives? I've found ways of doing this in C#, but not a lot for vb.net
Here is one option:
Private loadFailed As Boolean = False
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
'...
Catch ex As Exception
loadFailed = True
End Try
End Sub
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
If loadFailed Then
MessageBox.Show("Load failed")
Close()
End If
End Sub
In that case, the form will show with the message over it, then it will close when the message is dismissed. Here's an option that will not display the form:
Friend Module Form1Manager
Public Sub ShowForm1(owner As Form)
If Not Form1.Visible Then
Try
'...
'Pass data to Form1 here.
Form1.Show(owner)
Catch ex As Exception
MessageBox.Show("Load failed")
End Try
End If
End Sub
End Module
and, to use that:
Private Sub ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem.Click
Form1Manager.ShowForm1(Me)
End Sub

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.

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

Trying to enter a name in a textbox, hit ok, and have that form close and pass the info to another forms label

***THE CODE FROM THE FIRST WINDOW GOES INTO A TEXT BOX AND YOU HIT THE BUTTON HERE.
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
Me.Close()
End Sub
***THE CODE ON THE FORM WHERE I WANT THE INFO TO BE PLACED IS HERE.
Private Sub Loan1Form_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.lblCompanyName.Text = DataEntryForm.txtCompanyNameInput.Text
End Sub
Anyway's that's what I found on a youtube video and im having trouble getting it to work. Any help would be appreciated.
Thanks.
Pass the data to an instance of the form:
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
Dim f As New OtherForm
f.lblCompanyName.Text = txtCompanyNameInput.Text
f.Show()
Me.Close() 'make sure this form is not the one that closes the app if it closes
End Sub

VB.net opening and closing forms

I have a VB program that has two forms, i have coded the form load of each forms.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MessageBox.Show("I AM FORM 1")
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MessageBox.Show("I AM FORM 2")
End Sub
Here is how i switch through Form1 and Form2, i made use of a button.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
Form1.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
Form2.Show()
End Sub
But everytime i switch forms the form load event will only trigger once. Is there something wrong with my code? I am guesing the Me.Hide() will only hide the previous form and not totally close it. I want to be able to close the previous form so that when i will open it again, the form load event will trigger again.
But everytime i switch forms the form load event will only trigger once. Is there something wrong with my code? I am guesing the Me.Hide() will only hide the previous form and not totally close it.
This is exactly what is happening. The Hide method just hides the form from the user, effectively making it invisible.
What you're looking for is the Close method, which actually closes the form. (Since you are displaying the form using the Show method, you do not need to call Dispose.)
You will, however, not be able to close a form and continue to run code in its methods. So you'll need to reverse the order of the statements in your event handler functions, displaying the other form first and then closing itself. Make them look like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form1.Show()
Me.Close()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Show()
Me.Close()
End Sub
That will do what you want. The Load event will be triggered each time you call the Show method, because you're creating and showing a new form.
It is worth pointing out, though, that you're relying on an unusual characteristic of VB.NET, one that it retains from the older VB languages for backwards compatibility reasons. Instead of referring to an object of your form class (like you would have to do with all other class objects), you are referring to it by the type name (the name of the class itself). You really shouldn't do that, it causes all sorts of headaches and will confuse people reading your code. It is better to just instantiate a new form object, like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim frm As New Form1 ' create a new Form1 object
frm.Show() ' ... and display it
Me.Close()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim frm As New Form1 ' create a new Form2 object
frm.Show() ' ... and display it
Me.Close()
End Sub
When you run this code, you will likely run immediately into another problem: the first time you close Form1, your entire application will quit. This is because, by default for a new project, Form1 is designated as the "Startup form" in your project's properties ("My Project" in the Solution Explorer). You will either have to:
create a third form to use as the "main" form, and set the "Startup form" to this third form, or
change the "Shutdown mode" (also in "My Project") from "When startup form closes" to "When last form closes".
I am guesing the Me.Hide() will only hide the previous form and not totally close it
Yes, it does what it says. If you want to close the form then use Me.Close() instead. The Load event will fire again when you create the new instance.
You'll have to change a setting to ensure that doesn't also close your application. Project + Properties, Application tab, change the Shutdown mode setting to "When last form closes". And put the Me.Close() call after the Show() call.
I also had a similar question. When u .Hide() you are just storing it away in memory somewhere such that when it is re-opened it doesnt have to make a new form just recalls the one from memory hence that method is not called again. You have to destroy the form. So what you can do when navigating to another form is go to that form first and then destroy the current form like so Form2.Show()Me.Close(). Look at my question and my accepted answer. If that works please dont forget to tick this as your accepted answer.
When my form is hidden and reloaded from another form it is not executing the code in the Load event
If MessageBox.Show("Are you sure to close this application?", "Close",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
frmIndex.Show() //the main form
Else
e.Cancel = True
Me.Show() // The form open
End If
The form open going closing and going back to the main/index form. hope it help :) just play with the .show, .hide and e.cancel
I think you using a silly construction, but you should;
Private Sub Form2_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Form1.close()
End Sub
Use the Shown event.
And use ShowDialog()
Form1.ShowDialog()
Yes. What you are doing is closing the form before it could open up form2.
Instead Of:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
Form1.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Hide()
Form2.Show()
End Sub
You Need To Put:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form1.show
Me.hide
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.show
Me.hide
End Sub
If This Helps, Please Reply.