作成日: 2024/05/20 最終更新日: 2024/05/20
文書種別
使用方法
詳細
C1RichTextBoxコントロールにフォーカスが当たったとき、デフォルトではIMEモードが半角英数字になっています。
フォーカスが当たったときにIMEモードを日本語入力に変更するには、GotFocusイベントなどで次のようなコードを実行します。
◎サンプルコード(XAML)
<c1:C1RichTextBox x:Name="richTextBox1" GotFocus="richTextBox1_GotFocus"/>
◎サンプルコード(VB)
Class MainWindow
Private Sub richTextBox1_GotFocus(sender As Object, e As RoutedEventArgs)
Dim TextBox = GetChildrenOfType(Of TextBox)(richTextBox1).First()
InputLanguageManager.SetInputLanguage(TextBox, New System.Globalization.CultureInfo("ja-JP"))
InputMethod.SetPreferredImeConversionMode(TextBox, ImeConversionModeValues.Native)
InputMethod.SetPreferredImeState(TextBox, InputMethodState.On)
End Sub
Private Iterator Function GetChildrenOfType(Of T As DependencyObject)(e As DependencyObject) As IEnumerable(Of T)
If e IsNot Nothing Then
For i As Integer = 0 To VisualTreeHelper.GetChildrenCount(e) - 1
Dim child As DependencyObject = VisualTreeHelper.GetChild(e, i)
If child IsNot Nothing AndAlso TypeOf child Is T Then
Yield DirectCast(child, T)
End If
For Each childOfChild As T In GetChildrenOfType(Of T)(child)
Yield childOfChild
Next
Next
End If
End Function
End Class
◎サンプルコード(C#)
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void richTextBox1_GotFocus(object sender, RoutedEventArgs e)
{
var textbox = GetChildrenOfType<TextBox>(richTextBox1).First();
InputLanguageManager.SetInputLanguage(textbox, new System.Globalization.CultureInfo("ja-JP"));
InputMethod.SetPreferredImeConversionMode(textbox, ImeConversionModeValues.Native);
InputMethod.SetPreferredImeState(textbox, InputMethodState.On);
}
private IEnumerable<T> GetChildrenOfType<T>(DependencyObject e) where T : DependencyObject
{
if (e != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(e); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(e, i);
if (child != null && child is T)
{
yield return (T)child;
}
foreach (T childOfChild in GetChildrenOfType<T>(child))
{
yield return childOfChild;
}
}
}
}
}