0

I have my Combobox which I need to set from another class, so I am trying to set the value within my public property. This is what I have so far, however the combo box is not populating.

public string Title
{
    set 
    { 
        _Title = value;
        cmb_Title.Text = value;
    }
    get 
    { 
        return _Title; 
    }
}

I have also tried cmb_Title.selectedText = value, Index and all that i can think but I'm not to sure if its because I am setting it with in my property. Any ideas would be much appreciated.

Sach
  • 10,091
  • 8
  • 47
  • 84
Chaos
  • 633
  • 1
  • 6
  • 14
  • This is not databound? I believe you want a Value property for the combobox not Text? – Nikki9696 Aug 14 '17 at 18:47
  • I did try the cmb_Title.SelectedValue property but to no avail – Chaos Aug 14 '17 at 18:48
  • Sorry, it's been awhile for me for winforms. One moment. It's a weird thing where you select the item by text. – Nikki9696 Aug 14 '17 at 18:49
  • 1
    Does this help? Combox1.SelectedIndex = Combox1.FindStringExact("test1") https://stackoverflow.com/questions/450059/how-do-i-set-the-selected-item-in-a-combobox-to-match-my-string-using-c – Nikki9696 Aug 14 '17 at 18:49
  • Does your combo box already have a set of values assigned to it or is the value you want to set through the property is the only value that you want in it? Latter doesn't sound like a good use for a combo box. – Sach Aug 14 '17 at 18:54
  • No doesn't work unfortunately – Chaos Aug 14 '17 at 18:55
  • @dave.2 Silly question, but does the value actually already exist in the available items? How is the combobox being populated? – Nikki9696 Aug 14 '17 at 18:56
  • @Sach I already have 6 values stored inside it a but I also want to set the value through the property with out removing the existing values currently within – Chaos Aug 14 '17 at 18:57
  • OK so basically you want to 'add' a new item to your ComboBox through your property? – Sach Aug 14 '17 at 18:58
  • Yes @Nikki9696 the values already exists, I just want them to be set through another forms combo box, hence using the property. – Chaos Aug 14 '17 at 18:58
  • How does the other class have a reference to a form component? I'm wondering if it's a scope thing. – Nikki9696 Aug 14 '17 at 19:00

3 Answers3

0

You want to Add an item to your combo box as follows:

public string Title
{
    set 
    {  
        _Title = value;
        cmb_Title.Items.Add(_Title);
    }
    get 
    { 
        return _Title; 
    }
}

If you want the newly added item selected as well, you could do that as well. And I recommend using a new function.

private static void AddItemsToComboBox(ComboBox cmb, string value)
{
    cmb.Items.Add(value);
    cmb.SelectedValue = value;
}

Then call it in your setter:

AddItemsToComboBox(cmb, _Title);
Sach
  • 10,091
  • 8
  • 47
  • 84
  • I guess he do not want to add an item. He want to select an existing item and set it as new value of the combobox – Pavan Chandaka Aug 14 '17 at 19:03
  • @katamarayudu from his comment above what I gathered is that he already has items, and wants to add this as a new item. Agree OP is not clear but t his is what I gathered. – Sach Aug 14 '17 at 19:04
  • Yes I have the same values with in another form and I just want to pass them along to this once this has been created – Chaos Aug 14 '17 at 19:07
  • 1
    @dave.2 Like, you want all the same options available in this box as are in another box, and nothing has been populated yet? Or they are both populated and the user selects one from form1 and you want form2 to have the same item selected? – Nikki9696 Aug 14 '17 at 19:09
  • 1
    @dave.2 I'm with Nikki9696 here, your requirements are not very clear. Please be specific. – Sach Aug 14 '17 at 19:11
  • @Nikki9696 yes that is correct, they have both been populated and the user will select form1 and it needs to be passed to form2. Sorry for being vague – Chaos Aug 14 '17 at 19:12
  • @dave.2 I assume your form has a reference to the other form? I'd throw together a little example – Nikki9696 Aug 14 '17 at 19:14
  • yes form1 creates an instance of form 2, and all the data the passed through. `Form1 form1 = new Form1(); Form1.Title = "Value"; form1.ShowDialog();` – Chaos Aug 14 '17 at 19:16
  • Thats what I was trying in the other form – Chaos Aug 14 '17 at 19:19
  • What event in `form1` will trigger the addition? Do you want to add all items in `form1` combo to `form2` combo when the latter loads, or is it at the click of a button or something similar? If it's the former you could simply create a list of values based on `form1` combo box and pass it to `form2`, then populate your combo box. Otherwise my above example should work. – Sach Aug 14 '17 at 19:19
0

Please note that i pass form1 as owner so form2 can get access to property Title. Also, this is just an example. If a collection should be the same across forms, in real life I would bind to a collection in a DAL or business layer where they can be shared, not add items like this.

public partial class Form1 : Form
{
    private string _Title = "";

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        cmb_Title.Items.Add("Cat");
        cmb_Title.Items.Add("Dog");
        cmb_Title.Items.Add("Bear");
    }

    public string Title
    {
        set
        {
            _Title = value;
            cmb_Title.SelectedIndex = cmb_Title.FindStringExact(value);
        }
        get
        {
            return _Title;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        var form2 = new Form2();
        form2.Show(this);
    }
}



public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        cmb_Title2.Items.Add("Cat");
        cmb_Title2.Items.Add("Dog");
        cmb_Title2.Items.Add("Bear");

    }

    private void cmb_Title_SelectedIndexChanged(object sender, EventArgs e)
    {
        var f1 = this.Owner as Form1;
        f1.Title = cmb_Title2.Text;
    }
}
Nikki9696
  • 6,260
  • 1
  • 28
  • 23
  • Thanks all working now And your answer from before was working was just that I wasnt giving it the exact value using findstringexact – Chaos Aug 14 '17 at 19:29
0

Combox1.SelectedIndex = Combox1.FindStringExact("test1"); It is runing me