Windows Forms question

  • Thread starter Thread starter MounilK
  • Start date Start date
M

MounilK

Hi all,
I am not sure if this is the right NG to post this question. If
that's the case please point me to the correct NG.

I have a class library(myLib.dll) which has a class(myClass) which has a
method(myClassMethod) that creates a form with a button on it. The
application that calls the dll is a windows application(myWinApp).

When in myWinApp I do, myClass.myClassMethod, a form is created.

What I would like to know is, how can i access the click event of the form
that has been just created with the above call.

Thanks,
Mounil.
 
What do you mean "access the click event"? Do you mean attach a handler to
it or something?

Well, you might need to change your architecture a little. Maybe the method
should return a reference to the form.
 
Hi Peter,
Thanks for replying.Here's my code snippets

in the class library:-

public class MyClass
{
public void MyClassMethod()
{
frmParameterForm = new System.Windows.Forms.Form();
System.Windows.Forms.Button myButton = new
System.Windows.Forms.Button();

frmParameterForm.Controls.Add(myButton);
frmParameterForm.ShowDialog();
}
}


in the windows application WinApp:-

in the form1's button1's click event:

myClass cls = new myClass();
cls.MyClassMethod();


What I would like to know is how can i now handle the click event of the
form that was created by the cls.MyClassMethod call?
I am pretty new to this, so please excuse me if i sound naive.

Thanks,
Mounil.
 
After creating the button in MyClassMethod() constructor, add an
eventhandler to the button:

this.myButton.Click += new System.EventHandler(this.myButton_Click);

Then, you must define also in your class the myButton_Click event, so
as the eventhandler knows it. Do it this way:

private void myButton_Click(object sender, System.EventArgs e)
{
// put code to execute after myButton is clicked
}

This is the way VS.NET designer also works when you double click a
button in a VS.NET's designer mode.

Why are you using this "strange" approach of building forms through a
separate DLL? Any special requirements?

Hope it helps. Regards.
 
Well, first, you're going about this all wrong. You have a class that
contains a form. You should create a class that IS a form, that is, inherits
System.Windows.Forms.Form. There is no reason to put the form inside a
class. You're putting a container inside a container. The class that holds
the Windows Form class does nothing. Imagine, for example, that it's
Christmas. Your brother gives you a presnt, a nicely wrapped box. You tear
off the wrapping and open the box. Inside it is another box. You open that
box, and your present is inside it. Why did your brother put the box with
your present in it inside another box?

it sounds to me like you're no well-acquainted with Object-oriented
programming. When you inherit a class, you are creating a new class that has
all the characteristics of the old class, but with additional
characteristics you define. So, you don't put a form inside a class. You
inherit a Form class and write your own implementation of it.

Once you've done that, the Dialog form you've created handles the button's
click event itself. After all, the button is a member of the form. If you
want to do something with the data in the Dialog form in the form that opens
it (by calling the ShowDialog method of the form, rather than having the
form call its own ShowDialog method), you expose that data via a public, or
internal property or method.

If you are serious about using .Net, study up on object-oriented programming
principles, They make .Net powerful, and easy to use, if you employ them
correctly.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Big things are made up of
lots of little things.
 
Hi,


You are not hooking the Control.Click event, do that and you will be fine.

Also I recommend you to create your own form with its controls and stuff,
you can visually design it as a regular windows form even if your project is
a dll


cheers,
 
Hi,

No really, it's valid that a class contains a form in that way, it's even
possible to construct the form dynamically as the OP is doing it, that's why
Form.Controls is a public property and not a protected one.

Now, I do agree with you (and I said the same in the other post) that unless
needed adding the controls to a form through code is not the best idea, if
for any change the form change you could have to change lot of code all
aournd the place.
A better approach is to create your own form derived's class as you
suggest.

cheers,
 
Hi Ignacio,
As per your recommendations, I added a form to the dll.
now,

I have a DLL(myLib.dll) in which I have a form (myForm) On
myForm, I have a TabControl (myTabCtrl). I am creating a new tab-pages on
myTabCtrl at runtime; and on the tab-pages I am adding a DateTimePicker
control; On myForm I also have a command button (cmdClick). After the user
has selected the date on the DateTimePicker control and then clicks
cmdClick, I want to display a messagebox with the values of each
DateTimePicker on the various tab-pages. How do I do that? Pls help.

Thanking you,
Mounil.
 
Back
Top