locked
Find2.FindReplace works only correct the first time RRS feed

  • Question

  • I have an AddIn, which makes numerous SearchReplace calls. The first call works as expected the others do not.

    Find2 find = DTE.Find as Find2;
    find.WaitForFindToComplete = true;
    find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral;
    
    vsFindTarget target = vsFindTarget.vsFindTargetCurrentDocument;
    int intFindOptions = (int)vsFindOptions.vsFindOptionsFromStart + (int)vsFindOptions.vsFindOptionsMatchCase + (int) vsFindOptions.vsFindOptionsKeepModifiedDocumentsOpen;
    
    //working
    result = find.FindReplace(vsFindAction.vsFindActionReplaceAll, "=SL.", intFindOptions, "=WPF.", target, String.Empty, "*.xaml", vsFindResultsLocation.vsFindResults1);
    
    // not working
    result = find.FindReplace(vsFindAction.vsFindActionReplaceAll, @"xmlns:data=""clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data""", intFindOptions, String.Empty, target, String.Empty, "*.xaml", vsFindResultsLocation.vsFindResults1);
    
    
    Can please somebody help.
    

    Michael Schwabl
    Monday, March 7, 2011 10:04 AM

Answers

  • Is the XAML document still active after the find or has it activated the Find Results window?

    Ryan

    • Marked as answer by emikelsoft Tuesday, March 8, 2011 8:09 AM
    Tuesday, March 8, 2011 7:54 AM

