Is it possible to display a datetimepicker when a TextBox(MetroTextBox) is clicked? Well what I am trying to do is when I click the TextBox it will display a datetimepicker above the Textbox or below the textbox then If I select a certain date it will display the date in the textbox.
I have this as a basis, but I cant make it work.
Show DateTimePicker in Textbox
I've met my desired result by using MonthCalendar instead of DTP.
here's the code for future reference of other people in case they want a same output. Thanks, Peace!...
Private Sub NotificationV2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MonthCalendar1.BringToFront()
MonthCalendar1.Location = New Point(265, 160)
MonthCalendar1.Visible = False
End Sub
Private Sub MetroTextBox8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MetroTextBox8.Click
MonthCalendar1.Visible = Not MonthCalendar1.Visible
End Sub
Private Sub MonthCalendar1_DateSelected(ByVal sender As Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles MonthCalendar1.DateSelected
MonthCalendar1.Visible = False
MetroTextBox8.Text = e.Start.ToShortDateString
End Sub
Related
In my form named 'authors', I have two text fields both of them are for getting two different dates as input. If the user focus the text box it will display the calendar that I have placed in some other form. After selecting the date and clicking the select button, the selected date will be assigned to the text box.
This is the code that I have written:
authors - coding:
Private Sub DOFE_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DOFE.GotFocus
If DOFE.Text = "" Then
Calendar.ShowDialog()
Else
Me.DOFE.Select()
Me.DOFE.Focus()
End If
End Sub
calender - coding:
Private Sub B_SELECT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_SELECT.Click
Authors.DOFE.Text = Format(MonthCalendar1.SelectionRange.Start(), "dd/MM/yyyy")
Me.Close()
End Sub
Private Sub MonthCalendar1_DateChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles MonthCalendar1.DateChanged
Authors.DOFE.Text = Format(MonthCalendar1.SelectionRange.Start(), "dd/MM/yyyy")
End Sub
Now the problem is, When I trying to execute the following code:
Private Sub DOB_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DOB.GotFocus
If DOB.Text = "" Then
Calendar.ShowDialog()
Else
Me.DOB.Select()
Me.DOB.Focus()
End If
End Sub
I mean, this code is for assigning some other date to some other text field of same form.
It is possible by adding single line
**Authors.DOB.Text** = Format(MonthCalendar1.SelectionRange.Start(), "dd/MM/yyyy")
in calendar form but the reflection will happen to both text field.
Suggest me in this regard.
One way would be to use a Public variable to hold the date and then assign it after the ShowDialog method.
So in some module you would have
Public tempDate As Date
In your calendar code
Private Sub B_SELECT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B_SELECT.Click
tempDate = Format(MonthCalendar1.SelectionRange.Start(), "dd/MM/yyyy")
Me.Close()
End Sub
And then in your Text_Changed events
Private Sub DOB_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DOB.GotFocus
If DOB.Text = "" Then
tempDate = ""
Calendar.ShowDialog()
DOB.Text = tempDate
Else
Me.DOB.Select()
Me.DOB.Focus()
End If
End Sub
I am trying to pass a value of a textbox on frmMain to a textbox on frmDepartment. I have tried the following code which I thought would work but it dosen't. I ma a new user to VB and come from a php background where it would have been a simple task of setting a seesion. Can anyone help with this? If you need to see more code, please ask. many thanks
txtDeptCustomer.Text = frmMain.txtCustomerActive.Text
In frmMain, I am getting value like this:
Dim value As Object = UserDataGridView.Rows(e.RowIndex).Cells(0).Value
txtCustomerActive.Text = CType(value, String)
Private Sub btnDepts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDepts.Click
frmDepartment.Show()
End Sub
Which is showing in frmMain ok.
In frmDepartment I have this code
Private Sub txtDeptCustomer_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDeptCustomer.TextChanged
'Dim customer As String
txtDeptCustomer.Text = frmMain.txtCustomerActive.Text
End Sub
instead of putting the code within the txtDeptCustomer.TextChanged sub, try putting it within the frmDepartment_load:
Private Sub frmDepartment_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
txtDeptCustomer.Text = frmMain.txtCustomerActive.Text
End Sub
or you could set the frmDepartment text box text on the frmMain button click:
Private Sub btnDepts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDepts.Click
frmDepartment.txtDeptCustomer.Text = txtCustomerActive.Text
frmDepartment.Show()
End Sub
I have 2 text box in one vb form. if txtMaterial was fill i want to disable the txtPackage and vice versa. I use the code below, but it didn't work. could someone fixed it.
Really appreciate it. tq.
Private Sub txtMaterial_TextChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMaterial.TextChanged
txtMaterial.Enabled = True
txtPackage.Enabled = False
End Sub
Private Sub txtPackage_TextChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPackage.TextChanged
txtPackage.Enabled = True
txtMaterial.Enabled = False
End Sub
Make both TextBoxes fire the same handler, then simply set the Enabled() state of each one based on whether the other TextBox has something in it:
Private Sub txtChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMaterial.TextChanged, txtPackage.TextChanged
txtMaterial.Enabled = (txtPackage.TextLength = 0)
txtPackage.Enabled = (txtMaterial.TextLength = 0)
End Sub
*Note the ending of the first line has both controls listed using Handles txtMaterial.TextChanged, txtPackage.TextChanged at the end.
Private Sub txtMaterial_TextChange(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMaterial.TextChanged
If txtMaterial.Text <> "" Then
txtPackage.Enabled =False
End If
End Sub
based on the comment that you added
Blockquote i want to make it disable as the second one. but in my case if i fill the txtMaterial the txtPackage also can be fill with text at the same time.
the solution to your problem must be this:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtPackage.Enabled = False
End Sub
Private Sub txtMaterial_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMaterial.TextChanged
If Not String.IsNullOrEmpty(txtMaterial.Text) Then
txtPackage.Enabled = True
Else
txtPackage.Enabled = False
End If
End Sub
Using VB.Net (Windows Application)
In the form (called as FirstForm), i am using textbox, add-form Button, search button.
When i click the add-form button, it will give the new form (same as FirstForm)
Code for adding new form
Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
Dim SecondForm As New FirstForm
SecondForm.Show()
End Sub
Search Button Code
Private Sub Search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Search.Click
If FirstForm.Focus = True Then
FirstForm.textbox1.Text = gridview1.Rows(crRow).Cells("code").Value.ToString().Trim()
Else
Dim SecondForm As New FirstForm
SecondForm.textbox1.Text = gridview1.Rows(crRow).Cells("code").Value.ToString().Trim()
End If
End Sub
The above code is working, but If i am in second Form when i click the search button and selected the value, then the value is appearing in the FirstForm textbox, it is not appearing in the SecondForm textbox.
If SecondForm is showing, the selected Value should appear in the SecondForm textbox not in the FirstForm Textbox.
How to solve this issue.
Need Vb.net code Help
Use Me - Reference variable which hold ref. of current form.
Dim frm As FirstForm
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If IsNothing(frm) OrElse frm.IsDisposed Then
frm = New FirstForm
End If
frm.Show()
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Me.textbox1.Text = gridview1.Rows(crRow).Cells("code").Value.ToString().Trim()
End Sub
Using "me" will not solve the problem?? why are you referring to the form in a static way?
Private Sub Search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Search.Click
textbox1.Text = gridview1.Rows(crRow).Cells("code").Value.ToString().Trim()
End Sub
I have multiple textbox in a form. How do I know what textbox the cursor currently is?
Trying to do something like this:
If TextBox2.Focus() = True Then
MessageBox.Show("its in two")
ElseIf TextBox3.Focus = True Then
MessageBox.Show("its in three")
End If
But I think its not working.
TextBox.Focus actually assigns the focus to the given textbox. What you're looking for is TextBox.Focused. :)
In fact, all form controls have the Focused property.
I know this already has an accepted answer but I just think this method is a bit easier and should be up here for people who find this through Google or whatever.
Public focussedTextBox As TextBox
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each control As Control In Me.Controls
If control.GetType.Equals(GetType(TextBox)) Then
Dim textBox As TextBox = control
AddHandler textBox.Enter, Sub() focussedTextBox = textBox
End If
Next
End Sub
This way you can then just refer to the focussedTextBox at any time. You should make sure that you check that there is a focussedTextBox before you do however becuase when the application first loads there will not be. You can do this using:
If Not focussedTextBox Is Nothing Then
...
End If
Alternatively, you could set focussedTextBox to a TextBox of your choice on form load, either by setting its value or by focussing the TextBox.
Obviously, it will not work if you are calling your code in a Button_Click because when you click the Button then the focus is itself goes to the Button which you have clicked.
You can do two things:
Make a combined Focus event for all TextBoxes and check its Sender object.
Private Sub TextBox_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.Enter, TextBox3.Enter
Dim currTextBox As TextBox = sender
If currTextBox.Equals(TextBox2) Then
MessageBox.Show("it's in two")
ElseIf currTextBox.Equals(TextBox3) Then
MessageBox.Show("it's in three")
End If
End Sub
OR
Take a global string variable, and set its value at each TextBox_Focus event, then check string value in the button click event.
Dim str As String
Private Sub TextBox2_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.Enter
str = "two"
End Sub
Private Sub TextBox3_Focus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.Enter
str = "three"
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("it's in " & str)
End Sub