-
Apr 28th, 2016, 03:38 AM
#1
Thread Starter
Addicted Member
Date format in bound textbox
I show dates in a bound textbox using data bindings.
Data is displayed with date and time. How can I change the format to show date or time only?
-
Apr 28th, 2016, 05:25 AM
#2
Thread Starter
Addicted Member
Re: Date format in bound textbox
Meanwhile I could resolve the problem using the masked text box instead of the text box.
-
Apr 28th, 2016, 06:26 AM
#3
Re: Date format in bound textbox
An even better solution would have been to use the DateTimePicker control.
-tg
-
Apr 28th, 2016, 06:37 AM
#4
Thread Starter
Addicted Member
Re: Date format in bound textbox
I think, you are right.
I used the DTP in the beginning but in this particular case, I did not want the user to accedently open the DTP and change the value. Only in certain cases the DTP should be used to change data. For this case I decided to use the method that has been published elsewhere in the forum.
I move a hidden DTP to the location of the date, make it visible and copy the selected date to the field. It works very nicely. In fact the DTP is placed at the right side of the datefield. This way it looks very similar to the Datefield in a grid in MS Access which only comes up when the field is selected.
-
Apr 28th, 2016, 10:09 AM
#5
Re: Date format in bound textbox
Your method of hiding the DateTimePicker seems more of a hack rather than a solution. If you do not want the user to edit the value then you'd toggle the Enabled property. Let's assume that you do not want the user to edit the value in the DateTimePicker when a ComboBox's SelectedIndex is anything except for 0, then you'd use the following code in the SelectedIndexChanged event:
Code:
MyDateTimePicker.Enabled = (MyComboBox.SelectedIndex = 0)
-
Apr 28th, 2016, 10:31 AM
#6
Re: Date format in bound textbox
To answer your original question, set the format on the binding. Something like this:
Code:
Dim bs As New BindingSource
bs.DataSource = {DateTime.Now(), DateTime.Now.AddDays(5)}
TextBox1.DataBindings.Add("Text", bs, Nothing, True, DataSourceUpdateMode.OnPropertyChanged, String.Empty, Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.ShortDatePattern)
-
Apr 28th, 2016, 10:41 AM
#7
Thread Starter
Addicted Member
Re: Date format in bound textbox
To dday9 and TnTinMN,
I am looking with great interest at your solution and realize that I still have a long way to go to be reasonably familiar with VB.
Would you please be so kind and explain the syntax to me.
thanks
-
Apr 28th, 2016, 10:52 AM
#8
Re: Date format in bound textbox
In terms of my suggestion, the Enabled property is a Boolean property that indicates whether or not the control is editable or not(similar to a TextBox's ReadOnly property). I set the Enabled property to whether or not the ComboBox's SelectedIndex is 0. It is a much more concise version of this:
Code:
If MyComboBox.SelectedIndex = 0 Then
MyDateTimePicker.Enabled = True
Else
MyDateTimePicker.Enabled = False
End If
In terms o f TnTinMN's suggestion, he is using a BindingSource which can be thought of as the "glue to hold the data/control relation" between your data source and control in which is being bound. He then set's the DataSource of the BindingSource equal to the current Date and Time as well as the 5 days from the current Date and Time. Finally he bind's the TextBox's Text to show just the Short Date pattern of the selected DateTime value.
-
Apr 28th, 2016, 11:16 AM
#9
Thread Starter
Addicted Member
Re: Date format in bound textbox
thanks for explanation.
How do I know that the brackets act as if-clause and MyComboBox.SelectedIndex will not be assigned 0? Is there some doc abound this kind of syntax.
For the second I think I need time to digest. I even couldn't ask a question to this.
-
Apr 28th, 2016, 11:23 AM
#10
Re: Date format in bound textbox
They do not act as an if-clause, I use them to improve readability. You could just as well have used:
Code:
MyDateTimePicker.Enabled = MyComboBox.SelectedIndex = 0
The point is that the Enabled property gets or sets a Boolean value and so I set it equal to the logical expression of if the SelectedIndex equals 0. This is one of the few times I prefer C# syntax because in C# you will explicitly know that we are doing a comparison and not an assignment:
Code:
//C# Syntax
MyDateTimePicker.Enabled = MyComboBox.SelectedIndex == 0;
-
Apr 28th, 2016, 12:13 PM
#11
Thread Starter
Addicted Member
Re: Date format in bound textbox
That means VB works from right to left. Any positive number will be converted to 1 (true) and 0 would be converted to 0 (false). This makes sense to me.
-
Apr 28th, 2016, 12:26 PM
#12
Re: Date format in bound textbox
I don't understand what you mean right-to-left.
The code assigns a property equal to the result of a comparison.
-
Apr 28th, 2016, 12:27 PM
#13
Re: Date format in bound textbox
I have no problem trying to provide a better explanation than you could find by looking at the documentation. However, I expect you to first make an attempt at reading the relevant information first and then making a concise yet precise explanation of that which is not understood. You can almost always be successful with a search engine query by prefacing the query with "msdn" followed by the search term.
It has become apparent from your posts that you really need instruction on the VB language itself. To that end, I recommend that you study the "Visual Basic Language Reference". You can also find similar documentation in your VB installation directory. It will be located at a path similar to:
"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VB\Specifications\1033\Visual Basic Language Specification.docx"
-
Apr 28th, 2016, 12:29 PM
#14
Re: Date format in bound textbox
Originally Posted by smktec
That means VB works from right to left. Any positive number will be converted to 1 (true) and 0 would be converted to 0 (false). This makes sense to me.
That's not true... False is defined as 0 ... True is defined as Not False ... which means ANY non-zero number evaluates to True... VB may interpret 1 as true, but to other languages True has a value of -1 ...
At any rate, 0 is always regard as false... and other value is regarded as True, doesn't matter it is positive or negative.
-tg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|