DateTimePicker: the box goes back to it's original date? - vb.net

Hey guys so im doing a program for a zodiac finder on vb,The program works and all but my problem is that everytime I run it the DateTimePickerBox goes back to the current date instead of the date I have entered.
Here's my unfinished code;
Public Class HoroscopeSign
Private Sub HoroscopeSign_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'DateTimePicker.Format = DateTimePickerFormat.Custom
'DateTimePicker.CustomFormat = "dd MMMM yyyy"
txtDOB.Format = DateTimePickerFormat.Short
End Sub
Private Sub btnEnter_Click(sender As System.Object, e As System.EventArgs) Handles btnEnter.Click
Dim DOB As Date = txtDOB.Value
txtDOB.Text = Process(DOB)
End Sub
Public Function Process(dateOfBirth As Date) As String
Dim sign As String
Dim info As String
Dim BirthYear As Integer = dateOfBirth.Year
Dim BirthMonth As Integer = dateOfBirth.Month
Dim BirthDay As Integer = dateOfBirth.Day
If txtDOB.Value >= New Date(BirthYear, 1, 20) And txtDOB.Value <= New Date(BirthYear, 2, 18) Then
PBHoroscope.Load("E:\Aquarius.jpg")
sign = "Aquarius"
info = ""
Else txtDOB.Value >= New Date(BirthYear, 2, 19) And txtDOB.Value <= New Date(BirthYear, 3, 20) Then
PBHoroscope.Load("E:\Pisces.jpg")
sign = "Pisces"
lblSign.Text = sign
lblDescription.Text = info
End Function
Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
Dim a As Integer
a = MsgBox("Do you want to exit this application", vbYesNo, "Exit Application")
If a = vbYes Then
MsgBox("Thank you for using this application", vbOKOnly, "Exit Application")
End
Else
Me.Show()
End If
End Sub
Private Sub txtDOB_ValueChanged(sender As System.Object, e As System.EventArgs) Handles txtDOB.ValueChanged
Dim DOB As Date = txtDOB.Value
DOB = txtDOB.Value
End Sub
End Class

So you have declared your function Process to return a string, but you don't return anything from that function. The problem then arises when you try to assign the return value of that function to the txtDOB.Text property. You don't return anything and so the txtDOB resets itself to the current date
You should decide what string (in a date format) you want to return or better change your Process function to return a DateTime and assign that value to txtDOB.Value property. Or just change these lines to
Private Sub btnEnter_Click(sender As System.Object, e As System.EventArgs) Handles btnEnter.Click
' REMOVE THIS LINE Dim DOB As Date = txtDOB.Value
' REMOVE THIS LINE txtDOB.Text = Process(DOB)
Process(txtDOB.Value)
End Sub

Related

I want to stop data importing from other textbox too when user age is under 24

I have a listbox for customer name textbox, one listbox for nationality textbox and one DateTimePicker for date of birth listbox. One submit button for everthing to input. I want want to make sure if Customer age is < 24 then nothing will import in listboxs. I manage to show error for the date of birth if the customer is under 24 but it still imports everything from textbox.
Private Sub txtbirth_ValueChanged(sender As Object, e As EventArgs) Handles txtbirth.ValueChanged
Dim thisYear As Integer = DateTime.Now.Year
Dim yourYear As Integer = txtbirth.Value.Year
If thisYear - yourYear < 24 Then
MessageBox.Show("Customer must be 24 and over")
End If
End Sub
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
lstFname.Items.Add(txtForename.Text)
txtForename.Text = ""
lstSname.Items.Add(txtSurname.Text)
txtSurname.Text = ""
lstBirth.Items.Add(txtBirth.Text)
txtBirth.Text = ""
lstNationality.Items.Add(txtNationality.Text)
txtNationality.Text = ""
lstLicence.Items.Add(txtLicence.Text)
txtLicence.Text = ""
Pancustomer.Hide()
Pancusbar.Hide()
End Sub
The DateTimePicker.ValueChanged event is very eager to fire every time you touch the calendar, so I guess that's the issue. Why not just check the age when you click the button?
Private minAge As Integer = 24
Private birthday As DateTime
' txtbirth is a DateTimePicker
Private Sub txtbirth_ValueChanged(sender As Object, e As EventArgs) Handles txtbirth.ValueChanged
birthday = txtbirth.Value
End Sub
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
If userIsOldEnough() Then
lstFname.Items.Add(txtForename.Text)
txtForename.Text = ""
lstSname.Items.Add(txtSurname.Text)
txtSurname.Text = ""
lstBirth.Items.Add(txtBirth.Text)
txtBirth.Text = ""
lstNationality.Items.Add(txtNationality.Text)
txtNationality.Text = ""
lstLicence.Items.Add(txtLicence.Text)
txtLicence.Text = ""
Pancustomer.Hide()
Pancusbar.Hide()
Else
MessageBox.Show($"Customer must be {minAge} and over")
End If
End Sub
Private Function userIsOldEnough() As Boolean
Return getAge(birthday) > minAge
End Function
Private Shared Function getAge(birthday As DateTime) As Integer
Dim now = DateTime.Now
Dim age = now.Year - birthday.Year
If now < birthday.AddYears(age) Then age -= 1
Return age
End Function

