在c#中的事件处理程序中定义了变量后,如何访问事件处理程序外部的变量? [英] How to access a variable outside an event handler when it is defined within the event handler in c#?

查看:80
本文介绍了在c#中的事件处理程序中定义了变量后,如何访问事件处理程序外部的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我在C#Windows Forms应用程序中的一些代码.

Below is some of my code in a c# windows forms application.

它分配一个随机整数来处理.我如何访问handle的新值,以便此按钮事件之外的其他函数调用可以使用它,因为c#中没有全局变量?

It assigns a random Integer to handle. How can I access the new value of handle so it can be used by other function calls outside this button event since there is no global variables in c#?

我尝试使用方法,但是当我在按钮事件之外调用它时,它仍然无法识别句柄.

I've tried using a Method but it still wont recognise handle when I call it outside the button event.

我还能用什么来制作我的GUI,以便在按下此按钮之前不能使用其他一些控件?

Also what would I use to make my GUI that you cannot use some of the other controls until this button is pressed?

  private void button1_Click(object sender, EventArgs e)
    {
      Int handle = 0;
      random(ref handle);
    }

推荐答案

    public class MyClass
    {
            private int handle;

            private void button1_Click(object sender, EventArgs e)
            {
                handle = 0;
            }

            private void button2_Click(object sender, EventArgs e)
            {
                GetHandle();
            }

            private int GetHandle()
            {
                return handle;
            }
    }

这篇关于在c#中的事件处理程序中定义了变量后,如何访问事件处理程序外部的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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