none
Outlook 2013 - Populating a textbox from the selection in a listbox RRS feed

  • Question

  • Dear All

    Apologies, if this is the wrong forum!!

    I am currently trying to develop a Outlook 2013 Add-In, which puts in a button to the ribbon, in a new email window. This button show/hides a CustomTask pane with two controls on it. One control is a list box, and the other a TextBox. The aim of this add in is for it to connect to a SharePoint list, which has a common set of Email Subjects, with some standard Email Body Texts.

    I have to functionality working which returns the ListItemCollection, and I can add the subjects to the ListBox. My challange now, is that I would like for the text in the TextBox to change to the corresponding Body text, when a different subject is selected in the ListBox.

    My code for the adding of items to the ListBox, looks as follows:

    private void WriteMailTexts()
            {
                lstMailSubject.Items.Clear();
                txtBodyText.Text = "";
    
                foreach (ListItem item in txtCollection)
                {
                    lstMailSubject.Items.Add(item.FieldValues["Title"]);
                }
            }

    I have the event handler ready for the selected index changed ready. I just do not know what to enter in here? Any and all suggestions and help will be greatly appreciated!!

    Kind Regards

    Tom

    Thursday, June 18, 2015 11:52 AM

Answers

  • Hello Tom,

    You need to handle the SelectedIndexChanged event of the ListBox class which is fired when the SelectedIndex property or the SelectedIndices collection has changed. Here is what MSDN states for the event:

    You can create an event handler for this event to determine when the selected index in the ListBox has been changed. This can be useful when you need to display information in other controls based on the current selection in the ListBox. You can use the event handler for this event to load the information in the other controls.

    If the SelectionMode property is set to SelectionMode.MultiSimple or SelectionMode.MultiExtended, any change to the SelectedIndices collection, including removing an item from the selection, will raise this event.

    private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
       // Get the currently selected item in the ListBox. 
       string curItem = listBox1.SelectedItem.ToString();
     
       textBox.Text = curItem;
    }

    Thursday, June 18, 2015 1:49 PM

All replies

  • Hello Tom,

    You need to handle the SelectedIndexChanged event of the ListBox class which is fired when the SelectedIndex property or the SelectedIndices collection has changed. Here is what MSDN states for the event:

    You can create an event handler for this event to determine when the selected index in the ListBox has been changed. This can be useful when you need to display information in other controls based on the current selection in the ListBox. You can use the event handler for this event to load the information in the other controls.

    If the SelectionMode property is set to SelectionMode.MultiSimple or SelectionMode.MultiExtended, any change to the SelectedIndices collection, including removing an item from the selection, will raise this event.

    private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
       // Get the currently selected item in the ListBox. 
       string curItem = listBox1.SelectedItem.ToString();
     
       textBox.Text = curItem;
    }

    Thursday, June 18, 2015 1:49 PM
  • Hello Fei

    This was exactly what I needed. Thank you!!

    Regards 

    Tom Madsen

    Saturday, June 20, 2015 11:33 AM
  • You are welcome, Tom!
    Saturday, June 20, 2015 1:56 PM
  • Hi Again :-)

    This was not exactly what I was looking for, and I can see from my original question, that I was unclear. Apologies!!

    When I connect to my custom SharePoint list, I get returned a ListItemCollection, from a CAML query. This collection contains two columns. Title and a field called OutlookText. What I want to do is, for the text in the TextBox, to change to the corresponding OutlookText field in the ListItemCollection.

    So the ListBox has a collection of the Title fields returned. When a Title is selected there, I want the text in the TextBox, to change to OutlookText field that corresponds to that Title.

    Regards

    Tom

    Sunday, June 21, 2015 12:42 PM
  • Dear All

    Never mind. I found the solution. I can do it using the following code:

    private void lstMailSubject_SelectedValueChanged(object sender, EventArgs e)
            {
                lstMailBody.Items.Clear();
    
                foreach (ListItem item in txtCollection)
                {
                    if (item.FieldValues["Title"].ToString() == lstMailSubject.SelectedItem.ToString())
                    {
                        lstMailBody.Items.Add(item.FieldValues["OutlookText"]);
                    }
                }
            }

    Regards

    Tom Madsen

    Monday, June 22, 2015 1:54 PM