Passing Values from (DataGridView1_Click) in a Form to another sub in another form

I want to pass value from (DataGridView1_Click) to another sub which is in another form
How To Achieve that?
Is there any way to do that, Please help me
Public Class SearchCustomers
Private Sub SearchCustomers_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtCustomerSearchBox.Text = ""
CBCustomerSearch.SelectedIndex = -1
txtCustomerSearchBox_TextChanged(Nothing, Nothing)
End Sub
This the click Event
' Private Sub DataGridView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.Click
'FrmCustomers mycustomers = New FrmCustomers()
' mycustomers.show_data(DataGridView1.CurrentRow.Cells(1).Value.ToString)
' End Sub
Private Sub DataGridView1_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
For I = 0 To DataGridView1.Rows.Count - 1
DataGridView1.Rows(I).Cells(0).Value = "Go"
Dim row As DataGridViewRow = DataGridView1.Rows(I)
row.Height = 25
Next
End Sub
Private Sub DataGridView1_CellContentClick( ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
Private Sub DataGridView1_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.Click
Dim oForm As New FrmCustomers()
Dim CustomerCode As String
CustomerCode = (DataGridView1.CurrentRow.Cells(1).Value.ToString)
oForm.show_data(CustomerCode)
MsgBox(DataGridView1.CurrentRow.Cells(1).Value.ToString, MsgBoxStyle.Exclamation, "Warning Message")
End Sub
End Class
this is the sub method in form 2
I want from this method to show data from DB to TextBox as seen in the code below
Sub show_data(CustomerCod)
OpenFileDialog1.FileName = ""
Dim sqls = "SELECT * FROM Customers WHERE CustomerCode=N'" & (CustomerCod) & "'"
Dim adp As New SqlClient.SqlDataAdapter(sqls, SQLconn)
Dim ds As New DataSet
adp.Fill(ds)
Dim dt = ds.Tables(0)
If dt.Rows.Count = 0 Then
MsgBox("no record found", MsgBoxStyle.Exclamation, "warning message")
Else
Dim dr = dt.Rows(0)
On Error Resume Next
CustomerCode.Text = dr!CustomerCode
CustomerName.Text = dr!CustomerName
Address.Text = dr!Address
Country.Text = dr!Country
City.Text = dr!City
Fax.Text = dr!Fax
Mobile.Text = dr!Mobile
Email.Text = dr!Email
Facebook.Text = dr!Facebook
Note.Text = dr!Note
'====================== Image Reincyrpation
If IsDBNull(dr!Cust_image) = False Then
Dim imgByteArray() As Byte
imgByteArray = CType(dr!Cust_image, Byte())
Dim stream As New MemoryStream(imgByteArray)
Dim bmp As New Bitmap(stream)
Cust_image.Image = Image.FromStream(stream)
stream.Close()
Label16.Visible = False
'================================================
End If
BtnEdit.Enabled = False
BtnDelete.Enabled = False
BtnSave.BackColor = Color.Red
CustomerName.Focus()
End If
End Sub
You can do like this (just to Call):
Private Sub DataGridView1_Click(sender As Object, e As EventArgs) Handles DataGridView1.Click
FrmCustomers.Show()
FrmCustomers.Hide()
FrmCustomers.show_data(CustomerCod)
FrmCustomers.Dispose()
End Sub

Visual basic 2010, database – how to fill specific field

I'm creating database connection in Visual Basic. When user click on "borrow book" it transfer chosen data into table used for lent books. It all works alright, but I need last thing. In my database in lent table I have attribute "End date of lent" and I want fill it everytime when user borrow a book with date today + 1 month (for instance, if now is 19/04/2016, end date will be 19/05/2016). I created method "BorrowTime" for this, but I don't know what I should type into and how can I get value of inserted row into "row". And sorry for horrible look of code and form...
Public Class frm_UserView
Dim BooksSource As New BindingSource
Dim BorrowSource As New BindingSource
Dim BooksView As New DataView
Dim BorrowView As New DataView
Dim ds As New DataSet
Dim Borrow As Byte
Private Sub frm_UserView_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
tmr_Timer.Start()
Me.LoanerBooksTableAdapter.Fill(Me.GMITLibraryDataSet1.LoanerBooks)
Me.BooksTableAdapter.Fill(Me.GMITLibraryDataSet.Books)
BooksSource = dgd_UserBookView.DataSource
BooksView = CType(BooksSource.List, DataView)
ds = BooksView.DataViewManager.DataSet.Clone
BorrowSource.DataSource = ds
BorrowSource.DataMember = "Books"
BorrowView = CType(BorrowSource.List, DataView)
dgd_User_Borrow_View.DataSource = BorrowSource
End Sub
Private Sub btn_Borrow_Click(sender As System.Object, e As System.EventArgs) Handles btn_BorrowBook.Click
Borrow = 1
MoveBooks(dgd_UserBookView, BorrowView, Borrow)
dgd_User_Borrow_View.Sort(dgd_User_Borrow_View.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
BorrowSource.MoveLast()
BorrowSource.MoveLast()
End Sub
Private Sub MoveBooks(ByRef source As DataGridView, ByRef target As DataView, ByRef borrow As Byte)
For i = 0 To source.SelectedRows.Count - 1
Dim numberOfRow As Integer
Dim row As DataRowView
row = target.AddNew()
Dim col As Int16 = 0
For Each cell As DataGridViewCell In source.SelectedRows(i).Cells
row.Item(col) = cell.Value
If borrow = 1 Then
numberOfRow = 0
BorrowTime(numberOfRow, dgd_User_Borrow_View)
End If
col = col + 1
Next
Next
Dim count As Int16 = source.SelectedRows.Count
For i = 0 To count - 1
source.Rows.RemoveAt(source.SelectedRows(0).Index)
Next
End Sub
Private Sub BorrowTime(ByRef row As Integer, ByRef dgd_Table As DataGridView)
'Code for adding date
End Sub
Private Sub btn_ReturnBook_Click(sender As System.Object, e As System.EventArgs) Handles btn_ReturnBook.Click
Borrow = 0
MoveBooks(dgd_User_Borrow_View, BooksView, Borrow)
dgd_UserBookView.Sort(dgd_UserBookView.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
BooksSource.MoveLast()
BooksSource.MoveLast()
End Sub
Private Sub btn_UserLogOut_Click(sender As System.Object, e As System.EventArgs) Handles btn_UserLogOut.Click
frm_Login.Show()
Me.Hide()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles tmr_Timer.Tick
Dim countBooks As Integer
Dim countLent As Integer
countBooks = BooksSource.Count
countLent = BorrowSource.Count
lbl_BookCounter.Text = "There are " + countBooks.ToString + " books"
lbl_BorrowCounter.Text = "You have lent " + countLent.ToString + " books"
End Sub
End Class
I need to put code here:
Private Sub BorrowTime(ByRef row As Integer, ByRef dgd_Table As DataGridView)
'Code for adding date
End Sub
My form
Public Class frm_UserView
Dim BooksSource As New BindingSource
Dim BorrowSource As New BindingSource
Dim BooksView As New DataView
Dim BorrowView As New DataView
Dim ds As New DataSet
Dim Borrow As Byte
Private Sub frm_UserView_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
tmr_Timer.Start()
Me.LoanerBooksTableAdapter.Fill(Me.GMITLibraryDataSet1.LoanerBooks)
Me.BooksTableAdapter.Fill(Me.GMITLibraryDataSet.Books)
BooksSource = dgd_UserBookView.DataSource
BooksView = CType(BooksSource.List, DataView)
ds = BooksView.DataViewManager.DataSet.Clone
BorrowSource.DataSource = ds
BorrowSource.DataMember = "Books"
BorrowView = CType(BorrowSource.List, DataView)
dgd_User_Borrow_View.DataSource = BorrowSource
End Sub
Private Sub btn_Borrow_Click(sender As System.Object, e As System.EventArgs) Handles btn_BorrowBook.Click
Borrow = 1
MoveBooks(dgd_UserBookView, BorrowView, Borrow)
dgd_User_Borrow_View.Sort(dgd_User_Borrow_View.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
BorrowSource.MoveLast()
BorrowSource.MoveLast()
End Sub
Private Sub MoveBooks(ByRef source As DataGridView, ByRef target As DataView, ByRef borrow As Byte)
For i = 0 To source.SelectedRows.Count - 1
Dim numberOfRow As Integer
Dim row As DataRowView
row = target.AddNew()
Dim col As Int16 = 0
For Each cell As DataGridViewCell In source.SelectedRows(i).Cells
row.Item(col) = cell.Value
If borrow = 1 Then
numberOfRow = 0
BorrowTime(numberOfRow, dgd_User_Borrow_View)
End If
col = col + 1
Next
Next
Dim count As Int16 = source.SelectedRows.Count
For i = 0 To count - 1
source.Rows.RemoveAt(source.SelectedRows(0).Index)
Next
End Sub
Private Sub BorrowTime(ByRef row As Integer, ByRef dgd_Table As DataGridView)
'Code for adding date
End Sub
Private Sub btn_ReturnBook_Click(sender As System.Object, e As System.EventArgs) Handles btn_ReturnBook.Click
Borrow = 0
MoveBooks(dgd_User_Borrow_View, BooksView, Borrow)
dgd_UserBookView.Sort(dgd_UserBookView.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
BooksSource.MoveLast()
BooksSource.MoveLast()
End Sub
Private Sub btn_UserLogOut_Click(sender As System.Object, e As System.EventArgs) Handles btn_UserLogOut.Click
frm_Login.Show()
Me.Hide()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles tmr_Timer.Tick
Dim countBooks As Integer
Dim countLent As Integer
countBooks = BooksSource.Count
countLent = BorrowSource.Count
lbl_BookCounter.Text = "There are " + countBooks.ToString + " books"
lbl_BorrowCounter.Text = "You have lent " + countLent.ToString + " books"
End Sub
End Class

How to get online date in vb.net?

How to get online date in vb.net ? is this possible ?
i use this code but it gives error like this (The remote server
returned an error: (503) Server Unavailable.) can anyone help ? thank you in advance
Imports System.ComponentModel
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Function getPageSource(ByVal URL As String) As String
Dim getWebClient As System.Net.WebClient = New System.Net.WebClient()
Dim strSource As String = getWebClient.DownloadString(URL)
getWebClient.Dispose()
Return strSource
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myPageSource, myStr, myStr1, strDate As String
Dim curLen, myStrPos, setStartPos As Integer
Dim dt As DateTime
txtPageSource.Text = getPageSource("http://www.worldtimeserver.com/current_time_in_PH.aspx")
myPageSource = txtPageSource.Text
'code to trim all the text till TIME start position
myStr = ""
myStrPos = myPageSource.IndexOf(myStr)
txtPageSource.Text = txtPageSource.Text.Remove(1, myStrPos + 23)
myPageSource = txtPageSource.Text
'code to trim all the text till after DATE
curLen = Len(txtPageSource.Text)
myStr1 = "2010"
setStartPos = myPageSource.IndexOf(myStr1)
txtPageSource.Text = txtPageSource.Text.Remove(setStartPos + 4, curLen - setStartPos - 4)
'code to trim "" and "" tags in between TIME and DATE
' 6:27 AM
'
'
'Wednesday, June 23, 2010
txtPageSource.Text = txtPageSource.Text.Replace("", "")
txtPageSource.Text = txtPageSource.Text.Replace("", "")
strDate = txtPageSource.Text
dt = CType((TypeDescriptor.GetConverter(New DateTime(2000, 1, 1)).ConvertFrom(strDate)), DateTime)
'txtPageSource.Text = dt
MsgBox(dt)
End Sub
End Class

Syntax error: Missing operand before 'And' operator

i am using vb.net
here is my code to filter bindingsource
i get this erro Syntax error: Missing operand before 'And' operator.
Private Function SetFilter() As String
Dim datee As String = String.Format("datee >= #{0:M/dd/yyyy}# AND datee <= #{1:M/dd/yyyy}#", _
DateTimePicker1.Value, _
DateTimePicker2.Value)
Dim client As String = If((TextBox1.Text.Length > 0), String.Format("[client] LIKE '%{0}%'", TextBox1.Text), "")
Dim ref As String = If((TextBox2.Text.Length > 0), String.Format("[REF] LIKE '%{0}%'", TextBox2.Text), "")
Return String.Format("{0} AND {1} AND {2}", datee, client, ref)
End Function
Private Sub DateTimePicker1_ValueChanged(sender As System.Object, e As System.EventArgs) Handles DateTimePicker1.ValueChanged
SalesBindingSource.Filter = SetFilter()
End Sub
Private Sub DateTimePicker2_ValueChanged(sender As System.Object, e As System.EventArgs) Handles DateTimePicker2.ValueChanged
'error here
SalesBindingSource.Filter = SetFilter()
End Sub
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
SalesBindingSource.Filter = SetFilter()
End Sub
Private Sub TextBox2_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox2.TextChanged
SalesBindingSource.Filter = SetFilter()
End Sub
I suggest to replace your filter creating method with a more secure way to handle the content of the two textboxes and the situation in which one or both textboxes are empty
Dim client As String = If((TextBox1.Text.Length > 0), _
String.Format(" AND [client] LIKE '%{0}%'", TextBox1.Text.Replace("'", "''")),"")
Dim ref As String = If((TextBox2.Text.Length > 0), _
String.Format(" AND [REF] LIKE '%{0}%'", TextBox2.Text.Replace("'", "''"), "")
Return String.Format("{0} {1} {2}", datee, client, ref)
The replace call double a single quote inserted by your user in the textbox, the AND is directly inserted in the string for client and for ref, otherwise you get an invalid sql if one or both textboxes are empty