gpt4 book ai didi

c# - 如何让 UserControl 派发 HelpRequest 事件

转载 作者:太空狗 更新时间:2023-10-30 00:44:18 24 4
gpt4 key购买 nike

在“HelpRequest-mode”中单击时如何让 UserControl 发送 HelpRequest 事件?

我已尝试设置最简单的 UserControl,仅使用一些彩色背景。但无法让它工作。

更新

namespace SeoTools.UI.Components
{
public partial class HelpRequestTest : UserControl
{
public HelpRequestTest()
{
InitializeComponent();
}

protected override void OnHelpRequested(HelpEventArgs hevent)
{
base.OnHelpRequested(hevent); //can't get it here either
}
}
}

namespace SeoTools.UI.Components
{
partial class HelpRequestTest
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Component Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// HelpRequestTest
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.Name = "HelpRequestTest";
this.Size = new System.Drawing.Size(114, 94);
this.ResumeLayout(false);

}

#endregion
}
}

...

private void WebHelpRequested(object sender, HelpEventArgs hlpevent)
{
string tag = ((Control)sender).Tag.ToString();
if (!string.IsNullOrEmpty(tag))
{
try
{
ProcessStartInfo sInfo = new ProcessStartInfo(tag);
Process.Start(sInfo);
}
catch (Exception) { }
}
hlpevent.Handled = true;
}

...

//
// helpRequestTest1
//
this.helpRequestTest1.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.helpRequestTest1.Location = new System.Drawing.Point(91, 3);
this.helpRequestTest1.Name = "helpRequestTest1";
this.helpRequestTest1.Size = new System.Drawing.Size(114, 94);
this.helpRequestTest1.TabIndex = 1;
this.helpRequestTest1.Tag = "http://offerta.se";
this.helpRequestTest1.HelpRequested += new System.Windows.Forms.HelpEventHandler(this.WebHelpRequested);

最佳答案

最简单的形式:

public partial class UserControl1 : UserControl {
public UserControl1() {
InitializeComponent();
}
}

public partial class Form1 : Form {
public Form1() {
InitializeComponent();
userControl11.Tag = "http://www.stackoverflow.com";
userControl11.HelpRequested += userControl11_HelpRequested;
}

private void userControl11_HelpRequested(object sender, HelpEventArgs hlpevent) {
string tag = ((Control)sender).Tag.ToString();
if (!string.IsNullOrEmpty(tag)) {
try {
ProcessStartInfo sInfo = new ProcessStartInfo(tag);
Process.Start(sInfo);
}
catch (Exception) { }
}
hlpevent.Handled = true;
}
}

这在我的机器上“按原样”工作。我让它“停止”工作的唯一方法是,如果我向 UserControl 添加一个 TextBox 控件,并处理它的 HelpRequest 事件。

public partial class UserControl1 : UserControl {
public UserControl1() {
InitializeComponent();
}

private void textBox1_HelpRequested(object sender, HelpEventArgs hlpevent) {
//This prevents the UserControl from firing it's help request:
}
}

所以此时我唯一的建议是查看 UserControl 中的子控件,看看它们是否干扰了 UserControl 调用事件的能力。

关于c# - 如何让 UserControl 派发 HelpRequest 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8203382/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com