none
combobox select eg:1 to10 on a combebox to change the text on a button RRS feed

  • Question

  • I have 10 buttons (Button1 to Button10) and I would like to change the name on any of the buttons using a textbox to change the name of the button and a combobox to determine which button to pass the text to.

    I can send the text to a button but would like a combobox to choose which button to send the text to.

    any help would be appreciated


    Friday, December 22, 2017 10:26 AM

Answers

  • Hi Micheal,

    I restarted from scratch and it now works I am not sure why the original one I did came up with an error as it is identical to the one I just did other than the name. ?

    Thanks for your help I spent many nights trying to work this out, I have a greater understanding.

    • Marked as answer by vautek Saturday, December 23, 2017 9:36 AM
    Saturday, December 23, 2017 5:21 AM

All replies

  • To get the button's you can use Controls.Find(name, true) to find a specific button. To get all buttons use Controls.OfType<Button>, assuming you have no other buttons. If your buttons are in group then only look in that group's Controls property. Alternatively just hard code the button names in your combo box and then use Find to find them at runtime. Of course the user won't understand the buttons so your combo should store the current button text and button name. When the user selects an item then you use the name (SelectedValue) to get the button (using Controls.Find) and then change the Text property of the button and update the combo with the new value.

    Assuming:

    • Buttons reside in a child control (i.e. groupBox1)
    • Combo dynamically displays buttons and their text
    • Selecting an item from the combo populates the button's Text in textBox1
    • Clicking a save button (button5) sets the currently selected button's text to whatever is in textBox1 and updates the combo
    public partial class Form1 : Form
    {
        public Form1 ()
        {
            InitializeComponent();
        }
    
        protected override void OnLoad ( EventArgs e )
        {
            var buttons = groupBox1.Controls.OfType<Button>().ToList();
    
            //Bind combo to buttons
            comboBox1.DisplayMember = "Text";
            comboBox1.ValueMember = "Name";
            comboBox1.DataSource = buttons;
        }
    
        private void comboBox1_SelectedIndexChanged ( object sender, EventArgs e )
        {
            var combo = sender as ComboBox;
    
            //Get selected button's Text
            var button = combo.SelectedItem as Button;
            var buttonText = button?.Text;
    
            //Update textbox
            textBox1.Text = buttonText;
        }
    
        private void button5_Click ( object sender, EventArgs e )
        {
            var button = GetSelectedButton();
            if (button != null)
            {
                button.Text = textBox1.Text;
            };
        }
    
        private Button GetSelectedButton () => comboBox1.SelectedItem as Button;
    }


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, December 22, 2017 6:00 PM
    Moderator
  • Hi Michael, I have one error (OnLoad) underline in red in (protected override void OnLoad(EventArgs e)

    I have double checked what I have copied and can not find any mistyping form, in the .cs[Design] I have the 

    6x buttons  (button1, button2,button3 button4, button5, button6)

    1x combobox (comboBox1)

    1x textbox (textBox1)

    1x groupBox1 (all items are in the box)

     

     

    Saturday, December 23, 2017 3:36 AM
  • That would be odd. OnLoad is a protected method provided by the base class Form. Assuming this is a Winforms app and you haven't already defined it then it should be fine. What exact error are you getting? Does it compile and therefore it is just the editor out of sync?

    Michael Taylor http://www.michaeltaylorp3.net

    Saturday, December 23, 2017 3:45 AM
    Moderator
  • Hi Michael, I have added a picture also this the code I have underlined the error 

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;

    namespace button
    {
        public partial class a_delete_when_ready : Form
        {
            public a_delete_when_ready()
            {
                InitializeComponent();
            }
            protected override void OnLoad(EventArgs e)
            {
                var buttons = groupBox1.Controls.OfType<Button>().ToList();

                //Bind combo to buttons
                comboBox1.DisplayMember = "Text";
                comboBox1.ValueMember = "Name";
                comboBox1.DataSource = buttons;
            }

            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                var combo = sender as ComboBox;

                //Get selected button's Text
                var button = combo.SelectedItem as Button;
                var buttonText = button?.Text;

                //Update textbox
                textBox1.Text = buttonText;
            }
            protected override void OnLoad(EventArgs e)
            {

            }
            private void a_delete_when_ready_Load(object sender, EventArgs e)
            {

            }

            private void button5_Click(object sender, EventArgs e)
            {
                var button = GetSelectedButton();
                if (button != null)
                {
                    button.Text = textBox1.Text;
                };
            }

            private Button GetSelectedButton() => comboBox1.SelectedItem as Button;

            private void groupBox1_Enter(object sender, EventArgs e)
            {

            }

            
    Saturday, December 23, 2017 4:24 AM
  • Saturday, December 23, 2017 4:34 AM
  • Hi Micheal,

    I restarted from scratch and it now works I am not sure why the original one I did came up with an error as it is identical to the one I just did other than the name. ?

    Thanks for your help I spent many nights trying to work this out, I have a greater understanding.

    • Marked as answer by vautek Saturday, December 23, 2017 9:36 AM
    Saturday, December 23, 2017 5:21 AM