Dynamic Buttons

  • Thread starter Thread starter MikeY
  • Start date Start date
M

MikeY

Hi Everyone,

Does anyone know where I can get my hands on a sample with source code of a
simple dynamic button control in C# Windows form. I am looking for a sample
that uses a class library that sets the properties send/passed from the main
windows form. I'm having problems with the class library, the button control
collection and my referencing it ie this.Control.Add(aControl);.

Any and all help is appreciated. Thanks in advance.

MikeY
 
simple dynamic button control in C# Windows form

What is a simple dynamic button control? What does it do that is "dynamic"?
 
Hi Dave,

Its where you create your own buttons or button control, instead of dragging
and dropping the premade button from your component panel. Using the buttons
this way we take adantage of polymorphism.

MikeY
 
Ok, so your question is about design-time interoperability with VS.NET and a WinForms Button derived class?

Are you familiar with custom Designers and Editors? What is the particular problem you are having trouble with?

A simple code example of what your trying to do would help people to understand what your after. There's alot of resources out
there, and to determine the most appropriate more information about your issue is required.

Here's a link to get you started:

http://msdn.microsoft.com/library/d...l/cpconwinformscontrolssamples.asp?frame=true
 
Hi Dave,

Yes I think that's my question. My problem does lay within design-time. I am
not familiar with Designers or Editors. My source code follows then I'll
detail the problem.


********************

using System;
using System.Drawing;
using System.Collections;
using System.Component.Model;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace Test
{
public frmMain()
{
InitializeComponent( );
AddControl(new Button( ), new Point(5,5), new Size(75,75), "ButtonName",
0, "");
}
}


********************


using System;

namespace Test
{
public void AddControl(Control aControl, Point Location, Size Size, String
strText, int TabIndex, string strName);
{
aControl.Location = Location;
aControl..Size = Size;
aControl.Text = strText;
aControl.TabIndex = TabIndex;
aControl.Name = strName;
this.Controls.Add(aControl);
}
}

********************

When I put the "AddControl" function within the "frmMain.Test Class" I
recieve no problems at all. When build a Class Library and put the function
in there (for better OOPS) I get the following error: "Test.AddControl" does
not contain a definition for Controls. Hmm

I will look at the link you have provided. If you can think or suggest a
solution I would be much appreciated.

Thanks Dave,

MikeY
 
In C#, and for all I know every other managed language, you must create a class to hold methods. In other words, change your
library code to the following:

namespace Test
{
public class WinFormsCommon
{
// Notice the use of the "static" keyword which makes this method
// accesible without requiring an instance of "WinFormsCommon".
public static void AddControl(Form form, Control aControl, Point Location, Size Size, String
strText, int TabIndex, string strName)
{
// Todo: implemenation

// form has been passed in to this method as a parameter
// because you need a reference to the ControlCollection that "aControl"
// will be added to:
form.Controls.Add(aControl);
}

// This is a private constructor. It is declared to prevent instances of
// "WinFormsCommon" from being created.
private WinFormsCommon() { }
}
}

Note, in 2.0 you can declare static classes, which act the same as VB "modules".

Consume the code above in main:

public frmMain()
{
InitializeComponent( );

// Initialize and add a control to the form:
// (Notice the use of the "this" keyword, which passes an instance of the form
// that has a ControlCollection to add the "new Button()" to.)
WinFormsCommon.AddControl(this, new Button( ), new Point(5,5),
new Size(75,75), "ButtonName", 0, "");
}

When build a Class Library and put the function in there (for better OOPS)

This is not OOP. It definately is not Polymorphism. Instead, it's using a method of a static library. This is no different than
in languages that are not OO.

C# syntax and grammar reference on MSDN:

http://msdn.microsoft.com/library/?...tstart/html/cpcsvbvcjscript_c_.asp?frame=true

Polymorphism intro:

http://msdn.microsoft.com/library/d...ementinginterfacesincomponents.asp?frame=true
 
Thank you very much Dave for the solution, and for watching this post. The
solution works and seems to sold my problem. Thank you also for the links
looks like I have some reading ahead, lol.

Much Appreciated,

MikeY
 
Back
Top