BeginInvoke

  • Thread starter Thread starter Juan Carlos
  • Start date Start date
J

Juan Carlos

Hi I need to create dinamically checboxes in a Window form
but I get this exception:

Controls created on one thread cannot be parented to a
control on a different thread.

How can I use the BeginInvoke method to solve this
problem???

Regards
 
Hi Juan,
You can use form's Invoke method to send the execution in the UI thread and
let a delegate to do the job of creating the comboboxes.

HTH
B\rgds
100
 
* "Juan Carlos said:
Hi I need to create dinamically checboxes in a Window form
but I get this exception:

Controls created on one thread cannot be parented to a
control on a different thread.

How can I use the BeginInvoke method to solve this
problem???

Add a procedure to, for example, a form in the UI thread. This
procedure creates and adds the new controls. You can call the function
by using the form's 'Invoke' method.
 
Hi, thank a lot for answer me.

I did this:

First I created a delegate method
public delegate void creaControles(OdbcDataReader
odrDelegado);

And a method that creates checkbox controls

public void Crear(OdbcDataReader odrCrear) {
while(odrCrear.Read()) {
CheckBox chkBox = new CheckBox();
chkBox.Name = "chk" + i;
chkBox.Text = odrCrear.GetInt32(0).ToString();
chkBox.Location = new System.Drawing.Point
(coordx,coordy);
this.Controls.Add(chkBox);
i++;
coordy+=5;
}
odrCrear.Close();
}
coordx, coordy and i are global variables

Second I created an object of this method
creaControles obj = new creaControles(Crear);

and then I called the Invoke method
this.Invoked(obj);

But I get this exception:
Parameter count mismatch.

What's wrong??

Can you give an example please?

Best regards
 
Hi, thanks a lot for answer me.

I did this:

First I created a delegate method
public delegate void creaControles(OdbcDataReader
odrDelegado);

And a method that creates checkbox controls

public void Crear(OdbcDataReader odrCrear) {
while(odrCrear.Read()) {
CheckBox chkBox = new CheckBox();
chkBox.Name = "chk" + i;
chkBox.Text = odrCrear.GetInt32(0).ToString();
chkBox.Location = new System.Drawing.Point
(coordx,coordy);
this.Controls.Add(chkBox);
i++;
coordy+=5;
}
odrCrear.Close();
}
coordx, coordy and i are global variables

Second I created an object of this method
creaControles obj = new creaControles(Crear);

and then I called the Invoke method
this.Invoked(obj);

But I get this exception:
Parameter count mismatch.

What's wrong??

Can you give an example please?

Best regards
 
Hi Juan,
and then I called the Invoke method
this.Invoked(obj);

Invoke receives delegate as a parameter as well as the parameters for this
delegate. Your delegate expects one parameter *OdbcDataReader odrCrear* you
have to specify it in the Invoke call.

something like

creaControles obj = new creaControles(Crear);
OdbcDataReader odbcReader = <Create reader>;
this.Invoked(obj, new object[]{odbcReader});

HTH
B\rgds
100
 
Thanks it works, but now I have this problem

What's wrong in this code

while(odrCrear.Read()) {
CheckBox chkBox = new CheckBox();
chkBox.Name = "chk" + i;
chkBox.Text = odrCrear.GetInt32(0).ToString();
chkBox.Location = new System.Drawing.Point (100,coordy);
this.Controls.Add(chkBox);
i++;
coordy = coordy + 2;
}
odrCrear.Close();

The odrCrear object has 27 elements, but

Why does the code only create one checkBox???

Regards
 
Hi Juan,
Your code is OK it creates 27 check boxes, but it stacks them one on the top
of the other. You can see only the top one, which is the first you add to
the controls collection.

Change the following line on something like
coordy = coordy + chkBox.Height;
so you can see the check boxes.

HTH
B\rgds
100
 
Back
Top