[FIXED]Datarepeatercontrol leave event object sender changing!
Hello,
my 1st post :)
I am using visual basic power pack controls and Net 3.5 with the Datarepeatercontrol in c#.
i have it working great!! except for this weird thing i cant figure it out...
i was able to Try and catch the exception and it goes on working unnoticed (and even passes the data????) but i want to fix the exception instead of hiding it.
I have the datarepeater control with a tableLayoutPanel then a textbox on that.
in the textbox textbox_leave event i have the following code:
Code:
private void txtdata_Leave(object sender, EventArgs e)
{
try
{
dtsource.Rows[CurrentRow][intColIndex] = ((TextBox)sender).Text;
((DataTable)rptrData.DataSource).Rows[intColIndex]["ColumnData"] = ((TextBox)sender).Text;
}
catch
{
;
}
so basically it updates my datatable when i leave the textbox.
if i leave the texbox to anything on the repeater data control, it doesnt show an error
but if i go to click something outside of the window, like a menu item or button outside of the data repeater control area i get a
"Unable to cast object blah blah visual basic powerpack to System.textbox"
so i click continue and it still works!! the data is still updated into the table...
so for a quick fix i put in the try and catch and it works.
but i want to fix it right...
it looks like it sending 2 different objects as the sender?? hmmmm i dunno how to fix it.
please help !!
thanks
Re: Datarepeatercontrol leave event object sender changing!
Re: Datarepeatercontrol leave event object sender changing!
Quote:
Originally Posted by
drex
"Unable to cast object blah blah visual basic powerpack to System.textbox"
The "blah blah blah" part of that exception is the important bit ;). It is telling you that 'sender' is a "blah blah blah" and not a TextBox. I would bet that you have some code like this:
Code:
TextBox txtData.Leave += new EventHandler(txtData_Leave);
"blah blah blah" myBlahBlah.Leave += new EventHandler(txtData_Leave);
The runtime is not lying to you. If it says that sender is not a TextBox, it's not. It is good that you do not want to 'fix' this by swallowing errors.
Re: Datarepeatercontrol leave event object sender changing!
Hello
thanks for answering!
nope no code like that.
thats the only event handler for that textbox.
its becuase it only says its not a textbox sender when i click outside the datarepeater control (like a button or somewhere) that it throws this exception.
inside the datarepeater control when i leave the textbox, no exception thrown.
i thoughtwhen u leave the textbox you are leaving the textbox and the sender would always be the textbox item??
but in this case it is not.
it is changing depending on wether i click inside or outside of my data repeater control.
any more insite??
the problem is, i dont have a way to referencea textbox inside a tablelayoutpane inside a datarepeaterITEM..... hmmmmm... playing with objects is not fun in this application!!
Re: Datarepeatercontrol leave event object sender changing!
I can guarantee that you are handling the event of that control. Can you post your code please, including your designer file?
Re: Datarepeatercontrol leave event object sender changing!
WOW i found it!! but hmmm im not sure if its supposed to be there or not???
there are 2 events for it, and im didnt put the datarepeater leave event there??
its not listed in the events tab of the repeater...(does it inherit its child events?)
heres the code i found and now i see why theres 2 objects!!
Code:
// txtdata
//
txtdata.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
txtdata.CausesValidation = false;
txtdata.Location = new System.Drawing.Point(192, 3);
txtdata.Name = "txtdata";
txtdata.Size = new System.Drawing.Size(658, 20);
txtdata.TabIndex = 2;
txtdata.Leave += new System.EventHandler(this.txtdata_Leave);
txtdata.Enter += new System.EventHandler(this.txtdata_Enter);
//
and the 2nd reference to txtdata_leave
Code:
// rptrData.ItemTemplate
//
this.rptrData.ItemTemplate.BackColor = System.Drawing.Color.Gainsboro;
this.rptrData.ItemTemplate.CausesValidation = false;
this.rptrData.ItemTemplate.Controls.Add(this.tblData);
this.rptrData.ItemTemplate.Size = new System.Drawing.Size(952, 26);
this.rptrData.Location = new System.Drawing.Point(32, 39);
this.rptrData.Name = "rptrData";
this.rptrData.Size = new System.Drawing.Size(960, 581);
this.rptrData.TabIndex = 0;
this.rptrData.Text = "dataRepeater1";
this.rptrData.DrawItem += new Microsoft.VisualBasic.PowerPacks.DataRepeaterItemEventHandler(this.rptrData_DrawItem);
this.rptrData.Leave += new System.EventHandler(this.txtdata_Leave);
//
see them?? they both have the same event...
so its passing 2 diff objects based on where i click inside or out.
now i didnt add it, and it doesnt appear in the events tab under datarepeatercontrol.
so does this mean that the parent (the data repeater control is the parent of tablelayout which is the parent of the textbox in it) inherits the child's leave event automatically???
so i can either:
a. delete the datarepeater leave event handler (manually)
b. handle the two objects separately in the function based on the object sender type.
and i think B. is the wrong answer since its probably calling it twice.
so i think ill try A and see what happens.
thanks for your help!! but yah shed some light on how that event handler got in there in the 1st place :)
Re: Datarepeatercontrol leave event object sender changing!
removed the 2nd event handler from datarepeater control and it works!!!
who knows how it got in there... hmmmm...
thanks for the great help...
this is a great control for those of you who dont know about it.
dont let the visual basic powerpacks on the front fool you :)
Re: Datarepeatercontrol leave event object sender changing!
I would bet that you accidentally added it through the designer. You probably had both controls selected when you added the handler. Glad it's all figured out ;)