Click here to Skip to main content
15,904,153 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
here is my base class form
C#
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 want to inherit from this form so i wrote a new form,which inherits from form1
C#
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();
        }
    }
}

but the code is not working.. iam newbie in c#.. so pls help me
Posted
Updated 14-Jun-14 5:50am
v2
Comments
[no name] 14-Jun-14 8:59am    
What is it that you think "is not working" means?
Member 10884190 14-Jun-14 9:02am    
the derived form is not showing when i start debugging
[no name] 14-Jun-14 9:18am    
Since there is no code here that shows Form2, why would you expect it to?
khurram ali lashari 14-Jun-14 11:23am    
Why there are two Button Click Event of same Button Which was button1 in Form 1 ????????
OriginalGriff 14-Jun-14 11:40am    
Probably, he created a "Button1", added a Click handler then deleted the button later. When he then adds another button, it also gets called "Button1" but since a method exists called "button1_Click" VS will create a new method called "button1_Click_1" when you double click the new button.

1 solution

"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:
C#
Application.Run(new Form1());

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!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900