none
How to get the value of an array based on index RRS feed

  • Question

  • Hi,

    I'm working on outlook add in using Vs 2010 and c#. I've got an array of string like this:

    string [] arrText= new string [] {"Button 1", "Button1Path", "Button 2", "Button2Path", "Button 3", "Button3Path"...};

    string [] ButtonNamePath = new [] {""};

    I've 3 buttons in my custom ribbon: Button1, Button2, Button3

    When I click on a button I would like to store its path on a string Path;

    I tried something like that:

    string Path = "";

     foreach (string sOutput in arrText)
                {
                    ButtonNamePath[i] = sOutput;
                    int posButton = ButtonNamePath[i].IndexOf(sOutput);
                    if (control.Id == ButtonNamePath[i])
                    {
                        posButton++;

                        Path= ...  ;

                      MessageBox.Show(posButton.ToString());

                    }

                }

    The problem is with this code, when I click on all buttons, it always displays "1"(only Button1s 'path)?

    What should I do? and how to store the next value of an array in a string? Need help

    Thanks

    Wednesday, September 11, 2013 1:31 PM

Answers

  • Are you using unique Id properties for each button?

    You could just set the tag or description properties of the buttons to whatever you want when you create the custom ribbon UI, as an alternative.


    Ken Slovak MVP - Outlook

    • Marked as answer by suki_11 Wednesday, September 11, 2013 3:19 PM
    Wednesday, September 11, 2013 2:39 PM
  • I finally found the solution, putting xmlToAdd and element.Add(XElement.Parse(xmlToAdd)); after

    arrText.Add(ButtonName);
    • Marked as answer by suki_11 Friday, September 13, 2013 3:28 PM
    Friday, September 13, 2013 3:28 PM

All replies

  • Are you using unique Id properties for each button?

    You could just set the tag or description properties of the buttons to whatever you want when you create the custom ribbon UI, as an alternative.


    Ken Slovak MVP - Outlook

    • Marked as answer by suki_11 Wednesday, September 11, 2013 3:19 PM
    Wednesday, September 11, 2013 2:39 PM
  • Thanks for the answer, yes all my buttons have Id. how and why do you use tag?
    Wednesday, September 11, 2013 3:04 PM
  • Thanks for your advice, it's easier for me using tag.
    Wednesday, September 11, 2013 3:18 PM
  • As far as the original code, is it complete? I don't see "i" being initialized and it would have only 1 value in the loop as it's never incremented. I'd probably use something like this:

     int posButton = ButtonNamePath[i].IndexOf(sOutput);
     if (control.Id == ButtonNamePath[posButton])

    That should go into the if block for a match on the id.

    Tag is just a text property that you can use for any purpose, for example for what you're doing.


    Ken Slovak MVP - Outlook

    Wednesday, September 11, 2013 3:34 PM
  • Hi,

    Does somebody know where is the mistake in this code? let me explain:

    1. I want to read an xml file with streamReader

    2. Foreach value in this file, I want to add child node in the xml

    3.  I write the new xml file with FileStream

    in Param.Txt, I've :

    Button1 | ButtonPath1

    Button2 | ButtonPath2 ...

    the problem with this code is I could create only one child node "button2"

    This is the code:

    string username = Environment.UserName;
                string ButtonName = "";
                string ButtonPath = "";
                var xmlToAdd = "";
                XmlDocument doc = new XmlDocument();
                ArrayList arrText = new ArrayList();
                StreamReader Param = null;
                string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                 "<customUI onLoad=\"Ribbon_Load\" >" +
                     "<ribbon>" +
                         "<tabs>" +
                             "<tab idMso=\"TabAddIns\">" +
                                 "<group id=\"Messages\" label=\"Messages\">" +
                                     "<button id=\"button1\" label=\"button1\" showImage=\"false\" />" +
                                     "<button id=\"button2\" label=\"button2\" showImage=\"false\" />" +
                                 "</group>" +
                                 "<group id=\"group1\" label=\"Category\">" +
                                     "<menu id=\"menu1\" label=\"menu1\" showImage=\"false\" />" +
                                 "</group>" +
                             "</tab>" +
                         "</tabs>" +
                     "</ribbon>" +
                 "</customUI>";

                var xdoc = XDocument.Parse(xml);
                var element = xdoc.Descendants("menu").FirstOrDefault(x => x.Attribute("id").Value == "menu1");
           
                try
                {
                    StreamReader objReader = new StreamReader("Path for getting the param text", Encoding.GetEncoding("iso-8859-1"));
                    Param = objReader;
                }
                catch (SystemException ex)
                {
                    Console.WriteLine(ex.Message);
                }

                if (Param != null)
                {
                    while (Param.Peek() >= 0)
                    {
                        string str;
                        string[] strArray;
                        str = Param.ReadLine();
                        strArray = str.Split('|');
                        ButtonName = strArray[0];
                        ButtonPath = strArray[1];
                        arrText.Add(ButtonName);
                    }

                    Param.Close();
                }
                xmlToAdd = "<button id=\"" + ButtonName + "\" label=\"" + ButtonName + "\" tag=\"" + ButtonPath + "\" showImage=\"false\" />";
                
                  element.Add(XElement.Parse(xmlToAdd));
                  xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
                   "<customUI onLoad=\"Ribbon_Load\" >" +
                       "<ribbon>" +
                           "<tabs>" +
                               "<tab idMso=\"TabAddIns\">" +
                                   "<group id=\"Messages\" label=\"Messages\">" +
                                       "<button id=\"button1\" label=\"button1\" showImage=\"false\" />" +
                                       "<button id=\"button2\" label=\"button2\" showImage=\"false\" />" +
                                   "</group>" +
                                   "<group id=\"group1\" label=\"Category\">" +
                                     element  +
                                   "</group>" +
                               "</tab>" +
                           "</tabs>" +
                       "</ribbon>" +
                   "</customUI>";

                  FileStream file = new FileStream(@"Path whre you want to write the xmll file", FileMode.OpenOrCreate);
                  StreamWriter writer = new StreamWriter(file);
                  writer.WriteLine(xml);
                  writer.Close();

    Thanks for help

    Thursday, September 12, 2013 12:46 PM
  • I finally found the solution, putting xmlToAdd and element.Add(XElement.Parse(xmlToAdd)); after

    arrText.Add(ButtonName);
    • Marked as answer by suki_11 Friday, September 13, 2013 3:28 PM
    Friday, September 13, 2013 3:28 PM