Craig Gallagher
Craig Gallagher

Reputation: 1633

Finding the differences in 2 Textboxes Interop C#

I have 2 PowerPoint presentations one original and one edited and in each of them I have a TextBox.

The idea of this is to find any text that doesn't match. When I find text that doesn't match in the 2 TextBoxes I find the position of the edited word in the TextBoxin the edited presentation and add that word to the original presentation's textbox

e.g In My original presentation has a TextBox that contains "This is New" and in my edited presentation I have "Thisssss is Blaaa". When I run my code I get the following "This Thisssss is New Blaaa". This all works fine however when I have a TextBox in my original PowerPoint with the string "This This" and an edited TextBox saying "Thissss Blaa" I should get "This Thissss This Blaa" however I get "This Thissss Blaa" instead. It doesn't get the second "This" from the original TextBox for some reason. Any ideas how I could fix this?

var q = from original in originalList
        join editedTmp in editList on original.Id equals editedTmp.Id into g
        from edited in g.DefaultIfEmpty()
        select new
        {
            original,
            edited
        };

foreach (var item in q)
{ 
    var originalString = item.original.TextFrame.TextRange.Text;
    var editString = item.edited.TextFrame.TextRange.Text;
    var firstStringList = originalString.Split(delimiter).ToList();
    var secondStringList = editString.Split(delimiter).ToList();

    foreach (var word in firstStringList)
    {
        if (secondStringList.IndexOf(word) == -1)
        {
            var indexOfWord = firstStringList.IndexOf(word); //gets the position of the edited word eg. 3.
            // using indexOfWord+indexOfWord as I need do this if more than one word is added.
            secondStringList.Insert(indexOfWord + indexOfWord, word);
            // Insert the word that was not found at position 3 inside secondStringList
            one.Add(word);
        }
    }
    // Join the secondStringList to make 1 string separated by the space character
    item.edited.TextFrame.TextRange.Text = string.Join(" ", secondStringList);
}

Upvotes: 0

Views: 35

Answers (1)

Stephan Bauer
Stephan Bauer

Reputation: 9249

Instead of getting the 'current' index inside firstStringList via IndexOf, you can declare indexOfWord outside of the foreach loop and count up yourself so the variable holds the current index.

Otherwise firstStringList.IndexOf() will return the first occurence of the current string.

Furthermore, you can pass that index as second parameter to secondStringList.IndexOf() to ensure that you don't get the index of an already inserted string.

int indexOfWord=0;
foreach (var word in firstStringList)
{
    if (secondStringList.IndexOf(word,indexOfWord) == -1)
    {
        // using indexOfWord+indexOfWord as I need do this if more than one word is added.
        secondStringList.Insert(indexOfWord + indexOfWord, word);
        // Insert the word that was not found at position 3 inside secondStringList
        one.Add(word);
    }
    indexOfWord++;
}

Not sure if that works in all cases but with the given example it worked fine for me.

Upvotes: 1

Related Questions