为什么我的UserControl不保留其初始化? [英] Why isn't my UserControl keeping its initialization?

查看:111
本文介绍了为什么我的UserControl不保留其初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UserControl,我想为此初始化一些成员:

I have a UserControl for which I think I'm initializing some of the members:

// MyUserControl.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MyNamespace
{
    public partial class MyUserControl : UserControl
    {
        private string m_myString;
        private int m_myInt;

        public string MyString
        {
            get { return m_myString; }
            set { m_myString = value; }
        }
        public int MyInt
        {
            get { return m_myInt; }
            set { m_myInt = value; }
        }

        public MyUserControl()
        {
            InitializeComponent();

            MyString = "";   // was null, now I think it's ""
            MyInt = 5;       // was 0, now I think it's 5
        }

        // .........
    }
}

但是,当我将此控件插入到我的主窗体中并调用一个检查MyUserControl中的值的函数时,事情看起来好像没有被初始化:

When I insert this control into my main form, though, and call a function that checks values within MyUserControl, things don't look like they're initialized:

// MainForm.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MyProgram
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MyButton_Click(object sender, EventArgs e)
        {
            // this prints 0 rather than 5
            MessageBox.Show(this.theUserControl.MyInt.ToString());
        }
    }
}

我猜这是一个真的简单错误,但我不知道在哪里.我已经尝试将this.放在前面,但这可能不是修复代码的方法. :)

I'm guessing this is a really simple error but I don't know where. I've tried prepending this. to things, but this probably isn't the way to go about fixing code. :)

一如既往的感谢!

编辑:按照Pete的建议进入设计器代码,向我显示了重写发生的位置.首先,我调用了用户控件的构造函数,然后,使用默认值覆盖了这些值.我没有指定任何默认值(Sanjeevakumar Hiremath的建议),因此默认值是原始类型的值(对于int,它是0).

Stepping into the designer code as Pete suggested showed me where the write-over was happening. First I called the constructor of the user control, and then later, the values got overwritten with default values. I hadn't specified any default values (Sanjeevakumar Hiremath's suggestion) so the default values were those of the primitive types (for int this was 0).

推荐答案

您可能在这里看到的是设计人员的工件.如果您在添加MyUserControl之后曾经在设计器中打开MainForm,它可能会在MainForm的生成的InitializeComponent方法中记录MyUserControl的默认值.这些记录的值在MyUserControl的构造函数运行后会重新分配,因此它们会覆盖您设置的值.

What you're likely seeing here is an artifact of the designer. If you ever opened up MainForm in a designer after you added MyUserControl it likely recorded the default values of MyUserControl in the generated InitializeComponent method of MainForm. These recorded values are re-assigned after the constructor of MyUserControl runs hence they're overwriting the values you set.

您可以通过使用DesignerSerializationVisibilityAttribute

这篇关于为什么我的UserControl不保留其初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