From f5a49be6cd609b5ccfd4cec45d70c3a8b1f78a07 Mon Sep 17 00:00:00 2001 From: tsuji Date: Thu, 25 Nov 2021 17:17:32 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=BBpath=E5=85=A5=E5=8A=9B=E3=81=AE?= =?UTF-8?q?=E3=82=A8=E3=83=A9=E3=83=BC=E3=81=AB=E3=82=88=E3=82=8B=E5=87=A6?= =?UTF-8?q?=E7=90=86=E4=B8=AD=E6=96=AD=E6=99=82=E3=81=AE=E3=83=A1=E3=83=83?= =?UTF-8?q?=E3=82=BB=E3=83=BC=E3=82=B8=E5=87=BA=E5=8A=9B=E3=82=92=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Code/CSRender/CSRender/PageComboBox.cs | 123 +++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 Code/CSRender/CSRender/PageComboBox.cs diff --git a/Code/CSRender/CSRender/PageComboBox.cs b/Code/CSRender/CSRender/PageComboBox.cs new file mode 100644 index 0000000..4874d07 --- /dev/null +++ b/Code/CSRender/CSRender/PageComboBox.cs @@ -0,0 +1,123 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; +using System.Windows.Controls.Primitives; +using System.Text.RegularExpressions; + +namespace CSRender +{ + public partial class MainWindow : Window + { + //コンボボックスの要素 + public class ComboBoxSet + { + public int Id { get; set; } + public string Item { get; set; } + } + + //クリックしたときに全体を選択 + private void PageBox_GotFocusSelectAll(object sender, RoutedEventArgs e) + { + TextBox box = (TextBox)sender; + //box.SelectAll(); + this.Dispatcher.InvokeAsync(() => { Task.Delay(10); box.SelectAll(); }); + } + + //ページ指定のプルダウンに応じてテキストボックスを活性化/非活性化 + private void PageRange_SelectionChanged(object sender, SelectionChangedEventArgs e) + { + var SelectedItem = (ComboBoxSet)PageRange.SelectedItem; + if (SelectedItem == null) return; + string PageType = (string)SelectedItem.Item; + if(PageType == "指定") + { + pageBox.IsEnabled = true; + pageLabel.IsEnabled = true; + } + else + { + pageBox.IsEnabled = false; + pageLabel.IsEnabled = false; + } + } + + //数字とハイフン、カンマ以外を無効化 + private void PageBox_PreviewTextInput(object sender, TextCompositionEventArgs e) + { + var Pagebox = (TextBox)sender; + string str = Pagebox.Text;//文字列 + var inputPage = e.Text;//入力された文字 + + //正規表現で入力文字の判定、数字とピリオド、カンマならtrue + bool PageCheck = new System.Text.RegularExpressions.Regex("[0-9,-]").IsMatch(inputPage); + + //入力文字が数値とカンマ、ハイフン以外だったら無効 + if (PageCheck == false) + { + e.Handled = true;//無効 + return;//終了 + } + + //キャレット(カーソル)位置が先頭(0)であるときの、ハイフン入力は無効 + if (Pagebox.CaretIndex == 0 && inputPage == "-") { e.Handled = true; return; } + //キャレット(カーソル)位置が先頭(0)であるときの、カンマ入力は無効 + if (Pagebox.CaretIndex == 0 && inputPage == ",") { e.Handled = true; return; } + } + + //誤ったハイフン・カンマの入力を修正 + private void PageBox_LostFocus(object sender, RoutedEventArgs e) + { + //ハイフン、カンマの削除 + + var PageBox = (TextBox)sender; + string inputText = PageBox.Text; + + // -,や,- は入力ミスなので後の文字だけ削除 + inputText = inputText.Replace("-,", "-"); + inputText = inputText.Replace(",-", ","); + + //先頭か末尾にあった場合は削除 + if (inputText.StartsWith(",") || inputText.EndsWith(",")) + { + inputText = inputText.TrimStart(','); + inputText = inputText.TrimEnd(','); + } + if (inputText.StartsWith("-") || inputText.EndsWith("-")) + { + inputText = inputText.TrimStart('-'); + inputText = inputText.TrimEnd('-'); + } + + PageBox.Text = inputText; + } + + private void PageBox_PreviewExecuted(object sender, ExecutedRoutedEventArgs e) + { + //貼り付け無効 + if (e.Command == ApplicationCommands.Paste) + { + e.Handled = true; + } + } + + //スペースキーが押されたときは、それを無効にする + private void PageBox_PreviewKeyDown(object sender, KeyEventArgs e) + { + if (e.Key == Key.Space) e.Handled = true; + } + + } + + +} -- 2.22.0