Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   cration of a new control, problem with adding new controls (http://www.programmingforums.org/showthread.php?t=15620)

beni_dude Apr 15th, 2008 8:05 AM

cration of a new control, problem with adding new controls
 
Hello,
I have a problem when I try to add a new control to the panel control, I'm sure its a newbie problem but still I can't go around it!

What I'm trying to do is to create a new control which well be only one panel control and in it twelve text box (array of text box).

I'm also adding the code

Quote:

public partial class MonthPick : Control
{
public const int arrSize = 12;

public MonthPick()
{
InitializeComponent();

TextBox[] boxArr = new TextBox[arrSize];
initBoxArr(boxArr, arrSize);

//the problem is with the next line
panel1.Controls.Add = boxArr;
}

private void initBoxArr(TextBox[] boxArr, int arrSize)
{
for (int i = 0; i < arrSize; i++)
{
boxArr[i] = new TextBox();
}
}

protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}


}
I just don't know how to add the array of text box's

Thanks :icon_wink:

beni_dude Apr 15th, 2008 8:40 AM

Re: cration of a new control, problem with adding new controls
 
The error that I receive is this:

Quote:

Cannot assign to 'Contains' because it is a 'method group'

beni_dude Apr 15th, 2008 10:19 AM

Re: cration of a new control, problem with adding new controls
 
I just might figured it by myself I just used this code:

Quote:

panel1.Controls.Add(textBox1);
But now I have something new, I need to see the text box (in the new command that I have created) and that I don't know how to do.

beni_dude Apr 15th, 2008 11:22 AM

Re: cration of a new control, problem with adding new controls
 
I just want to create a control with 12 other controls I can't believe its so hard

lectricpharaoh Apr 15th, 2008 4:07 PM

Re: cration of a new control, problem with adding new controls
 
Are you trying to actually create a new control (ie, a custom control), or are you just trying to stick a bunch of stuff onto a panel? If it's the former, it's a little involved, particularly if you want design-time support (support in the drag-and-drop 'forms designer'). If it's the latter, you can just add them in the designer. If you need to iterate through them with array notation, you add the TextBoxes (or whatever) in the designer, and give them descriptive names (not necessary, but a good habit to get into). Then, in your code, you create an array of TextBoxes, and assign the references:
:

  1. TextBox[] textBoxArray = new TextBox[numberOfTextBoxes];
  2. textBoxArray[0] = txtInput;
  3. textBoxArray[1] = txtOutput;
  4. textBoxArray[2] = txtSomethingElse;
  5. // and so on

Now you can process them in a loop, which is probably why you want to use an array:
:

  1. for(int x=0; x<textBoxArray.Length; ++x)
  2.   textBoxArray[x].Text = x.ToString();

Remember that even though you can create an array of controls, you still need to initialize each element of the array appropriately. The designer is the easiest way to do this; otherwise, you need to set the width, height, location, etc of each one individually.

beni_dude Apr 15th, 2008 4:31 PM

Re: cration of a new control, problem with adding new controls
 
First thing thanks for the Replay!

Second yes I'm trying to create an custom control which is a bit of a problem.
Now I did manage to create the array of the controls (in this case array of text box) but WHEN I create it in the panel and make sure to add the text box array I get only one text box, I think that its actually the twelfth box's in the same position so now I need to move them in the panel so they won't be on the top of each other.

If some one can come up with a simple clear cut way it will be great!


All times are GMT -5. The time now is 4:01 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC