有关C#中代码的问题 [英] Question about code in C#

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

问题描述

我有2种表格

第一个在他的页面上包含一个面板(大小= 400X400)
和一个打开第二个表单的按钮
包括2个文本框和一个按钮

用户需要输入宽度和高度,然后按按钮

我希望用户输入的大小可以更改第一个(主)表单上的面板,而无需创建另一个form1(主)对象?

I have 2 forms

the first includes on his page a panel (Size = 400X400)
and a button that opens the second form
that includes 2 textbox and a button

the user need to enter Width and Height and then press the button

I want that the size entered by the user will change the panel on the first (main) form without creating another object of form1 (Main) ?

form 1 - Main - Panel + Button<br />
form 2 - .... - 2 textboxs + button<br />
<br />
form 1 (Main) - code --> <br />


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 Form1 : Form
    {
        private int X2, Y2;
        public Form5(int W2, int H2)
        {
            X2 = W2;
            Y2 = H2;
            InitializeComponent();
            panel1.Size = new Size(X2, Y2);
        }

        private void Enter(object sender, EventArgs e) /->Button Event<-\
        {
            Form6 form2 = new Form2();
            form2.Show();
        }
    }
}


form2-代码->


form2 - code ->

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 Form6 : Form
    {
        public Form6()
        {
            InitializeComponent();
        }

        private void BtnClick(object sender, EventArgs e) /->Button Event<-\
        {
            int W2 = int.Parse(textBox1.Text);
            int H2 = int.Parse(textBox2.Text);
            Form5 form5 = new Form5(W2, H2);
            form5.Show();
        }
    }
}


请使用文本框帮助???


Please Help ???

推荐答案

Form2:

Form2 with the textboes:

namespace WindowsFormsApplication1
{
    public partial class Form6 : Form
    {
        private int panelWidth;
        private int panelHeight;

        public int PanelWidth
        {
            return panelWidth;
        }

        public int PanelHeight
        {
            return panelHeight;
        }


        public Form6()
        {
            InitializeComponent();
        }

        private void BtnClick(object sender, EventArgs e)
        {
            panelWidth = int.Parse(textBox1.Text);
            panelHeight = int.Parse(textBox2.Text);

            Close();
        }
    }
}




表格1与面板:




Form 1 with the panel:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        private void Enter(object sender, EventArgs e) /->Button Event<-\
        {
            Form6 form2 = new Form2();
            form2.ShowDialog();

            panel1.Size = new Size(form2.PanelWidth, form2.PanelHeight);
        }
    }
}


您可以注入另一个对象以保留引用.

You can inject another object to hold onto the references.

public class PanelSize
{
   public int Width {get; set;}
   public int Height {get; set;}
}




在这里,我们在表单中有一个持有人,用于接收输入.




Here we have a holder for it in the Form that is receiving inputs.

public PanelSize Dimensions {get; set;}



将点击事件更改为以下内容:



Which changes the click event to be this:

private void BtnClick(object sender, EventArgs e) /->Button Event<-\
{
    Dimensions.Width = int.Parse(textBox1.Text);
    Dimensions.Height = int.Parse(textBox2.Text);
}



然后,您可以执行ShowDialog而不是执行Show().然后,在ShowDialog()运行之后,您可以捕获该值并进行设置.如果需要Show()起作用,则需要执行一些事件触发或实际绑定.

这是您使用ShowDialog
的方法



Then rather than doing a Show() you can do a ShowDialog. Then after the ShowDialog() runs you can capture the value and set it. If you require a Show() to work you will need to do some event triggering or actual binding.

Here is how you would do with ShowDialog

public PanelDimensions Dimensions {get; private set;}

private void Enter(object sender, EventArgs e)
{
   //Set up the PanelDimensions object that is to be injected
    Dimensions = new PanelDimensions();
    Form6 form2 = new Form2();
    form2.Dimensions = Dimensions; //This means both are now sharing this object and it is a class so it is by reference, i.e. same data
form
    form2.ShowDialog();
    
    panel1.Width = Dimensions.Width;
    panel1.Height = Dimensions.Height;
}


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

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