2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

TextBox への貼り付け時に改行コードを調整

Last updated at Posted at 2023-05-12

Windows Forms の TextBox へクリップボードから貼り付ける際、改行コードが CR+LF でないと改行が表示されません。

貼り付け時に改行コードを調整して対処しました。

public class MyTextBox : TextBox
{
    public static string NormalizeNewLine(string s)
    {
        return s.Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", "\r\n");
    }

    protected override void WndProc(ref Message m)
    {
        const int WM_PASTE = 0x302;
        if (m.Msg == WM_PASTE)
        {
            try
            {
                var t = Clipboard.GetText();
                Paste(NormalizeNewLine(t));
                return;
            }
            catch { }
        }
        base.WndProc(ref m);
    }
}

参考

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?