如何正确保存MemoEdit的格式

时间:2017-03-06 14:02:01

标签: vb.net winforms devexpress

我有一个MemoEdit字段,当用户插入的文本每行的字符数多于我的MemoEdit-Field时,组件行为存在问题。 一旦达到一条线的最大长度,该组件就会断开该线并在下一行继续。

然而,这些阵容只是" visual"这意味着每次休息时都没有添加任何换行符。 由于这些缺少的换行符,即使正确格式化的文本在保存后也会被解散,因为正在格式化它们的用户无法看到是否有回车换行或只是一个" visual"越线。 有没有办法根据备注编辑字段的大小确定字符串中的换行符即将发生的确切索引? 特别是当我尝试考虑单个字符间距时,似乎很难找到一种通用的方法。

2 个答案:

答案 0 :(得分:0)

最简单的解决方案是禁用自动换行。您可以通过设置memoEdit.Properties.WordWrap = false来实现此目的。这将导致您的编辑器向右流动而不是制作视觉换行符。因此,您的用户可以在需要时添加换行手册。

获取文本应该被破坏的索引似乎很难。我想出了类似的东西:

private void memoEdit1_TextChanged(object sender, EventArgs e)
{
   using (Graphics gr = Graphics.FromHwnd(IntPtr.Zero))
   {
      //Get the actual line
      string text = memoEdit1.Lines[memoEdit1.Lines.Length - 1];

      //Calculate the size of the string
      SizeF size = gr.MeasureString(text, memoEdit1.Font);

      //Check if the string is as big as the memoedit
      //Notice the 50 which is constant for the width of the vertical scrollbar
      //so far you use one. You may need to fit this to your needs.
      if (size.Width >= memoEdit1.Size.Width - 50)
      {
         int index = memoEdit1.Text.Length - 1;
      }
    }
}

这只是一个想法,您可能需要修改它以在项目中正确运行。我不熟悉vb.net,所以这是C#,但应该可以采用。

希望这对你有所帮助。

答案 1 :(得分:0)

Win32 API中有EM_FMTLINES条消息。如果您将此邮件发送到TextBox控件,则其Text属性将包含软换行符。软换行符是CrCrLf个字符的组合,用于标记因换行而导致换行的位置。
以下是简单扩展模块的示例:

Module TextBoxExtension

    Private Const EM_FMTLINES As UInteger = &HC8

    <DllImport("user32.dll", CharSet:=CharSet.Auto)>
    Private Function SendMessage(hWnd As IntPtr, Msg As UInteger, wParam As Integer, lParam As IntPtr) As IntPtr
    End Function

    <Extension()>
    Public Function GetWrappedText(ByVal textBox As TextBox) As String

        Dim handle = textBox.Handle

        SendMessage(handle, EM_FMTLINES, 1, IntPtr.Zero)

        GetWrappedText = textBox.Text

        SendMessage(handle, EM_FMTLINES, 0, IntPtr.Zero)

    End Function

End Module

MemoEdit class是保存TextBoxMaskBox类实例的框。 TextBoxMaskBox类继承自System.Windows.Forms.TextBox类。您可以从MemoEdit.MaskBox属性获取它。

Dim text = MemoEdit1.MaskBox.GetWrappedText

现在您可以使用String.Split方法获取这些行:

Dim lines = text.Split({vbCr & vbCrLf, vbCrLf, vbLf}, StringSplitOptions.None)
相关问题