点击事件处理程序问题

时间:2018-12-05 04:03:58

标签: c#

我正在制作一个快速应用程序,该程序将随机数显示在第二个Windows窗体上。我将其作为一个随机数生成器,就像从单击按钮事件处理程序中显示随机数一样。我在弄清楚如何在新的Windows窗体上显示随机生成的数字时遇到麻烦。我创建了两个表格。这是我的代码如下:

{
    public partial class Form1 : Form
    {
        Random rnd = new Random();
        int randomnumber;


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            randomnumber = rnd.Next(100);
            Form2 r2 = new Form2();
            r2.ShowDialog();

            MessageBox.Show( randomnumber.ToString()); 
// as you see, I displayed it to a MessageBox because
// I was having difficulty showing this value onto the second windows forum named Form 2 
        }
    }
}

// note, this is the code for the first form.

以及以下是第二种形式的代码:

{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void label5_Click(object sender, EventArgs e)
        {


        }
    }
}

1 个答案:

答案 0 :(得分:2)

选项01

您可以在Form2中创建一个将值分配给所需Label控件的方法。

import Vue from 'vue'    
import NewComponent from './some-file-path/some-file-name.vue'

Vue.component('NewComponent', NewComponent) // register the component

new Vue({
    el: '#example'
}) // initiate a Vue instance

然后在生成随机数之后,可以使用该方法分配值。

public void AssignRandomNumber(int randomNumber)
{
    label5.Text = randomNumber.ToString();
}

选项2:

您可以对Form2的构造函数进行相同的操作

 randomnumber = rnd.Next(100);
 Form2 r2 = new Form2();
 r2.AssignRandomNumber(randomnumber);
 r2.ShowDialog();

在这种情况下,您在Form1中的代码看起来像

public Form2(int randomNumber)
{
    InitializeComponent();
    label1.Text = randomNumber.ToString();
}
相关问题