Crash if press enter key after changing line and caret postiion to end of document

SyntaxEditor for WPF Forum

Posted 3 years ago by Mark
Version: 16.1.0633
Platform: .NET 4.5
Environment: Windows 10 (64-bit)
Avatar

Is there a fix or a workaround for this issue?

  1. Run test app
  2. Click the "Open" button to load a file
  3. Enter a number in the "Jump" text box which is higher than the total number of lines in the loaded file.
  4. Click the "Jump" button.
  5. Press enter
  6. Unhandled exception is thrown "ArgumentOutOfRangeException"

To reproduce this issue, please use the following code to create a simple test app:

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication2"
        xmlns:syntaxeditor="http://schemas.actiprosoftware.com/winfx/xaml/syntaxeditor"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        
        <syntaxeditor:SyntaxEditor Name="actiproEditor" IsLineNumberMarginVisible="True" IsOutliningMarginVisible="False" IsSelectionMarginVisible="False"
                FontFamily="Consolas" FontSize="16" TextOptions.TextFormattingMode="Display" IsDefaultContextMenuEnabled="False"
                Background="White" LineNumberMarginForeground="Gray" IsCurrentLineHighlightingEnabled="True">
        </syntaxeditor:SyntaxEditor>

        <StackPanel Grid.Row="1" Orientation="Horizontal">
            <Button Content="Open" Margin="0,0,5,0" Click="Open_OnClick"/>
            <Button Content="Jump" Margin="5,0" Click="Jump_OnClick"/>
            <TextBox Name="txtJump" Text="100"  Margin="5,0"/>
        </StackPanel>
    </Grid>
</Window>

 

using System.Windows;

namespace WpfApplication2
{
    using Microsoft.Win32;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        public MainWindow()
        {
            this.InitializeComponent();
        }

        private void Open_OnClick(object sender, RoutedEventArgs e)
        {
            var dialog = new OpenFileDialog();
            var result = dialog.ShowDialog();

            if (result.Value)
            {
                this.actiproEditor.Document.LoadFile(dialog.FileName);
            }
        }

        private void Jump_OnClick(object sender, RoutedEventArgs e)
        {
            var line = (int)double.Parse(this.txtJump.Text);
            line = line - 1; // in Actipro Synax Editor the first line is line #0
            if (line < 0)
            {
                line = 0;
            }
            else if (line >= this.actiproEditor.ActiveView.CurrentSnapshot.Lines.Count)
            {
                line = this.actiproEditor.ActiveView.CurrentSnapshot.Lines.Count;
            }

            var position = new ActiproSoftware.Text.TextPosition(line, 0);
            this.actiproEditor.ActiveView.Scroller.ScrollIntoView(position, false);
            this.actiproEditor.Caret.Position = position;
            this.actiproEditor.Focus();
        }
    }
}

Comments (1)

Answer - Posted 3 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Mark,

I think the problem is your line 38 where you are recognizing a line is out of range but you forgot to subtract 1 in response.  A better way to do all that would simply be this at line 31, as it will do proper bounds checking:

var lineIndex = Math.Max(0, Math.Min(actiproEditor.ActiveView.CurrentSnapshot.Lines.Count - 1, line - 1));  // Line indices are zero-based


Actipro Software Support

The latest build of this product (v2019.1 build 0683) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.