14

C# でメモ帳アプリケーションを実装しました。すべての機能が完全に機能します。正確に実装できないことが 1 つあります。編集ドロップダウン メニューにはいくつかのメニュー項目がありますが、それらの有効なプロパティは状況に応じて変更する必要がありますテキストボックス、2 つの状況で問題があり、このイベントのイベント ハンドラーで有効なプロパティを変更するためにイベントを探しています。問題は次のとおりです。

2) テキスト ボックスでテキストが選択されている場合、削除、コピー、貼り付けのオプションが有効になります。それを検出するにはどうすればよいですか? texchanged イベントをテストし、以下のコードのような条件を記述しましたが、機能しませんでした。 、クリップボードだけがうまく機能します:

private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if (textBox1.SelectionLength> 0)
            button1.Enabled = false;
        if (Clipboard.ContainsText())
            button2.Enabled = false;


    }

リッチテキストボックスではなくテキストボックスを使用する必要があるので、どうすれば問題を解決できますか。任意の提案をいただければ幸いです。どうもありがとう

4

5 に答える 5

16

選択を調べるには

if (textbox1.SelectionLength > 0)
{

}

クリップボードの内容については、

System.Windows.Forms.Clipboard.getText();

クリップボードの内容を確認するには、

IDataObject iData = Clipboard.GetDataObject();
// Is Data Text?
if (iData.GetDataPresent(DataFormats.Text))
    label1.Text = (String)iData.GetData(DataFormats.Text);
else
label1.Text = "Data not found."; 

これはコードに実装されています。上記のように直接使用できます

最も重要なこと、忘れないでください

public virtual string SelectedText { get; set; }

これは、メニュー項目を含む完全なコードです

private void Menu_Copy(System.Object sender, System.EventArgs e)
{
// Ensure that text is selected in the text box.    
if(textBox1.SelectionLength > 0)
    // Copy the selected text to the Clipboard.
    textBox1.Copy();
}

private void Menu_Cut(System.Object sender, System.EventArgs e)
{   
 // Ensure that text is currently selected in the text box.    
 if(textBox1.SelectedText.Length > 0)
    // Cut the selected text in the control and paste it into the Clipboard.
    textBox1.Cut();
 }

Private void Menu_Paste(System.Object sender, System.EventArgs e)
{
// Determine if there is any text in the Clipboard to paste into the text box. 
if(Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
{
    // Determine if any text is selected in the text box. 
    if(textBox1.SelectionLength > 0)
    {
      // Ask user if they want to paste over currently selected text. 
      if(MessageBox.Show("Do you want to paste over current selection?", "Cut Example", MessageBoxButtons.YesNo) == DialogResult.No)
         // Move selection to the point after the current selection and paste.
         textBox1.SelectionStart = textBox1.SelectionStart + textBox1.SelectionLength;
    }
    // Paste current text in Clipboard into text box.
    textBox1.Paste();
  }
}


private void Menu_Undo(System.Object sender, System.EventArgs e)
{
// Determine if last operation can be undone in text box.    
if(textBox1.CanUndo == true)
{
   // Undo the last operation.
   textBox1.Undo();
   // Clear the undo buffer to prevent last action from being redone.
   textBox1.ClearUndo();
}
}
于 2013-02-11T05:40:56.903 に答える
3

私のために働いた。

private void button2_Click(object sender, EventArgs e)
    {
        if (textBox1.SelectedText != String.Empty)  
        {
            label1.Text = textBox1.SelectedText;
        }


        if (Clipboard.ContainsText())
        {
            label2.Text = Clipboard.GetText();
        }
    }
于 2013-02-11T05:37:31.353 に答える
2

私の意見では、これを行う最も簡単な方法は、有効化/無効化の方法を定義することです。

private void editMenuItemOpened(object sender, EventArgs e)
{
    //enable copy and cut only if some text is selected
    copyMenuItem.Enabled = cutMenuItem.Enabled = textBox1.SelectionLength > 0;
    //enable paste only if there's some text in the clipboard to paste
    pasteMenuItem.Enabled = Clipboard.ContainsText();
}

editMenuItem.DropDownOpenedイベント(フォームを使用する場合)またはeditMenuItem.SubmenuOpenedイベント(WPFを使用する場合。この場合RoutedEventArgsの代わりに使用することもできます)に添付します。EventArgs

または、WPF を使用している場合は、textBox1.SelectionChangedイベントを利用できます。フォームには存在しないため、その場合はおそらくtextBox1.MouseUptextBox1.KeyUpイベントを組み合わせて使用​​する必要があります。

于 2013-02-11T08:51:54.143 に答える
1

質問の後半について:

textbox1.TextChanged += new TextChangedEventHandler(textbox1_TextChanged);

private void textbox1_TextChanged(object sender, EventArgs e)
{
    if (textbox1.Text.Length > 0)
    {
        // enable delete, copy & paste functions
    }
    else
    {
        // disable delete, copy & paste functions
    }
}
于 2013-02-11T05:39:14.987 に答える