Cong Bang
Cong Bang

Reputation: 11

Winforms combobox set default selected value not working

I am using Bunifu UI winforms, I am trying to set the default value for the combobox but my code is not working, I tried setting it with the selectedIndex property but nothing is happening. Can someone help me? Thanks!

ReadOnlyCollection<TimeZoneInfo> timeZones = TimeZoneInfo.GetSystemTimeZones();
        TimeZoneInfo currentTimeZone = TimeZoneInfo.FindSystemTimeZoneById(TimeZone.CurrentTimeZone.StandardName);
        Dictionary<int, string> dic = new Dictionary<int, string>();
        dic.Add(0, "Choose Timezone");
        foreach (var item in timeZones)
        {
            int time = (item.BaseUtcOffset.Hours * 60 + item.BaseUtcOffset.Minutes) * 60;
            if (!dic.ContainsKey(time))
            {
                dic.Add(time, (time == 0) ? item.DisplayName.Substring(0, 5) : item.DisplayName.Substring(0, 11));
            }
        }

        TimeZoneDropdown.DataSource = dic.ToList();
        TimeZoneDropdown.DisplayMember = "Value";
        TimeZoneDropdown.ValueMember = "Key";
        TimeZoneDropdown.SelectedValue = (!String.IsNullOrEmpty(Configs.GetValue("TimeZone"))) ? Convert.ToInt32(Configs.GetValue("TimeZone")) : (currentTimeZone.BaseUtcOffset.Hours * 60 + currentTimeZone.BaseUtcOffset.Minutes) * 60;

Upvotes: 0

Views: 227

Answers (0)

Related Questions