.net 自定义 MessageBox 文本突出显示

发布于 2024-12-29 19:46:01 字数 1193 浏览 0 评论 0原文

为什么?

我用 C# 构建了一个简单的自定义 MessageBox 作为对话框。当我通过 Show() 显示消息框时,消息文本不会突出显示。当我通过 ShowDialog() 显示此消息框时,文本突出显示。

我不想突出显示我的文本。有什么想法或想法吗?

public partial class MyMessageBox : Form
    {
        private String mCaption;
        private String mMessage;


        public MyMessageBox( Form anOwner, String aCaption, String aMessage )
        {
            InitializeComponent();

            mCaption = aCaption;
            Owner = anOwner;
            mMessage = aMessage;
        }


        private void btnCancelRequest_Click( object sender, EventArgs e )
        {
            ( (AddressForm)Owner ).RequestCancelled();
        }


        private void btnOk_Click( object sender, EventArgs e )
        {
            CloseDialog();
        }


        public void CloseDialog()
        {
            Close();
        }


        // Called from the Address Form
        public void HideCancelRequestButton()
        {
            btnCancelRequest.Visible = false;
        }


        private void MyMessageBox_Activated( object sender, EventArgs e )
        {
            Text = mCaption;
            txtMessage.Text = mMessage;
        }
    }

Why?

I built a simple custom MessageBox as as a Dialog in C#. When I show the message boxvia Show(), the message text is not highlighted. When I show this messagebox vi ShowDialog(), the text is highlighted.

I don't want my text to be highlighted. Any thoughts or ideas?

public partial class MyMessageBox : Form
    {
        private String mCaption;
        private String mMessage;


        public MyMessageBox( Form anOwner, String aCaption, String aMessage )
        {
            InitializeComponent();

            mCaption = aCaption;
            Owner = anOwner;
            mMessage = aMessage;
        }


        private void btnCancelRequest_Click( object sender, EventArgs e )
        {
            ( (AddressForm)Owner ).RequestCancelled();
        }


        private void btnOk_Click( object sender, EventArgs e )
        {
            CloseDialog();
        }


        public void CloseDialog()
        {
            Close();
        }


        // Called from the Address Form
        public void HideCancelRequestButton()
        {
            btnCancelRequest.Visible = false;
        }


        private void MyMessageBox_Activated( object sender, EventArgs e )
        {
            Text = mCaption;
            txtMessage.Text = mMessage;
        }
    }

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

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

发布评论

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

评论(1

笙痞 2025-01-05 19:46:01

虽然我不明白为什么文本会突出显示,但您可以通过显式指定文本框选择长度来避免这种情况:

txtMessage.SelectionLength = 0;

尽管正如杰里米评论的那样,这可能是更好地使用标签,除非您实际上期望用户输入。

While I don't see why the text would be highlighted, you could avoid this by explicitly specifying the textbox selection length:

txtMessage.SelectionLength = 0;

though as Jeremy commented, this may be a better use of a label unless you're actually expecting user input.

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