Form databinding using list obj instead of biz obj

M

moondaddy

I have a form where I was using databinding to bind all of it's fields to a
business class. Now I'm converting it over to binding to a list object
which is a list of the same business objects.

Note: me.Me.CustName is a textbox in this form, "Me.CustName" is a property
in the business object, and oList is a reference to the list class which as
a collection of the business objects)

Me.CustName.DataBindings.Add("Text", oList, "CustName")

Rather than updating the business class with the new value in the textbox
(CustName) when it looses the focus and is forced to update, I want the
business class to update with every time the data is changed (as each letter
is keyed) so I added the event handler:

AddHandler CustName.TextChanged, AddressOf TextValChanged

and in that event Handler I used this line to pass the value back to the
business object:

objB.CustName= sender.Text

But now I'm binding to oList which is a list of objB and I cant use this
line:

oList.CustName= sender.Text

Question: When binding to oList, how can I update it (pass the current
value of CustName) with each key stroke?

Thanks.
 
J

Jeffrey Tan[MSFT]

Hi moondaddy,

Thanks for your post!!

Based on my understanding, you placed many Biz class objects in an list(I
suspect it is ArrayList? ), then you binds this list as datasource to the
TextBox, you want to get the function that: whenever the user pressed a
key, the datasource is updated.

For this issue, we can intercept TextBox's TextChanged event and force the
current edit to end. To get this done, we can use EndCurrentEdit method,
sample code snippet lists below:

private ArrayList al=new ArrayList();
private void Form1_Load(object sender, System.EventArgs e)
{
for(int i=0;i<5;i++)
{
Biz obj=new Biz();
obj.prop="Item"+i.ToString();
al.Add(obj);
}

this.textBox1.DataBindings.Add("Text", al, "prop");
this.textBox1.TextChanged+=new EventHandler(textBox1_TextChanged);

this.timer1.Interval=2000;
this.timer1.Start();
}

private void textBox1_TextChanged(object sender, System.EventArgs e)
{
this.textBox1.DataBindings["Text"].BindingManagerBase.EndCurrentEdit();
}

private void timer1_Tick(object sender, System.EventArgs e)
{
Console.WriteLine(((Biz)al[0]).prop);
}

public class Biz
{
private string _prop="abc";
public string prop
{
get
{
return _prop;
}
set
{
_prop=value;
}
}
}
It works well on my side. Note: in the sample, I used a Timer component to
dynamically get the datasource value, if we press a new character in the
textbox, we will see that the datasource is changed immediately.
=======================================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi moondaddy,

I am glad I can help you. If you need further help, please feel free to
tell me, I will work with you :). Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi moondaddy,

Have I misunderstood you? Do you still have unclear on the usage of timer
component in my sample code snippet? Just as I pionted out in last reply:
I used a Timer component to dynamically get the datasource value, so that
we can see the changes immediately. Please feel free to tell me your
concern. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
M

moondaddy

I dont understand or see how the Timer component dynamically gets the
datasource value.
 
J

Jeffrey Tan[MSFT]

Hi moondaddy,

Thanks for your feedback!!

Yes, I draged a Timer component from the ToolBox to the form designer. In
the Form1_Load event, I start the Timer component with the code below:
this.timer1.Interval=2000;
this.timer1.Start();

Then, in timer1_Tick event(yes, I add this event handler in Property
Browser), I print out the first item's "prop" property in the datasource
with:
private void timer1_Tick(object sender, System.EventArgs e)
{
Console.WriteLine(((Biz)al[0]).prop);
}
This is because our textbox databinding defaultly binds to the first item
in the ArrayList.

At last, please note that, we should run the sample code snippet in Debug
mode(Press F5 in VS.net), so that Console.WriteLine method will print the
string to the "Output" window in VS.net IDE.

If I misunderstand you or you still have anything unclear, please feel free
to tell me. Thanks
===========================================================
Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi moondaddy,

Does my last reply clarify the usage of Timer component now? Do you still
have any concern on this issue? Please feel free to feedback. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Ok, if you need further help, please feel free to post. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top