从C#中的多种形式调用方法 [英] Call a method from multiple forms in C#

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

问题描述

我有一个使用C#和3种形式的基于SDI的项目.

-Form1包含1个按钮和1个标签.我写了一种方法来更改这种形式的文本.

-Form2仅包含1个按钮

-表格3包含1个按钮和1个文本框

当用户在Form1中按下按钮时,Form2显示为斜角,而在Form2中用户按下按钮时,Form3也显示为斜角. (同时打开3个表格)

每次用户在Form3的文本框中键入任何内容时,Form1中的标签将具有相同的值.我不知道该怎么办?如果有可能,请帮助我!

谢谢,

Tuyen Vu Huy

I have a SDI based project using C# with 3 forms in it.

- Form1 contains 1 button and 1 label. I wrote a method to change the text in this form.

- Form2 contains 1 button only

- Form 3 contains 1 button and 1 textbox

when user press button in Form1 then Form2 shows as a diagloque, and user press a button in Form2 then Form3 shows as diagloque too. (3 forms open at the same time)

Everytime user type anything in textbox at Form3 then the label in Form1 will have same value. I don''t know how can I do it? And if it is possile, please help me!

Thanks,

Tuyen Vu Huy

推荐答案

在Form1中设置属性,然后在Form3按钮上单击,为Form1的属性分配文本框值,在Form1上分配textbox.text = Property'的值

希望你能理解:)


例如

Form1.cs
Make a property in Form1 and on Form3 Button click assign the Form1''s property the textbox value and on Form1 assign textbox.text = Property''s value

Hope you have understand :)


Eg

Form1.cs
string tbox;
 public static string Txt
        {
            get
            {
                return tbox;
            }
            set
            {
                tbox= value;
            }

        }




Form3.cs




Form3.cs

Form1.Txt =textbox1.Text;



Form1.cs



Form1.cs

txt.Text=Txt;


我建​​议在这里像这样分解"依赖项:

1. Form1是主窗体,即启动窗体.
2. Form2由Form1显示,但与Form1没有动态交互
3. Form2由Form2表示,并且From3的TextBox中的更改应立即传输到Form1的Label. Form3与Form2没有动态交互

请注意,您可以按以下任一方式查看Form1和Form3之间的关系:

1. Form1观察/监视" Form3中发生的情况,并进行自我更新.要观察" Form1需要:
一个.使它可以访问Form3的TextBox的引用
b.一种了解Form3的TextBox上的文本何时更改的方法.

2.或者,Form3在更改时发布"其TextBox文本,而Form1订阅该发布事件.为此,Form3必须定义一个Form1可以订阅的公开"可见事件.

最后,可以通过将所有窗体之间的所有事件和交互移到一个特殊的类(该类将处理它们并以更一般的方式公开控件和方法)中,采用质上不同的方法.也许在这里考虑一下控制器"在MVC范例中的作用.

此类问题中一个有趣的困境"是,为了连接"事件,进行表单交互:这些表单不仅需要通过新的Form#n()实例化. .但实际上已加载并显示.

在这种特殊情况下,您的设计显然要求在用户采取某些措施之前不显示表格.

因此,例如,即使您在Form3上创建了TextBox类型的公共属性,直到显示Form3,也无法在显示Form3之前访问实际的TextBox控件.而且,不,您不能像设置Form3.Visible = false那样使用技巧",然后再使用"Show"或"ShowModal".

但是,这是您可以做的...是的,最后是一个简单的解决方案,但带有一点黑臭味...
I suggest ''de-composing'' the dependencies here like this:

1. Form1 is the Main Form, the start-up Form.
2. Form2 is shown by Form1, but has no dynamic interaction with Form1
3. Form3 is shown by Form2, and changes in From3''s TextBox should be immediately transmitted to Form1''s Label. Form3 has no dynamic interaction with Form2

Note that you can view the relationship between Form1 and Form3 as either:

