MessageBox appears at wrong time - vb.net

I have a form which contains a PctureBox. When the user clicks this PictureBox, they will proceed to another form.
In the other form, I have a DataGridView and I have a search TextBox for the user to search content in the DataGridView. However I want to display "no record found" when the user enters something that is not in the database.
The MessageBox displays "no record found" however it also does this when the user first clicks on the PictureBox and the form loads.
Code:
Private Sub TextBox3_TextChanged_1(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
If Me.MasterlistTableAdapter1.FillBySearchProject(Me.DocumentDataSet.masterlist, TextBox3.Text)=0 Then
System.Windows.Forms.MessageBox.Show("No Record has been Found")
End If
End Sub
When I created a Button this code works perfectly. The prompt only displays when no records are found on the search.
I don't want to use a Button to do the search. My search functions when a user only types several words, data found.

Try this
Private Sub TextBox3_TextChanged_1(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
If Not TextBox3.Text = "" Then
If Me.MasterlistTableAdapter1.FillBySearchProject(Me.DocumentDataSet.masterlist, TextBox3.Text)=0 Then
System.Windows.Forms.MessageBox.Show("No Record has been Found")
End If
End If
End Sub

Related

mandatory field in VB and save Button

if somebody can help me in VB coding to show required field to be mandatory when I click save Button and the function of saving should not be work till all required field to be filled
(please visual basic codes not other )
You can use a simple If function that occurs when the button is pressed.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If TextBox1.Text = "" Or TextBox2.Text = "" Then
Msgbox("A field is missing")
Exit Sub 'To avoid saving, escape the procedure
Else
' code to save goes here
End If
End Sub
Here, I assume that TextBox1 and TextBox2 are the fields you want and Button1 is the save button.

How do I use the Tag property with forms and code in VB 2012?

I am writing a program using a database for customers and technicians. The main form (CustomerIncidents) has a toolstripbutton that opens a different form to (SearchByState) where the user inputs a state code and looks for any incidents.
If the user clicks into one of the datagrid cells I want that customers information to be stored in the TAG so that when the form is closed using the OK button that it will show back up in the main form (CustomerIncidents).
Edited 03/11/14 12:21pm
The problem is in the Main Form. When I click the OK button in the Second Form it tries to convert the DialogResult Button to a String. I can't figure out how to fix it.
Customer Form (Main Form) Opens to Secondary Form
Private Sub btnOpenState_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnOpenState.Click
Dim frmSearchState As New FindCustomer
----->>Dim selectedButton As DialogResult = frmSearchState.ShowDialog()
If selectedButton = Windows.Forms.DialogResult.OK Then
CustomerIDToolStripTextBox.Text = frmSearchState.Tag.ToString
End If'
Search By State Form (Secondary Form) Or "Child Form"
Private Sub btnOk_Click(message As String, ByVal e As DataGridViewCellEventArgs) Handles btnOk.Click
message = CustomersDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString
Me.Tag = message
Me.DialogResult = DialogResult.OK
End Sub
The click event for a button does not have a DataGridViewCellEventArgs parameter, and will throw an exception when you try to use it.
You don't need to use the Tag property since you can just create your own property.
In your child form, create a property called GridValue:
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
If dgv.CurrentCell Is Nothing OrElse dgv.CurrentCell.Value Is Nothing Then
MessageBox.Show("A cell needs to be selected.")
Else
Me.DialogResult = DialogResult.OK
End If
End Sub
Public ReadOnly Property GridValue As String
Get
Return dgv.CurrentCell.Value.ToString
End Get
End Property
In your parent form, you can now access your information:
Using frmSearchState As New FindCustomer
If frmSearchState.ShowDialog(Me) = DialogResult.Ok Then
CustomerIDToolStripTextBox.Text = frmSearchState.GridValue
End If
End Using
My personal approach for doing this kind of stuff is to create a public property in the child form, having the same type as the DATA you want to take back to your main form. So instead of storing DataGridView's reference in Tag property, you should really be storing the actual value that was there in the cell that the user clicked on.
For example, if your DGV cell has a string value in it, you could do something like:
Public Readonly Property StateName As String
Get
If YourDGV.SelectedCell IsNot Nothing Then
Return YourDGV.SelectedCell.Value
Else
Return ""
End If
End Get
End Property
(I have written that code by hand, so there may be some syntax problems, but you should be able to get the idea.)
You can now use ShowDialog() in the main form to bring up this child form and upon OK or Cancel, you could check the value of StateName property of your child form to get this value. The thing to remember here is that closing a form doesn't dispose off all its constituent controls and properties and therefore you can access them even after the form has finished ShowDialog() call.

message when leaving a datagridview

I have a datagridview with a menustrip. I want to give the user a message that he has to save when he leaves the datagridview and made any changes in there. What i tried:
Private Sub DGV_validated(sender As Object, e As EventArgs) Handles DGV.validated
If DataSet1.table.GetChanges IsNot Nothing Then
MsgBox("You made changes please press the save button!")
End If
End Sub
I tried that with the leave, validated and lostfocus event but the msgbox pops up not until i am on another form after pressing something in the menustrip.
I just made a forms project with a DataGridView on the form and put this in the code behind and it worked without issue:
Private Sub DataGridView1_MouseLeave(sender As Object, e As System.EventArgs) Handles DataGridView1.MouseLeave
MsgBox("hi")
End Sub
Basically, after the cursor leaves the DataGridView, the MsgBox will pop up. Is that what you were looking for? This was done in Visual Studio 2010.

How to use two forms to search in one and display the records to the other using Visual Basic 2010, SQL Server 2008

I have 2 forms in my visual basic 2010 project. Both are connected with an SQL 2008 Server where i keep a db with customers records(name,surname,address, etc).
What i want to do is to give certain criteria in the first form, lets say name,and hit a search button. The program should search the db if the specified name exists.If it exists i want to show the results in the second form. By results i mean the name , surname, address,etc. If it does not exist i want to prompt the user to insert data into the db.
The problem is that i know how to do this work in a single form but not how to make the two forms "communicate" with each other (meaning that one does the search and the other the projection-adding part).
Any help is appreciated
Edit
I am sorry guys but i cannot figured it out. I tried setting the filters but all i get is errors. I will post whatever i have written so far in case someone can help me out. So here it is.
As i stated earlier i know how to search for the data in a single form(lets call it DisplayForm). As Olivier mentioned i have textboxes for the records, a combo box with the search criteria(name , surname,id) and a Search button that perfoms the search and populates the text boxes. The code for the search button is this:
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Dim intPosition As Integer
'Combo box code
Select Case cboField.SelectedIndex
Case 0
ObjDataView.Sort = "surname"
Case 1
ObjDataView.Sort = "id"
End Select
If cboField.SelectedIndex = 0 Then
intPosition = ObjDataView.Find(txtSearch.Text)
ElseIf cboField.SelectedIndex = 1 Then
intPosition = ObjDataView.Find(CType(txtSearch.Text, Integer))
End If
If intPosition = -1 Then
MessageBox.Show("No customer found", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)
Else
objCurrencyManager.Position = intPosition
End If
End Sub
I want this procedure be done in a second form (lets call it FormSearch) and populate the textboxes in form DisplayForm. I know that it is easy enough for someone that knows VB but i am tottally a newbie and desperatly need help.
Thanks a lot for any advice
In your display form add this code
Public Sub SetFilter(name as String)
' Set the filter here
End Sub
In your filter form do this
DirectCast(Application.OpenForms(0), DisplayForm).SetFilter(NameTextBox.Text)
Here I assume that DisplayForm is your main form and that it's name is DisplayForm. You would call this in a button click event hanlder, for instance.
EDIT in response to comment:
You said that you know how to do it on a single form. I do not know how you do it, but probably you have textboxes for the name, the address, etc. that you are looking for. Then probably you press a button called btnSearch. By pressing this button you trigger
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch_Click.Click
' The search logic goes here
End Sub
What I am telling you is to replace btnSearch_Click by Public Sub SetFilter(name as String) that is public. You would move those search fields and the button to the second form and from there call SetFilter of the first form in order to communicate with it and tell it to search a customer. In my example, I pass only a name, but I left it up to you to pass more parameters as required.
Assuming that your customer form is called "DisplayForm", you could access it like this
Dim cust As DisplayForm
cust = DirectCast(Application.OpenForms("DisplayForm"), DisplayForm)
cust.SetFilter(NameTextBox.Text)
My answer was only about the communication between the two forms, assuming that you already managed the other part.
Searching for "VB.NET Tutorial: Working With Data" on YouTube results in six videos on this subject. Video #3 in particular shows how to use filters.
EDIT #2. Here is a little more help:
I do not know how you are opening the two forms. Since you need a reference to the display form in the search form, it is easier to open the search form as the main form at application startup. At Form_Load in the search form, you then open the display form by code
Code in the search form:
' Reference to the display form
Private _displayForm As DisplayForm
' Create the display form and open it
Private Sub SearchForm_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
_displayForm = New DisplayForm()
_displayForm.Show()
End Sub
' Get the search parameters and tell the display form to set the filter
Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click
Dim field As String, search As Object
Select Case cboField.SelectedIndex
Case 0
field = "surname"
search = txtSearch.Text
Case 1
Dim n As Integer
field = "id"
If Integer.TryParse(txtSearch.Text, n) Then
search = n
Else
MessageBox.Show("Please enter a number for id search", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)
Return
End IF
End Select
_displayForm.SetFilter(field, search)
End Sub
Code in the display form that actually performs sorting and searching:
Public Sub SetFilter(ByVal field As String, ByVal search As Object)
Dim intPosition As Integer
ObjDataView.Sort = field
intPosition = ObjDataView.Find(search)
If intPosition = -1 Then
MessageBox.Show("No customer found", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning)
Else
objCurrencyManager.Position = intPosition
End If
End Sub

Help with events

I have multiple textboxes which I want them to perform the same thing upon clicking them. By default I can use the handles textbox1.click for 1 single textbox as shown below but I am not sure how to do handle multiples of them. Of course I can write a handler for every single textbox but I have about 50 of them. I am sure there must be a more efficient way. Please advice. Thanks.
Sub TextBox1_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Click
If Button9.Text = "Make Changes" Then
If TextBox2.Text <> "" Then
Frm_Cine1.Show()
Frm_Cine1.chooseCine(ComboBox1.SelectedItem)
Else
MsgBox("Please check input!")
Exit Sub
End If
End If
End Sub
why don't you create a customizable textbox?
If Button9.Text = "Make Changes" Then
If TextBox2.Text <> "" Then
These two lines are going to be same for all those 50 buttons?
If yes, then I think you can assign same event handler to each of the button's click event.
Other way is, create one private method which takes one string as an argument and returns boolean value depending on whether your string is blank or not and call this method from all the 50 button's click event.
Thanks for all your advices, I am not sure if this is what you guys are suggesting but apparently it is how I wanted it to work:
Sub TextBoxs_click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles TextBox2.Click, TextBox3.Click, TextBox4.Click 'This part is disturbing if I have 50 textboxes...
'For Each obj As Control In Panel2.Controls
If sender.GetType.ToString = "System.Windows.Forms.TextBox" Then
Dim txtbox As TextBox = sender
textbox_verification(txtbox)
End If
'Next
End Sub
Sub textbox_verification(ByVal txtbox As TextBox)
If Button9.Text = "Make Changes" Then
If txtbox.Text <> "" Then
Frm_Cine1.Show()
Frm_Cine1.chooseCine(ComboBox1.SelectedItem, "FILE1-->This should be a variable")
Else
MsgBox("Please check timings input!")
Exit Sub
End If
End If
End Sub
If you indeed need to use the same click handler for multiple test boxes, you can use the AddHandler command to associate the click event of each test box with the handler routine, as shown:
AddHandler TextBoxX.Click AddressOf TextBox1_Click
You will need to add this statement to your program (maybe in the form load routine) once for each text box you want to handle. (Using the name of each text box in place of the "TextBoxX" in the above code.)