How to Show text in combobox when no item selected like “..Select room…”












0















How can I show Text in ComboBox when no item selected in Windows Application using LINQ C#
Here is my code how I get all rooms.... in Combobox.



private void LoadRoom()
{

try
{
db = new HotelEntities();

// cmbProvince.Text = "";

var Room = (from u in db.Room

select new { u.RoomId, u.RoomNumber }).ToList();

cmbRoom.Text = ".. Select.."; // This one do not working.
cmbRoom.DisplayMember = "RoomNumber";
cmbRoom.ValueMember = "RoomId";
cmbRoom.DataSource = Room;

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}


Thank you!










share|improve this question





























    0















    How can I show Text in ComboBox when no item selected in Windows Application using LINQ C#
    Here is my code how I get all rooms.... in Combobox.



    private void LoadRoom()
    {

    try
    {
    db = new HotelEntities();

    // cmbProvince.Text = "";

    var Room = (from u in db.Room

    select new { u.RoomId, u.RoomNumber }).ToList();

    cmbRoom.Text = ".. Select.."; // This one do not working.
    cmbRoom.DisplayMember = "RoomNumber";
    cmbRoom.ValueMember = "RoomId";
    cmbRoom.DataSource = Room;

    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    }


    Thank you!










    share|improve this question



























      0












      0








      0








      How can I show Text in ComboBox when no item selected in Windows Application using LINQ C#
      Here is my code how I get all rooms.... in Combobox.



      private void LoadRoom()
      {

      try
      {
      db = new HotelEntities();

      // cmbProvince.Text = "";

      var Room = (from u in db.Room

      select new { u.RoomId, u.RoomNumber }).ToList();

      cmbRoom.Text = ".. Select.."; // This one do not working.
      cmbRoom.DisplayMember = "RoomNumber";
      cmbRoom.ValueMember = "RoomId";
      cmbRoom.DataSource = Room;

      }
      catch (Exception ex)
      {
      MessageBox.Show(ex.Message);
      }
      }


      Thank you!










      share|improve this question
















      How can I show Text in ComboBox when no item selected in Windows Application using LINQ C#
      Here is my code how I get all rooms.... in Combobox.



      private void LoadRoom()
      {

      try
      {
      db = new HotelEntities();

      // cmbProvince.Text = "";

      var Room = (from u in db.Room

      select new { u.RoomId, u.RoomNumber }).ToList();

      cmbRoom.Text = ".. Select.."; // This one do not working.
      cmbRoom.DisplayMember = "RoomNumber";
      cmbRoom.ValueMember = "RoomId";
      cmbRoom.DataSource = Room;

      }
      catch (Exception ex)
      {
      MessageBox.Show(ex.Message);
      }
      }


      Thank you!







      c# winforms linq combobox






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 26 '18 at 13:20









      Dennis

      29.9k760112




      29.9k760112










      asked Nov 26 '18 at 13:16









      Helen TekieHelen Tekie

      1401110




      1401110
























          2 Answers
          2






          active

          oldest

          votes


















          0














          If you set the DataSource of a combobox, a currencymanager is used on the background and its position is selected (the first item).



          Instead of setting DataSource, try adding the items:



          cmbRoom.Items.AddRange(Room);


          NB, setting Text as a placeholder will not work if an item is chosen and cleared later, unless you add an extra check (in TextChanged or SelectedIndexChanged)






          share|improve this answer

































            0














            Fast solution



            Create a source class for your ComboItems, with (at least) the properties to Display and the inner value of the property. If you create a generic class you can use it for all your combo boxes.



            class ComboDisplay<TSource>
            {
            public string Display {get; set;}
            public TSource Value {get; set;}
            }

            cmbRoom.DisplayMember = nameof(ComboDisplay.Display);
            cmbRoom.ValueMember = nameof(ComboDisplay.Value);


            When you create the data source for your combobox, make sure you add a default value. In the example below I assume that you want to select items of type Room in your combo:



            IEnumerable<Room> availableRooms = myDbContext.Rooms
            .Where(room => room.IsAvailable)
            .Select(room => new ComboDisplay<Room>
            {
            Display = room.Name,
            Value = new Room
            {
            Id = room.Id,
            ...
            },
            })
            // add a dummy value if nothing is selected
            .Concat(new Room
            {
            Display = "Please select a room",
            Value = null, // meaning: nothing selected
            });


            After selection, use comboBox1.SelectedValue to get the selected Room, or null if nothing is selected.



            Create a Special Combobox class



            If you have to use this regularly, consider creating a generic sub class of ComboBox that can display items of a certain TSource, and will return null if nothing is selected:



            class MyComboBox<TSource> : ComboBox
            {
            public MyComboBox() : base()
            {
            base.DataSource = this.EmptyList;
            base.DisplayMember = nameof(ComboDisplay.Display);
            base.ValueMember = nameof(ComboDisplay.Value);
            }

            private static readonly EmptyItem = new ComboDisplay
            {
            Display = "Please select a value",
            Value = null,
            }


            Make a property that returns the available combo items. Make sure that the EmptyItem is always in the collection:



            public IReadonlyCollection<TSource> ComboItems
            {
            get {return (IReadOnlyCollection<TSource>)base.DataSource;}
            set
            {
            // TODO: check if the empty element is in your list; if not add it
            base.DataSource = value;
            }
            }


            Finally: the function to get the Selected value, or null if nothing is selected:



            public TSource SelectedValue
            {
            get => return (TSource)base.SelectedValue;
            set
            {
            // TODO: check if value is in ComboItems
            base.SelectedValue = value;
            }
            }





            share|improve this answer
























            • This is too complicate for me..;) I'am new for this staffs. Is this all code just to have a text when there is no selected item?

              – Helen Tekie
              Nov 26 '18 at 15:05











            • No, it is a general solution, it works for all kinds of items you want in your comboboxes. Whether it is a combobox showing Persons, or whether it is a combobox showing countries. When creating a multi-usable solution, it is more work the first time, but the next time you only have to re-use it, not code again

              – Harald Coppoolse
              Nov 27 '18 at 7:20












            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53481960%2fhow-to-show-text-in-combobox-when-no-item-selected-like-select-room%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            If you set the DataSource of a combobox, a currencymanager is used on the background and its position is selected (the first item).



            Instead of setting DataSource, try adding the items:



            cmbRoom.Items.AddRange(Room);


            NB, setting Text as a placeholder will not work if an item is chosen and cleared later, unless you add an extra check (in TextChanged or SelectedIndexChanged)






            share|improve this answer






























              0














              If you set the DataSource of a combobox, a currencymanager is used on the background and its position is selected (the first item).



              Instead of setting DataSource, try adding the items:



              cmbRoom.Items.AddRange(Room);


              NB, setting Text as a placeholder will not work if an item is chosen and cleared later, unless you add an extra check (in TextChanged or SelectedIndexChanged)






              share|improve this answer




























                0












                0








                0







                If you set the DataSource of a combobox, a currencymanager is used on the background and its position is selected (the first item).



                Instead of setting DataSource, try adding the items:



                cmbRoom.Items.AddRange(Room);


                NB, setting Text as a placeholder will not work if an item is chosen and cleared later, unless you add an extra check (in TextChanged or SelectedIndexChanged)






                share|improve this answer















                If you set the DataSource of a combobox, a currencymanager is used on the background and its position is selected (the first item).



                Instead of setting DataSource, try adding the items:



                cmbRoom.Items.AddRange(Room);


                NB, setting Text as a placeholder will not work if an item is chosen and cleared later, unless you add an extra check (in TextChanged or SelectedIndexChanged)







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 26 '18 at 13:46

























                answered Nov 26 '18 at 13:36









                Me.NameMe.Name

                10.2k22039




                10.2k22039

























                    0














                    Fast solution



                    Create a source class for your ComboItems, with (at least) the properties to Display and the inner value of the property. If you create a generic class you can use it for all your combo boxes.



                    class ComboDisplay<TSource>
                    {
                    public string Display {get; set;}
                    public TSource Value {get; set;}
                    }

                    cmbRoom.DisplayMember = nameof(ComboDisplay.Display);
                    cmbRoom.ValueMember = nameof(ComboDisplay.Value);


                    When you create the data source for your combobox, make sure you add a default value. In the example below I assume that you want to select items of type Room in your combo:



                    IEnumerable<Room> availableRooms = myDbContext.Rooms
                    .Where(room => room.IsAvailable)
                    .Select(room => new ComboDisplay<Room>
                    {
                    Display = room.Name,
                    Value = new Room
                    {
                    Id = room.Id,
                    ...
                    },
                    })
                    // add a dummy value if nothing is selected
                    .Concat(new Room
                    {
                    Display = "Please select a room",
                    Value = null, // meaning: nothing selected
                    });


                    After selection, use comboBox1.SelectedValue to get the selected Room, or null if nothing is selected.



                    Create a Special Combobox class



                    If you have to use this regularly, consider creating a generic sub class of ComboBox that can display items of a certain TSource, and will return null if nothing is selected:



                    class MyComboBox<TSource> : ComboBox
                    {
                    public MyComboBox() : base()
                    {
                    base.DataSource = this.EmptyList;
                    base.DisplayMember = nameof(ComboDisplay.Display);
                    base.ValueMember = nameof(ComboDisplay.Value);
                    }

                    private static readonly EmptyItem = new ComboDisplay
                    {
                    Display = "Please select a value",
                    Value = null,
                    }


                    Make a property that returns the available combo items. Make sure that the EmptyItem is always in the collection:



                    public IReadonlyCollection<TSource> ComboItems
                    {
                    get {return (IReadOnlyCollection<TSource>)base.DataSource;}
                    set
                    {
                    // TODO: check if the empty element is in your list; if not add it
                    base.DataSource = value;
                    }
                    }


                    Finally: the function to get the Selected value, or null if nothing is selected:



                    public TSource SelectedValue
                    {
                    get => return (TSource)base.SelectedValue;
                    set
                    {
                    // TODO: check if value is in ComboItems
                    base.SelectedValue = value;
                    }
                    }





                    share|improve this answer
























                    • This is too complicate for me..;) I'am new for this staffs. Is this all code just to have a text when there is no selected item?

                      – Helen Tekie
                      Nov 26 '18 at 15:05











                    • No, it is a general solution, it works for all kinds of items you want in your comboboxes. Whether it is a combobox showing Persons, or whether it is a combobox showing countries. When creating a multi-usable solution, it is more work the first time, but the next time you only have to re-use it, not code again

                      – Harald Coppoolse
                      Nov 27 '18 at 7:20
















                    0














                    Fast solution



                    Create a source class for your ComboItems, with (at least) the properties to Display and the inner value of the property. If you create a generic class you can use it for all your combo boxes.



                    class ComboDisplay<TSource>
                    {
                    public string Display {get; set;}
                    public TSource Value {get; set;}
                    }

                    cmbRoom.DisplayMember = nameof(ComboDisplay.Display);
                    cmbRoom.ValueMember = nameof(ComboDisplay.Value);


                    When you create the data source for your combobox, make sure you add a default value. In the example below I assume that you want to select items of type Room in your combo:



                    IEnumerable<Room> availableRooms = myDbContext.Rooms
                    .Where(room => room.IsAvailable)
                    .Select(room => new ComboDisplay<Room>
                    {
                    Display = room.Name,
                    Value = new Room
                    {
                    Id = room.Id,
                    ...
                    },
                    })
                    // add a dummy value if nothing is selected
                    .Concat(new Room
                    {
                    Display = "Please select a room",
                    Value = null, // meaning: nothing selected
                    });


                    After selection, use comboBox1.SelectedValue to get the selected Room, or null if nothing is selected.



                    Create a Special Combobox class



                    If you have to use this regularly, consider creating a generic sub class of ComboBox that can display items of a certain TSource, and will return null if nothing is selected:



                    class MyComboBox<TSource> : ComboBox
                    {
                    public MyComboBox() : base()
                    {
                    base.DataSource = this.EmptyList;
                    base.DisplayMember = nameof(ComboDisplay.Display);
                    base.ValueMember = nameof(ComboDisplay.Value);
                    }

                    private static readonly EmptyItem = new ComboDisplay
                    {
                    Display = "Please select a value",
                    Value = null,
                    }


                    Make a property that returns the available combo items. Make sure that the EmptyItem is always in the collection:



                    public IReadonlyCollection<TSource> ComboItems
                    {
                    get {return (IReadOnlyCollection<TSource>)base.DataSource;}
                    set
                    {
                    // TODO: check if the empty element is in your list; if not add it
                    base.DataSource = value;
                    }
                    }


                    Finally: the function to get the Selected value, or null if nothing is selected:



                    public TSource SelectedValue
                    {
                    get => return (TSource)base.SelectedValue;
                    set
                    {
                    // TODO: check if value is in ComboItems
                    base.SelectedValue = value;
                    }
                    }





                    share|improve this answer
























                    • This is too complicate for me..;) I'am new for this staffs. Is this all code just to have a text when there is no selected item?

                      – Helen Tekie
                      Nov 26 '18 at 15:05











                    • No, it is a general solution, it works for all kinds of items you want in your comboboxes. Whether it is a combobox showing Persons, or whether it is a combobox showing countries. When creating a multi-usable solution, it is more work the first time, but the next time you only have to re-use it, not code again

                      – Harald Coppoolse
                      Nov 27 '18 at 7:20














                    0












                    0








                    0







                    Fast solution



                    Create a source class for your ComboItems, with (at least) the properties to Display and the inner value of the property. If you create a generic class you can use it for all your combo boxes.



                    class ComboDisplay<TSource>
                    {
                    public string Display {get; set;}
                    public TSource Value {get; set;}
                    }

                    cmbRoom.DisplayMember = nameof(ComboDisplay.Display);
                    cmbRoom.ValueMember = nameof(ComboDisplay.Value);


                    When you create the data source for your combobox, make sure you add a default value. In the example below I assume that you want to select items of type Room in your combo:



                    IEnumerable<Room> availableRooms = myDbContext.Rooms
                    .Where(room => room.IsAvailable)
                    .Select(room => new ComboDisplay<Room>
                    {
                    Display = room.Name,
                    Value = new Room
                    {
                    Id = room.Id,
                    ...
                    },
                    })
                    // add a dummy value if nothing is selected
                    .Concat(new Room
                    {
                    Display = "Please select a room",
                    Value = null, // meaning: nothing selected
                    });


                    After selection, use comboBox1.SelectedValue to get the selected Room, or null if nothing is selected.



                    Create a Special Combobox class



                    If you have to use this regularly, consider creating a generic sub class of ComboBox that can display items of a certain TSource, and will return null if nothing is selected:



                    class MyComboBox<TSource> : ComboBox
                    {
                    public MyComboBox() : base()
                    {
                    base.DataSource = this.EmptyList;
                    base.DisplayMember = nameof(ComboDisplay.Display);
                    base.ValueMember = nameof(ComboDisplay.Value);
                    }

                    private static readonly EmptyItem = new ComboDisplay
                    {
                    Display = "Please select a value",
                    Value = null,
                    }


                    Make a property that returns the available combo items. Make sure that the EmptyItem is always in the collection:



                    public IReadonlyCollection<TSource> ComboItems
                    {
                    get {return (IReadOnlyCollection<TSource>)base.DataSource;}
                    set
                    {
                    // TODO: check if the empty element is in your list; if not add it
                    base.DataSource = value;
                    }
                    }


                    Finally: the function to get the Selected value, or null if nothing is selected:



                    public TSource SelectedValue
                    {
                    get => return (TSource)base.SelectedValue;
                    set
                    {
                    // TODO: check if value is in ComboItems
                    base.SelectedValue = value;
                    }
                    }





                    share|improve this answer













                    Fast solution



                    Create a source class for your ComboItems, with (at least) the properties to Display and the inner value of the property. If you create a generic class you can use it for all your combo boxes.



                    class ComboDisplay<TSource>
                    {
                    public string Display {get; set;}
                    public TSource Value {get; set;}
                    }

                    cmbRoom.DisplayMember = nameof(ComboDisplay.Display);
                    cmbRoom.ValueMember = nameof(ComboDisplay.Value);


                    When you create the data source for your combobox, make sure you add a default value. In the example below I assume that you want to select items of type Room in your combo:



                    IEnumerable<Room> availableRooms = myDbContext.Rooms
                    .Where(room => room.IsAvailable)
                    .Select(room => new ComboDisplay<Room>
                    {
                    Display = room.Name,
                    Value = new Room
                    {
                    Id = room.Id,
                    ...
                    },
                    })
                    // add a dummy value if nothing is selected
                    .Concat(new Room
                    {
                    Display = "Please select a room",
                    Value = null, // meaning: nothing selected
                    });


                    After selection, use comboBox1.SelectedValue to get the selected Room, or null if nothing is selected.



                    Create a Special Combobox class



                    If you have to use this regularly, consider creating a generic sub class of ComboBox that can display items of a certain TSource, and will return null if nothing is selected:



                    class MyComboBox<TSource> : ComboBox
                    {
                    public MyComboBox() : base()
                    {
                    base.DataSource = this.EmptyList;
                    base.DisplayMember = nameof(ComboDisplay.Display);
                    base.ValueMember = nameof(ComboDisplay.Value);
                    }

                    private static readonly EmptyItem = new ComboDisplay
                    {
                    Display = "Please select a value",
                    Value = null,
                    }


                    Make a property that returns the available combo items. Make sure that the EmptyItem is always in the collection:



                    public IReadonlyCollection<TSource> ComboItems
                    {
                    get {return (IReadOnlyCollection<TSource>)base.DataSource;}
                    set
                    {
                    // TODO: check if the empty element is in your list; if not add it
                    base.DataSource = value;
                    }
                    }


                    Finally: the function to get the Selected value, or null if nothing is selected:



                    public TSource SelectedValue
                    {
                    get => return (TSource)base.SelectedValue;
                    set
                    {
                    // TODO: check if value is in ComboItems
                    base.SelectedValue = value;
                    }
                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 26 '18 at 14:27









                    Harald CoppoolseHarald Coppoolse

                    13.3k13064




                    13.3k13064













                    • This is too complicate for me..;) I'am new for this staffs. Is this all code just to have a text when there is no selected item?

                      – Helen Tekie
                      Nov 26 '18 at 15:05











                    • No, it is a general solution, it works for all kinds of items you want in your comboboxes. Whether it is a combobox showing Persons, or whether it is a combobox showing countries. When creating a multi-usable solution, it is more work the first time, but the next time you only have to re-use it, not code again

                      – Harald Coppoolse
                      Nov 27 '18 at 7:20



















                    • This is too complicate for me..;) I'am new for this staffs. Is this all code just to have a text when there is no selected item?

                      – Helen Tekie
                      Nov 26 '18 at 15:05











                    • No, it is a general solution, it works for all kinds of items you want in your comboboxes. Whether it is a combobox showing Persons, or whether it is a combobox showing countries. When creating a multi-usable solution, it is more work the first time, but the next time you only have to re-use it, not code again

                      – Harald Coppoolse
                      Nov 27 '18 at 7:20

















                    This is too complicate for me..;) I'am new for this staffs. Is this all code just to have a text when there is no selected item?

                    – Helen Tekie
                    Nov 26 '18 at 15:05





                    This is too complicate for me..;) I'am new for this staffs. Is this all code just to have a text when there is no selected item?

                    – Helen Tekie
                    Nov 26 '18 at 15:05













                    No, it is a general solution, it works for all kinds of items you want in your comboboxes. Whether it is a combobox showing Persons, or whether it is a combobox showing countries. When creating a multi-usable solution, it is more work the first time, but the next time you only have to re-use it, not code again

                    – Harald Coppoolse
                    Nov 27 '18 at 7:20





                    No, it is a general solution, it works for all kinds of items you want in your comboboxes. Whether it is a combobox showing Persons, or whether it is a combobox showing countries. When creating a multi-usable solution, it is more work the first time, but the next time you only have to re-use it, not code again

                    – Harald Coppoolse
                    Nov 27 '18 at 7:20


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53481960%2fhow-to-show-text-in-combobox-when-no-item-selected-like-select-room%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Costa Masnaga

                    Fotorealismo

                    Sidney Franklin