1. Form1 ''observes/monitors'' what happens in Form3, and updates itself. To ''observe'' Form1 needs:
a. a reference to give it access to Form3''s TextBox
b. a way to know when the Text on Form3''s TextBox changes.

2. or, Form3 ''publishes'' its TextBox text as it changes, and Form1 ''subscribes to the publishing event. For that to happen Form3 must define a ''publicly'' visible event that Form1 can subscribe to.

Finally, a qualitatively different approach could be taken by moving all the events, and interactions, between all Forms into a special Class which will handle them, and which will expose Controls and Methods in a more general way. Perhaps think here about the role of the ''Controller'' in the MVC paradigm.

One interesting ''dilemma'' in this type of problem is that in order to ''hook-up'' events, for Form interaction: those Forms need to be not just instantiated ... by new Form#n() ... but actually loaded and shown.

In this particular case, your design, evidently, demands the Forms not be shown until the user has taken some action.

So even if, for example, you create a public property of type TextBox on Form3, until Form3 is shown, you cannot access the actual TextBox control until Form3 is shown. And, nope, you cannot use a ''trick'' like setting Form3.Visible = false, and then using ''Show, or ''ShowModal.

However, here''s what you can do ... yes, at last, the simple solution, but with a slight hackish smell ...
// in class Form1 body
     Form2 f2;
     Form3 f3;

     private void Form1_Load(object sender, EventArgs e)
     {
         f2 = new Form2();
         f3 = new Form3();

         // note that setting Form2''s Visible
         // to false prior to Show will have
         // no effect, and the Form is shown

         // note that using ShowDialog here
         // will not work !
         f2.SuspendLayout();
             f2.Show();
             f2.Visible = false;
         f2.ResumeLayout();

         f3.SuspendLayout();
             f3.Show();
             f3.Visible = false;
         f3.ResumeLayout();

         // note that Form2''s Button will be null
         // until Form2 is actually shown !
         f2.f2Button.Click += new EventHandler(f2Button_Click);

         f3.f3TextBox.TextChanged += new EventHandler(f3TextBox_TextChanged);
     }

     private void button1_Click(object sender, EventArgs e)
     {
         f2.ShowDialog();
     }

     private void f2Button_Click(object sender, EventArgs e)
     {
         f3.ShowDialog();
     }

     private void f3TextBox_TextChanged(object sender, EventArgs e)
     {
         label1.Text = f3.f3TextBox.Text;
     }


将上述代码视为实现解决方案的一种方法,然后应该自己弄清楚Form2如何公开"其Button,而Form3如何公开"其TextBox.

从技术上讲,此解决方案将事件处理程序注入"到Form2和Form3中:要实现这一点,请记住,这些Form不仅必须由"new"实例化,还必须创建.

以我的拙见,这样的情况将带您进入一个边缘,在该边缘开始需要进行更一般的解决方案,而我的意思是将所有事件管理和Form交互形式化为一个类(可能是静态的),并将所有事件委托给该类然后路由"并调度"它们.

最好,Bill


Looking at the above code as one way to achieve a solution, you should then figure out for yourself exactly how Form2 ''exposes'' its Button, and Form3 ''exposes'' its TextBox.

Technically this solution ''injects'' event handlers into Form2 and Form3: for that to happen, keep in mind those Forms have to be not just instantiated by ''new,'' but created.

In my humble opinion, cases like this take you to the edge where a more general solution starts being worth the struggle, and by that I mean formalizing all event management and Form interaction into a class, which is probably static, and delegating all events into that class which then ''routes'' and ''dispatches'' them.

best, Bill


dude,如果您想将值从一种形式转移到另一种形式,则将标签值转移到另一种形式,然后再为该表单创建一个对象,则可以访问该标签
dude, if you want transfer a value from one form to another form make the label value in other to internal and then create an object for the form then you can access the label in this form.


这篇关于从C#中的多种形式调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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