All replies

  • Hi Michael,

     

    Thanks for your post.

    I'm not aware of your situation from your description.

    In the title you mentioned as "the first time is working", do you mean the first time when you execute

    //working
    result = find.FindReplace(vsFindAction.vsFindActionReplaceAll,
    "=SL.", intFindOptions, "=WPF.", target, String.Empty, "*.xaml", vsFindResultsLocation.vsFindResults1);

    it is working, but later(try it again) it doesn't

    Or do you mean regardless of what the order of the following two method

    //working
    result = find.FindReplace(vsFindAction.vsFindActionReplaceAll,
    "=SL.", intFindOptions, "=WPF.", target, String.Empty, "*.xaml", vsFindResultsLocation.vsFindResults1);

    and

    // not working
    result = find.FindReplace(vsFindAction.vsFindActionReplaceAll,
    @"xmlns:data=""clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data""", intFindOptions, String.Empty, target, String.Empty, "*.xaml", vsFindResultsLocation.vsFindResults1);

    Only the first one is working.

    Looking forward to your reply.

     

     

    Best Regards,

    Ziwei Chen

     

    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg @ microsoft.com


    Ziwei Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 8, 2011 3:11 AM
  • I mean the sort order of the statements.

    The problem occurs only if we specify  vsFindTarget.vsFindTargetCurrentDocument, for vsFindTarget.vsFindTargetCurrentProject it works as expected.

    I think it has something to do with the searchstring literal.

    Here is my code for the Visual Studio AddIn:

    public void ReplaceXaml()
    {
    
    	Find2 find = DTE.Find as Find2;
    	find.WaitForFindToComplete = true;
    	find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral;
    
    	vsFindTarget target = vsFindTarget.vsFindTargetCurrentDocument;
    	int intFindOptions = (int)vsFindOptions.vsFindOptionsFromStart + (int)vsFindOptions.vsFindOptionsMatchCase + (int) vsFindOptions.vsFindOptionsKeepModifiedDocumentsOpen;
    
    	if (cboLookIn.SelectedIndex == 0)
    	{
    		target = vsFindTarget.vsFindTargetCurrentDocument;
    	}
    	else if (cboLookIn.SelectedIndex == 1)
    	{
    		target = vsFindTarget.vsFindTargetCurrentProject;
    		intFindOptions += (int)vsFindOptions.vsFindOptionsSearchSubfolders;
    	}
    
    
    	vsFindResult result;
    
    
    	Document doc = Project.DTE.ActiveDocument;
    
    
    	result = find.FindReplace(vsFindAction.vsFindActionReplaceAll, @"xmlns:basics=""clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls""", intFindOptions, String.Empty, target, String.Empty, "*.xaml", vsFindResultsLocation.vsFindResults1);
    	Console.WriteLine(result);
    
    	result = find.FindReplace(vsFindAction.vsFindActionReplaceAll, @"xmlns:data=""clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data""", intFindOptions, String.Empty, target, String.Empty, "*.xaml", vsFindResultsLocation.vsFindResults1);
    	Console.WriteLine(result);
    
    	result = find.FindReplace(vsFindAction.vsFindActionReplaceAll, "=SL.", intFindOptions, "=WPF.", target, "", "*.xaml", vsFindResultsLocation.vsFindResults1);
    	Console.WriteLine(result);
    
    	result = find.FindReplace(vsFindAction.vsFindActionReplaceAll, @"xmlns:msc=""clr-namespace:Microsoft.Windows.Controls;assembly=Microsoft.Windows.Controls""", intFindOptions, String.Empty, target, String.Empty, "*.xaml", vsFindResultsLocation.vsFindResults1);
    	Console.WriteLine(result);
    																	
    
    	// beware of <basedata:bla />
    	result = find.FindReplace(vsFindAction.vsFindActionReplaceAll, @"<data:", intFindOptions, @"<", target, String.Empty, "*.xaml", vsFindResultsLocation.vsFindResults1);
    	Console.WriteLine(result);
    
    	result = find.FindReplace(vsFindAction.vsFindActionReplaceAll, @"</data:", intFindOptions, @"</", target, String.Empty, "*.xaml", vsFindResultsLocation.vsFindResults1);
    	Console.WriteLine(result);
    
    
    	result = find.FindReplace(vsFindAction.vsFindActionReplaceAll, @"basics:", intFindOptions, String.Empty, target, String.Empty, "*.xaml", vsFindResultsLocation.vsFindResults1);
    	Console.WriteLine(result);
    
    	result = find.FindReplace(vsFindAction.vsFindActionReplaceAll, @"msc:", intFindOptions, String.Empty, target, String.Empty, "*.xaml", vsFindResultsLocation.vsFindResults1);
    	Console.WriteLine(result);
    			
    	// replace HyperlinkButton (WPF does not support HyperlinkButton -> we created our own.
    	//
    	result = find.FindReplace(vsFindAction.vsFindActionReplaceAll, @"<HyperlinkButton", intFindOptions, @"<corec:HyperlinkButton", target, String.Empty, "*.xaml", vsFindResultsLocation.vsFindResults1);
    	Console.WriteLine(result);
    }
    

    Here is the Xaml in the ActiveDocument, which we want to replace:

    <UserControl x:Class="Core.Basedata.Gui.DeviceTypeList"
    	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    	xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"   
    	xmlns:corec="clr-namespace:Core.Controls;assembly=SL.Core.Controls"
    	xmlns:corecommon="clr-namespace:Core.Common;assembly=SL.Core.Common"
    	xmlns:resc="clr-namespace:Core.Resource;assembly=SL.Core.Resource"
      xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" 
    	xmlns:vmbase="clr-namespace:Core.ViewModelBase;assembly=SL.Core.ViewModelBase">
    
    	<UserControl.Resources>
    		<resc:DeviceType x:Key="rescDeviceType" />
    		<resc:SystemConfiguration x:Key="rescSystemConfiguration" />
    	</UserControl.Resources>
    
    
    	<data:DataGrid x:Name="Grid" 
    				  IsReadOnly="False" 
    				  AutoGenerateColumns="False" 
    				  DataContext="{Binding}"
    				  ItemsSource="{Binding DeviceTypeList}" 
    				  SelectionMode="Single"
    				  VerticalAlignment="Stretch"
    				  HorizontalAlignment="Stretch"
    				  Style="{StaticResource CheckBoxDataGridStyle}">
    		<data:DataGrid.Columns>
    			<data:DataGridTemplateColumn 
    								Header="rescSystemConfiguration.Field_IsEnabled"
    								SortMemberPath="IsEnabled">
    				<data:DataGridTemplateColumn.CellTemplate>
    					<DataTemplate>
    						<CheckBox IsChecked="{Binding IsEnabled, Mode=TwoWay}" Style="{StaticResource GridCheckBoxStyle}" />
    					</DataTemplate>
    				</data:DataGridTemplateColumn.CellTemplate>
    			</data:DataGridTemplateColumn>
    
    			<data:DataGridTextColumn IsReadOnly="True" Header="rescDeviceType.Field_Name" Binding="{Binding Name}"/>
    		</data:DataGrid.Columns>
    
    	</data:DataGrid>
    
    </UserControl>
    

     

     

     

     

     

     

    • Edited by emikelsoft Tuesday, March 8, 2011 6:38 AM typo
    Tuesday, March 8, 2011 6:37 AM
  • Is the XAML document still active after the find or has it activated the Find Results window?

    Ryan

    • Marked as answer by emikelsoft Tuesday, March 8, 2011 8:09 AM
    Tuesday, March 8, 2011 7:54 AM
  • This was it, the find results window gets activated. So the solution is to call it with vsFindResultsLocation.vsFindResultsNone.
    With this option the XAML document has the focus.

    thx a lot for your help.

     

    br, Michael


    Michael Schwabl
    Tuesday, March 8, 2011 8:09 AM
  • Thanks Ryan, I didn't realize that situation.


    Ziwei Chen [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, March 8, 2011 8:34 AM