BunifuDropdown comes with a lot of flexibility in terms of UI and also the functionality,  to achieve the below designs you can head to the properties window and customize to the best you can. Judging from the title of this post I trust you are well familiar on how to add a control to toolbox if not please refer here. Let’s get started.

Adding items to BunifuDropdown can be implemented using the Form designer and also from code.

Adding from the designer

  • Select BunifuDropdown and Right click -> Select Properties -> Identify Items property

  • Click the .. button and enter your items as below.
  • Click okay to save and you are good to go.

Adding from code

BunifuDropdown accepts collections that includes Lists, Arrays, etc that means we can add items from any source as long as it is a collection.

  • Adding from and array

C#

 string[] items = { "Male", "Female", "Other" }; 
  foreach (var item in items) 
  { 
    bunifuDropdown1.Items.Add(item); 
  }

VB

Dim items As String() = {"Male", "Female", "Other"}
    For Each item In items
        bunifuDropdown1.Items.Add(item)
    Next
  • Adding from a List

C#

 IList<string> items = new List<string>();
 items.Add("Male");
 items.Add("Female");
 items.Add("Other");
   foreach (var item in items)
   {
      bunifuDropdown1.Items.Add(item); 
   }

VB

 Dim items As IList(Of String) = New List(Of String)()
    items.Add("L-Male")
    items.Add("L-Female")
    items.Add("L-Other")

    For Each item In items
        bunifuDropdown1.Items.Add(item)
    Next

Note : Instead of looping through the collection you can simply write:

//C# 
bunifuDropdown1.DataSource = items;

'VB
 bunifuDropdown1.DataSource = items
  • Adding from a Dictionary

C#

 SortedDictionary<string, string> userFullnames = new SortedDictionary<string, string>
 {
  {"John", "Doe"},
  {"Barrack", "Obama"},
  {"Amos", "Chepchieng"}
 };
 bunifuDropdown1.DataSource = new BindingSource(userFullnames, null);
 bunifuDropdown1.DisplayMember = "Key";
 bunifuDropdown1.ValueMember = "Value";

VB

Dim userFullnames As SortedDictionary(Of String, String) = New SortedDictionary(Of String, String) From {
    {"John", "Doe"},
    {"Barrack", "Obama"},
    {"Amos", "Chepchieng"}
}

bunifuDropdown1.DataSource = new BindingSource(userFullnames, null)
bunifuDropdown1.DisplayMember = "Key"
bunifuDropdown1.ValueMember = "Value"
  • Adding from a Model

C#

 private void addFromModel()
 {
            var students = new List<Student>
        {
            new Student() { FirstName="Student1FirstName", LastName="Student1LastName"},
            new Student() { FirstName="Student1FirstName", LastName="Student1LastName"},
            new Student() { FirstName="Student1FirstName", LastName="Student1LastName"}
        };

            foreach (Student student in students)
            {
                bunifuDropdown1.Items.Add(student.FirstName + "  " + student.LastName);

            }
        }


        //sample model class

        public class Student
        {
            public string FirstName { get; set; }
            public string LastName { get; set; }
        }

VB

  Private Sub addFromModel()
        Dim students = New List(Of Student) From {
            New Student() With {
                .FirstName = "Student1FirstName",
                .LastName = "Student1LastName"
            },
            New Student() With {
                .FirstName = "Student1FirstName",
                .LastName = "Student1LastName"
            },
            New Student() With {
                .FirstName = "Student1FirstName",
                .LastName = "Student1LastName"
            }
        }

        For Each student As Student In students
            bunifuDropdown1.Items.Add(student.FirstName & "  " & student.LastName)
        Next
    End Sub

    Public Class Student
        Public Property FirstName As String
        Public Property LastName As String
    End Class
End Class

Adding data from Mysql database

After knowing all the above now we can pull data from the database, below is the sample code

C#

 private void addFromDb(Bunifu.UI.WinForms.BunifuDropdown bunifuDropdown)
        {
            MySqlConnection con = new MySqlConnection("server=127.0.0.1;uid=root;password=;database=bunifu_tests;");
            con.Open();
            MySqlCommand cmd = new MySqlCommand("SELECT `gender` FROM `students` WHERE 1", con);
            MySqlDataReader read = cmd.ExecuteReader();
            while (read.Read())
            {
                bunifuDropdown.Items.Add(read.GetString(0));
            }
            con.Close();
        }

VB

Private Sub addFromDb(ByVal bunifuDropdown As Bunifu.UI.WinForms.BunifuDropdown)
    Dim con As MySqlConnection = New MySqlConnection("server=127.0.0.1;uid=root;password=;database=bunifu_tests;")
    con.Open()
    Dim cmd As MySqlCommand = New MySqlCommand("SELECT `gender` FROM `students` WHERE 1", con)
    Dim read As MySqlDataReader = cmd.ExecuteReader()

    While read.Read()
        bunifuDropdown.Items.Add(read.GetString(0))
    End While

    con.Close()
End Sub

That’s about it.

Please find the attached source code for reference

Source

Cheers!