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 WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public class NewLabel: Form1 { public NewLabel() { Label test = new Label(); test.Text = "This is a label"; test.Location = new Point(100, 100); test.Visible = true; this.Controls.Add(test); } } private void CreateLabelButton_Click(object sender, EventArgs e) { NewLabel TestLabel = new NewLabel(); } } } |
When I create an instance of my class, the constructor method should be called and create a new label and location (100,100). The problem is the label does not show on the form.
I just started C# a few weeks ago and would like to know where the problem is in my code.
Thanks.
Aaron ColenPosted Sep 28, 2010, 1:58 PM
So anything you do to 'sender' gets done to the calling form (or whatever form you put in Label.Add(YourFormHere)).
Edward HanPosted Sep 28, 2010, 1:53 PM
Although I don't quite understand the concept behind adding "Form sender" to the method parameter but I'll try to do some research over the internet.
Thanks again guys.
Aaron ColenPosted Sep 28, 2010, 1:18 PM
public partial class Form1 : Form
{
public class NewLabel : Form1
{
...
}
...
}
You want to separate your classes. Into new files when possible.
Form1.cs:
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
NewLabel x = new NewLabel();
NewLabel.Add();
....
}
}
NewLabel.cs
namespace WindowsFormsApplication2
{
public class NewLabel
{
public void Add()
{
...
}
}
}
Of course what you're trying to do is probably better off as a static class.
public static class Label
{
public static void Add()
{
}
}
Then you call 'Label.Add()' without needing an instance of it.
edit: Forgot to mention. When referencing the Form externally you will need a reference to it.
public static void Add(Form sender)
{
Label x = new Label();
sender.Controls.Add(x);
}
And call it with 'Label.Add(this)' from the main form.
Edward HanPosted Sep 28, 2010, 1:03 PM
Using the "Visible" method and "Show" method both show a new form when I press the button.
Thanks for your help Aaron. Indeed, your approach works. However, as my windows application gets more complex, I realized that I am adding too many methods to Form1 class and it's hard to keep track of them all.
I come from a C/C++ background and I am just wondering why the object-oriented approach does not work as you've seen in the third post of this thread.
In a console application, I can have multiple classes I created and they work but in a windows application, I can't seem to have any of my self-created classes working.
Maybe the way I'm programming is wrong as I am very new to C#. Can anyone clarify this to me?
Aaron ColenPosted Sep 28, 2010, 12:34 PM
NewLabel : Form1 / Form1 : Form
NewLabel inherits Form1 which inherits Form. This makes each instance of NewLabel a form all on it's own.
The code you have inside of 'public NewLabel()' adds a Label to the new form NewLabel (Label test = new Label(); and this.Controls.Add(test);).
Remove the extra 'public class NewLabel : Form1' and the change the CreateLabelButton to 'NewLabel()' ie just calling the function.
Mike GoldPosted Sep 28, 2010, 10:22 AM
e.g.
private bool myformShowing = false; // as a class field
...
if (myformShowing == false)
{
test.Show();
myformShowing = true;
}
else
{
test.Visible = true;
}
Edward HanPosted Sep 27, 2010, 11:10 PM
Mike GoldPosted Sep 27, 2010, 9:34 PM
test.Show();
Edward HanPosted Sep 27, 2010, 7:47 PM
My question is, why these codes work for console application:
namespace Class
{
class Program
{
static void Main(string[] args)
{
myclass test = new myclass();
Console.ReadKey(true);
}
}
class myclass
{
public myclass()
{
Console.WriteLine("myclass works");
}
}
}
and why these codes do not work in windows application:
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 WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
myclass test = new myclass();
}
}
public class myclass : Form1
{
public myclass()
{
Label label1 = new Label();
label1.Visible = true;
label1.Location = new Point(100,100);
label1.Text = "myclass works";
}
}
}
When I create a "myclass" instance in form1 class, I should invoke its constructor. But it is not working.
Mike GoldPosted Sep 27, 2010, 5:22 PM
First You don't need to create a new label because C# already has a built in class for this.
2nd of all, if you are going to create a new label class, you don't want to inherit it from your form because a label is not a form.
the better strategy if you want to create your own label class (if you have to, and that is only if you are providing some new functionality that the label doesn't already have), is to inherit from the Label class already built into windows. And you don't need to nest this class unless your form is the only form that will ever use the Label class.
http://msdn.microsoft.com/en-us/library/system.windows.forms.label.aspx
e.g.
public MySpecialLabelThatDoesSomethingWindowsFormCant : Label
{
public MySpecialLabelThatDoesSomethingWindowsFormCant ()
{
}
// add overrides here
}