我在c#表单中遇到了继承问题 [英] i'am having trouble with inheritance in c# forms

查看:76
本文介绍了我在c#表单中遇到了继承问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的基类表单

here is my base class form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String str = textBox1.Text;
            textBox2.Text = str;
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            int a, b, c, d;
            a = Convert.ToInt32(textBox3.Text);
            b = Convert.ToInt32(textBox4.Text);
            c = Convert.ToInt32(textBox5.Text);
            d = (a + b + c) / 3;
            if (d <= 50 && d >= 40)
            {
                label1.Text = "A";
            }
            else if (d >= 30 && d <= 40)
            {
                label1.Text = "B";
            }
            else
                label1.Text = "C";
            label7.Text = Convert.ToString(d);

        }

        private void label1_Click(object sender, EventArgs e)
        {
            
        }

        private void label7_Click(object sender, EventArgs e)
        {

        }
    }
    
}



i想要从这个表单继承,所以我写了一个新的表单,继承自form1


i want to inherit from this form so i wrote a new form,which inherits from form1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace formstud
{
    public partial class Form2 : formstud.Form1
    {
        public Form2()
        {
            InitializeComponent();
        }
    }
}



但代码不能正常工作..我在c#的新手..所以请帮助我


but the code is not working.. iam newbie in c#.. so pls help me

推荐答案

当我开始调试时,派生的表格没有显示



嗯,不,它不会 - 除非你明确告诉系统显示表格的实例。



有两种方法来处理这个问题,他们两者都需要你在一定程度上知道发生了什么。首先在Visual Studio中打开项目,然后查看解决方案资源管理器窗格。 (如果你没有默认打开它,现在打开它并在屏幕右侧停放它 - 它是系统中最有用的一个)。

扩展你的项目分支,您将看到一个名为Program.cs的文件 - 双击它,它将打开以显示Main方法。这是启动应用程序的原因,您将看到它包含一行:

"the derived form is not showing when i start debugging"

Well, no, it won't - not unless you specifically tell the system to display an instance of the form.

There are two ways to handle this, and they both need you to know "what goes on" to a certain extent. Start by opening your project in Visual Studio, and look at the Solution Explorer pane. (if you don't have this open by default, open it now and "park it" on thr right hand side of your screen - it's one of the most useful in the system).
Expand your project branch, and you will see a file called "Program.cs" - double click on it and it will open to show you the "Main" method. This is what starts your application, and you will see it contains a line:
Application.Run(new Form1());



这是运行应用程序的行,并指定要显示的表单 - Form1类的新实例。



所以你可以做两件事:

1)简单路线:改变 Form1 in 运行行到 Form2

2)不那么容易的路线:改变你处理事物的方式:创建一个新的Form2,添加按钮等,然后将Form1定义更改为继承自Form2而不是Form。



我?我会采用第二种方法,因为它对我来说感觉更自然。



请更改你的所有名字!不要坚持使用Visual Studio的默认名称:Button1在您阅读代码时没有多大帮助; butCalculate使您的代码更加自我记录 - 文本框,标签等也是如此。养成从一开始就使用有用名称的习惯,以后会节省很多时间!


This is the line that runs your application and specifies which form to display - a new instance of the Form1 class.

So there are two things you can do:
1) The "Easy Route": Change the Form1 in the "Run" line to Form2
2) The "Not So Easy Route":Change the way you handle things: create a new Form2, add your buttons and so forth, then change the Form1 definition to inherit from Form2 instead of Form.

Me? I'd go with the second approach, because it feels more "natural" to me.

And please, change all your names! Don't stick with the Visual Studio default names for things: "Button1" doesn't help much when you read your code; "butCalculate" makes your code much more self documenting - and the same goes for textboxes, labels, and so forth. Get into the habit of using useful names right from the start, and it will save you a lot of time later!


这篇关于我在c#表单中遇到了继承问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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