International dayNames
Printable View
International dayNames
Cool example although, it would have been a good idea to include comments in your code so users could know how to use the code and what it happening.
here it is with comments. HTH:
vb Code:
Imports System.Globalization Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'create new datatable Dim dt As New DataTable 'add 7 columns for dayNames() (0 to 6 = sunday to saturday) dt.Columns.Add("0") dt.Columns.Add("1") dt.Columns.Add("2") dt.Columns.Add("3") dt.Columns.Add("4") dt.Columns.Add("5") dt.Columns.Add("6") 'add a column for the EnglishName of the culture dt.Columns.Add("cultureName") 'for each individual culture For Each ci As CultureInfo In CultureInfo.GetCultures(CultureTypes.SpecificCultures) 'create a new list + add DayNames() + EnglishName Dim fields As New List(Of String)(New CultureInfo(ci.Name).DateTimeFormat.DayNames) fields.Add(ci.Parent.EnglishName) 'add a new row to the datatable containing the contents of the fields list Dim dr As DataRow = dt.NewRow dr.ItemArray = fields.ToArray dt.Rows.Add(dr) Next 'select distinct rows from the datatable dt = dt.DefaultView.ToTable(True, New String() {"cultureName", "0", "1", "2", "3", "4", "5", "6"}) 'remove unwanted rows from the datatable For x As Integer = dt.Rows.Count - 1 To 0 Step -1 If dt.Rows(x).Item("cultureName").ToString = "Invariant Language (Invariant Country)" Then dt.Rows.Remove(dt.Rows(x)) End If Next 'setup the databindings ComboBox1.DisplayMember = "cultureName" ComboBox1.DataSource = dt TextBox1.DataBindings.Add("Text", dt, "0") 'sunday TextBox2.DataBindings.Add("Text", dt, "1") 'monday TextBox3.DataBindings.Add("Text", dt, "2") 'tuesday TextBox4.DataBindings.Add("Text", dt, "3") 'wednesday TextBox5.DataBindings.Add("Text", dt, "4") 'thursday TextBox6.DataBindings.Add("Text", dt, "5") 'friday TextBox7.DataBindings.Add("Text", dt, "6") 'saturday End Sub
The only relevant bit is the CultureInfo.DateTimeFormat.DayNames property (line 22), which returns a string array with the names of the days in the language corresponding to that culture. Besides that, there is a loop through all cultures (line 20). The rest is just databinding to get the days into the textboxes.
By the way paul, thanks for this. I didn't know this information was available this easily. I'm using it for a month calendar control I'm creating, so now I can make it culture specific. The DateTimeFormat also specifies other things such as month names, abbreviated day and month names, first day of the week, etc. All very useful for a calendar control :)