1
2

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 3 years have passed since last update.

LINQPadに複数テキスト行入力ダイアログを追加

Last updated at Posted at 2021-03-15

C#erのみなさんは Linqpad を使っている方も多いかと思います。
ちょっとしたデータ分析では結果を表やグラフで出力でき、nugetも使えてデバッガも動きVisualStudioよりも便利なことも多いです。

そんな利用頻度の高いLinqpadですが、データとしてcsvやjsonなどの複数行テキストを入力したい場合、一度ファイルに保存して、そのファイル名を入力する形となるのが若干億劫です。1行だけなら以下のようにLinqpad標準の機能はあるのですが。

var filename = Util.Readline("ファイル名を入力してください");

使い捨てのデータをわざわざファイル作成するのも面倒なので、実行中にテキストをペーストできるよう複数行入力ダイアログ機能を入れましょう。

利用イメージ

使い方は画像内のコードでイメージできるかと思いますが MyExtensions.ReadLines() で読み込みます。メソッド戻り値はstring[]、キャンセルされた場合は空配列(string[0])が戻ります。
image.png

ソース

以下コードをMy Queries/My Extensionsの中にあるMy Extensions クラス内にコピペし保存すれば どのQueryでも上記の画像のように使えるようになります。

public static string[] ReadLines(string title = null)
{
	var win = new System.Windows.Window() { Width = 400, Height = 300, Title = title ?? string.Empty };
	var grid = new System.Windows.Controls.Grid();
	grid.RowDefinitions.Add(new System.Windows.Controls.RowDefinition() { Height = new System.Windows.GridLength(1, System.Windows.GridUnitType.Star) });
	grid.RowDefinitions.Add(new System.Windows.Controls.RowDefinition() { Height = new System.Windows.GridLength(36) });
	var margin3 = new System.Windows.Thickness(3);
	var tbox = new System.Windows.Controls.TextBox()
	{
		Margin = margin3,
		AcceptsReturn = true,
		AcceptsTab = true,
		VerticalScrollBarVisibility = System.Windows.Controls.ScrollBarVisibility.Auto,
		HorizontalScrollBarVisibility = System.Windows.Controls.ScrollBarVisibility.Auto,
	};
	var btnOk = new System.Windows.Controls.Button() { Content = "OK", Margin = margin3, Width = 80 };
	var btnCancel = new System.Windows.Controls.Button() { Content = "Cancel", Margin = margin3, Width = 80 };
	btnOk.Click += (_, _) => { win.DialogResult = true; win.Close(); };
	btnCancel.Click += (_, _) => { win.DialogResult = false; win.Close(); };
	var sp = new System.Windows.Controls.StackPanel()
	{
		Orientation = System.Windows.Controls.Orientation.Horizontal,
		HorizontalAlignment = System.Windows.HorizontalAlignment.Right,
	};
	sp.Children.Add(btnOk);
	sp.Children.Add(btnCancel);
	System.Windows.Controls.Grid.SetRow(tbox, 0);
	System.Windows.Controls.Grid.SetRow(sp, 1);
	grid.Children.Add(tbox);
	grid.Children.Add(sp);
	win.Content = grid;
	return win.ShowDialog() == true ? tbox.Text.Split("\r\n") : new string[0];
}
1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?