C#EventHandler的Form_Load [英] C# EventHandler Form_Load

查看:139
本文介绍了C#EventHandler的Form_Load的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我正在开发一个程序,在该程序中,我打开一个带有一些文本,条形码和一些要打印的图像的窗口.这是我的问题:我想以主形式输入一个字符串,然后将其转换并以第二种形式显示.所有这些操作都应该在事件Form_Load上执行,或者以我的情况为例:Printform_Load但是我找不到为什么我的代码无法正常工作的原因.
在代码打印窗口中的代码:

Hello,

I''m working on a program where I open a window with some text, a barcode and some images in it which I want to print. This is my problem: I want to enter a string in the main form, and convert it and display it in the second form. All these actions should be executed on the event Form_Load, or in my case: Printform_Load But I can not find why my code is not working.

Code in the code printing window:

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 WindowsFormsApplication1
{
    public partial class Printform : Form
    {
        public Printform()
        {
            InitializeComponent();
        }
        private void BtnProgram_Click(object sender, EventArgs e)
        {
            SSP_MainFrom MF = new SSP_MainFrom();
            MF.IdentityUpdated += new 
// Its going wrong here: this is underlined red:
SSP_MainFrom.IdentityUpdateHandler(Printform_Load);
            MF.Show();
        }

        private void Printform_Load(object sender,  e)
        {
            LblSerialNumberCoded = e.TestData;
        }
    }
}



主窗口中的代码:



code in the main window:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using BarCode;

namespace WindowsFormsApplication1
{
    public partial class SSP_MainFrom : Form
    {
        public delegate void IdentityUpdateHandler(object sender, IdentityUpdateEventArgs e);
        public event IdentityUpdateHandler IdentityUpdated;

        public SSP_MainFrom()
        {
            InitializeComponent();
        }



        private void BtnProgram_Click(object sender, EventArgs e)
        {
            string sTestData = "testdata";
            IdentityUpdateEventArgs args = new IdentityUpdateEventArgs(sTestData);
            IdentityUpdated(this, args);

            new Printform().Show();
        }

        public class IdentityUpdateEventArgs : System.EventArgs
        {
            private string mTestData;
            public IdentityUpdateEventArgs(string sTestData)
            {
                this.mTestData = sTestData;
            }

            public string TestData
            {
                get
                {
                    return mTestData;
                }
            }

        }
    }

推荐答案

首先,事件Form.Load是一个伪"事件:在构造表单之后,在所有其他事件之前调用该事件.您无法通过处理解决任何问题.同样,您可以在构造函数的最后调用一些函数.可能甚至添加了它以支持Designer,以便在构造后立即以与通过Designer通过事件处理程序添加的所有其他用户方法相同的样式引入一些自定义初始化的入口点.我个人不在Designer中添加任何事件处理程序,也不建议这样做.

现在,您的问题与此无关.这是关于表单协作的流行问题.最健壮的解决方案是在Form类中实现适当的接口,并传递接口引用而不是对Form的整个实例"的引用.请查看我过去的解决方案以获取更多详细信息:如何以两种形式在列表框之间复制所有项目 [
First of all, the event Form.Load is a "fake" one: it is called after the form is constructed before all other events. You cannot solve any problems by handling it. With the same success, you can call some function in your constructor, at the very end. Probably, this even is added to support the Designer, to introduce an entry point for some custom initialization right after construction in the same style as all other user methods added through the Designer, the event handlers. I personally do not add any event handlers in Designer and do not advise doing so.

Now, your problem is not related to this. This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

See all other solutions to this problem in the discussion referenced above, of course.

—SA


当我看到Unareshraju链接中的小代码时,在这里找到了我的解决方案:
When i saw the small codes in the link from Unareshraju i found my solution here: Passing Data Between Forms[^]

Thanks!


pieterjann,
据我了解,您有两种形式
让我们form1,form2
当您的应用程序第一次执行时,它将不加任何点击地加载form1(我们知道这是假设),假设您在表头中输入了一个文本框,否则您已经加载了数据,很好.
现在,这些数据是通过按钮click事件向右传递给form2的!
然后在加载两个表单时将显示这些数据.
因为我们需要在form1和form2之间有一个调解人,所以我们需要使用传递参数对象,为此我们在这里有示例代码
请在此处查看:
http://codeincsharp.blogspot.in/2008/04/how-to-pass-values-to-another-form.html [
Hi pieterjann,
As i understood you have two form
let us form1,form2
when your application executed first it will load form1 with out any clicking (we know this ),suppose you have a textbox in form tou entered somthing or else you have loaded data ,fine.
now these data you passed into form2 by the button click event right!
then while loading the form two those data will displayed.
for that weneed to have amediator between form1 and form2 two pass that data.for that we need to use passing parameters objects, for that we have sample code here
please see here: http://codeincsharp.blogspot.in/2008/04/how-to-pass-values-to-another-form.html[^]


这篇关于C#EventHandler的Form_Load的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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