将变量从主窗体传递到输入窗体

发布于 2024-08-05 11:09:38 字数 240 浏览 8 评论 0原文

我有一个简单的问题。我有一个主窗体,然后是一个启动窗体,我可以从中选择要生成的新 3D 模型。当从启动表单中选择一个新的 3D 模型时,我想首先检查我之前处理的模型是否已保存。我只是想使用委托将布尔值从主窗体传递到启动窗体,但我似乎无法访问主窗体或其任何变量。我认为这就像说: frmMain myForm = new frmMain(); 一样简单,但是输入 frmMain 不会在智能感知中显示任何内容。

有什么提示吗?

I have a simple question. I have a main form, and then a startup form from where I can select a new 3D model to generate. When selecting a new 3D model from the startup form, I want to check first whether the previous model I worked on has been saved or not. I simply want to pass a boolean value from the main form to the startup form using a delegate, but I can't seem to access the main form or any of its variables. I thought it would be as simple as saying: <code>frmMain myForm = new frmMain();</code>, but typing frmMain doesn't show up anything in intellisense.

Any hints?

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

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

发布评论

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

评论(3

分開簡單 2024-08-12 11:09:38

在主窗体上添加公共属性

public bool IsDirty
{
    get;set;
}

,然后您可以在启动窗体中访问 this.ParentForm.IsDirty

请记住在显示启动窗体时传递对主窗体的引用... startupForm.showDialog(this);

Add a public property on your main form

public bool IsDirty
{
    get;set;
}

you can then access this.ParentForm.IsDirty in your startup form,

remember to pass a reference to the main form when you show the startup form ... startupForm.showDialog(this);

后eg是否自 2024-08-12 11:09:38

您的主窗体无法通过启动窗体访问。您必须将其存储到您想要使用它的地方可以访问的位置。

您也可以通过以下方式(以及其他方式:)来完成此操作,

// This class is mainly used to transfer values in between different components of the system
    public class CCurrent
    {

        public static Boolean Saved = false;


    }

确保将此类放在两种形式都可以访问的命名空间中。

现在,在您的 frmMain 表单中设置 CCurrent.Saved 的值并在启动表单中访问它。

Your main form is not accessible to Startup form.You have to store it to something that is accessible at a point where you want to use it.

You can do it by following way also ( along with other ways :)

// This class is mainly used to transfer values in between different components of the system
    public class CCurrent
    {

        public static Boolean Saved = false;


    }

make sure you put this class in namespace which is accessible to both the forms.

Now In your frmMain form set the value of CCurrent.Saved and access it in your startup form.

吾性傲以野 2024-08-12 11:09:38

这是我的建议:
在主窗体中放置一个 3DModel 对象属性:

private Model _model;

将您的启动窗体声明为对话框(如 OpenFileDialog)并执行如下操作:

public void OpenModel()
{
    using(var frm=new StartUpForm())
    {
        if(frm.ShowDialog()==DialogResult.OK))
        {
            if(_model.IsDirty)
            {
                if(MessageBox.Show("Model is changed do you want to save it?","",MessageBoxButtons.YesNo)==DialogResult.Yes)
                    _model.Save();

                _model=frm.SelectedModel;
            }
        }
    }
}

您的启动窗体应具有如下界面:

public interface IStartupForm:IDisposable
{
    DialogResult ShowDialog(IWin32Window parent);
    Model SelectedModel{get;}
}

Here's my suggestion:
place a 3DModel object property in your main form:

private Model _model;

Declare your startup form as a Dialog ( like OpenFileDialog) and do something like this:

public void OpenModel()
{
    using(var frm=new StartUpForm())
    {
        if(frm.ShowDialog()==DialogResult.OK))
        {
            if(_model.IsDirty)
            {
                if(MessageBox.Show("Model is changed do you want to save it?","",MessageBoxButtons.YesNo)==DialogResult.Yes)
                    _model.Save();

                _model=frm.SelectedModel;
            }
        }
    }
}

your startup form should have a interface like this:

public interface IStartupForm:IDisposable
{
    DialogResult ShowDialog(IWin32Window parent);
    Model SelectedModel{get;}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文