C# 发送控件快捷方式

发布于 2024-10-19 03:19:10 字数 190 浏览 1 评论 0原文

我正在用 c Sharp/xaml 编写一个程序。我有一个保存按钮,并且认为使其生效的最简单方法是在按下时如果我可以发送“control s”信号。执行此操作的命令是什么(以及任何包含 VS 不会添加为标准的命令)?

减少线程数量的一个完全不同的问题是,当您到达末尾时,如何使文本块(或文本框,如果更容易的话)自动换行,而不是继续将文本发送到屏幕外。

I am making a program in c sharp/xaml. I have a save button, and figured the easiest way to make it effective was when pressed if I could send the "control s" signal. What is the command (and any includes VS wouldn't add as standard) to do so?

A completely different question to cut down on thread count, how would I make a textblock (or textbox if easier) automatically newline when you reach the end rather than continuing to send text offscreen.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

沩ん囻菔务 2024-10-26 03:19:10

在将 UI 与按键耦合之前,我建议您先研究一下更好的模式(例如 命令),但如果您确实想这样做,可以使用 SendKeys 类。这将允许您将按键发送到应用程序,就像用户按下这些键一样。

至于第二个问题,如果您使用 WPF,只需创建一个文本框元素并设置 AcceptsReturn="True"TextWrapping="Wrap" 。这是一个例子:

<TextBox
  Name="tbMultiLine"
  TextWrapping="Wrap"
  AcceptsReturn="True"
  VerticalScrollBarVisibility="Visible"
>
  This TextBox will allow the user to enter multiple lines of text.  When the RETURN key is pressed, 
  or when typed text reaches the edge of the text box, a new line is automatically inserted.
</TextBox>

There are better patterns that I would suggest looking into before coupling your UI to keypresses (such as Commands) but if you really want to do this, you can use the SendKeys class from windows forms. This will allow you to send key presses to the application as if the user pressed those keys.

As for the second question, if you're using WPF just create a textbox element and set AcceptsReturn="True" and TextWrapping="Wrap". Here's an example:

<TextBox
  Name="tbMultiLine"
  TextWrapping="Wrap"
  AcceptsReturn="True"
  VerticalScrollBarVisibility="Visible"
>
  This TextBox will allow the user to enter multiple lines of text.  When the RETURN key is pressed, 
  or when typed text reaches the edge of the text box, a new line is automatically inserted.
</TextBox>
蓝海似她心 2024-10-26 03:19:10

您可以使用 SaveFileDialog() 。

private void button1_Click(object sender, EventArgs e)
    {
        // When user clicks button, show the dialog.
        saveFileDialog1.ShowDialog();
    }

    private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
    {
        // Get file name.
        string name = saveFileDialog1.FileName;
        // Write to the file name selected.
        // ... You can write the text from a TextBox instead of a string literal.
        File.WriteAllText(name, "test");
    }

You can use the SaveFileDialog() .

private void button1_Click(object sender, EventArgs e)
    {
        // When user clicks button, show the dialog.
        saveFileDialog1.ShowDialog();
    }

    private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
    {
        // Get file name.
        string name = saveFileDialog1.FileName;
        // Write to the file name selected.
        // ... You can write the text from a TextBox instead of a string literal.
        File.WriteAllText(name, "test");
    }
呆头 2024-10-26 03:19:10

如果您使用 XAML,我会假设您使用 WPF。因此,您应该能够将按钮的 Command 属性绑定到 ApplicationCommands.Save

If you're using XAML I'll assume you're using WPF. So you should be able to bind the Command property of the Button to ApplicationCommands.Save.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文