简体   繁体   English

C# windows 表格项目

[英]C# windows form project

Method of the 1st Form Class:第一种形式 Class 的方法:

private void englishToolStripMenuItem_Click(object sender, EventArgs e)
{
    this.Hide();
    var Editor = new Editor();
    Editor.Engprop = 1;
    Editor.Closed += (s, args) => this.Close();
    Editor.Show();
}

Method of the 2nd Form Class:第二种形式 Class 的方法:

public partial class Editor : Form
{
    public int Engprop { get; set; }
    public Uri MyProperty { get; set; }
    public Editor()
    {
       InitializeComponent();
        webBrowser1.ScriptErrorsSuppressed = true;

    }
    Uri temp = new Uri("file:///C:/Users/PC/Desktop/projese302/newtmp.html");
    Uri eng = new Uri("file:///C:/Users/PC/Desktop/projese302/engtemp.html");

    private void Editor_Load(object sender, EventArgs e)
    {
        if (Engprop==1)
        {
            webBrowser1.Navigate(eng);
        }
    }

I am trying to do that when Engprop becomes 1 (when a user clicks the item of the ToolStripMenu), it should navigate the eng URL but it doesn't work properly.我试图在Engprop变为 1 时(当用户单击 ToolStripMenu 的项目)时这样做,它应该导航eng URL 但它不能正常工作。 I will be grateful for your help.我会很感激你的帮助。

I suppose the Editor_Load method is called from the OnLoad event of the form.我想 Editor_Load 方法是从表单的 OnLoad 事件中调用的。

If so, I'm not sure when this event is raised... Can be after the constructor (it explain why you have this problem) or after the Show method (in this case, I don't know why it doesn't work)如果是这样,我不确定何时引发此事件......可以在构造函数之后(它解释了为什么你有这个问题)或在 Show 方法之后(在这种情况下,我不知道为什么它没有工作)

Try to check if the event is raised before or after you set the Engprop property (debug + breakpoints)尝试检查在设置 Engprop 属性之前或之后是否引发了事件(调试 + 断点)

In any way, I think your code has a lack of architecture, since who use your Editor class need to know that can change the Engprop only between the constructor and the Show method.无论如何,我认为您的代码缺乏架构,因为谁使用您的编辑器 class 需要知道只能在构造函数和 Show 方法之间更改 Engprop。

I think you can make a better/reusable code if:我认为您可以在以下情况下制作更好/可重用的代码:

  • Set the Engprop property as read only and accept his value in the constructor: this is usually done when the language is set when someone instantiate your class and will never change将 Engprop 属性设置为只读并在构造函数中接受他的值:这通常在有人实例化您的 class 并且永远不会更改时设置语言时完成
  • In the setter of the Engprop raise an event when it changes: this is done when you allow to change the language also after the instance is created.在 Engprop 的设置器中,当它发生变化时会引发一个事件:当您允许在创建实例之后也更改语言时,就会完成此操作。 So, your Editor class can add an handler to this event and change the language of the page every time someone changes this property因此,您的编辑器 class 可以向此事件添加处理程序,并在每次有人更改此属性时更改页面的语言

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM