C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » .NET »

Dynamic Controls


Posted Date: 06 Nov 2009      Posted By: sirisha      Member Level: Silver     Points: 1   Responses: 7



Hi Everyone.
Please anyone help me. My problem is that
I have a form in which i created 4 dynamic textboxes and add, save buttons. When i enter text in the textboxes and click on the save button, the text in the textboxes has to be added to access database. All the ctrls are created dynamically. This is a windows appln in vb.net.
I have searched many tutorials but could find this only in asp.net. Pls anyone give me some sample coding in vb.net





Responses

Author: Pannalal Sinha    06 Nov 2009Member Level: GoldRating: 2 out of 52 out of 5     Points: 2

When creating the textboxes dynamically, provide names to each one e.g., TextBox1, TextBox2, TextBox3, TextBox4.

In the save button click event handler, use this names...

strQuery="Insert into table1 (col1,col2,col3,col4) values ('"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"','"+TextBox4.Text+"')"


Thanks
Panna



Author: sirisha    06 Nov 2009Member Level: SilverRating: 2 out of 52 out of 5     Points: 2

Hello Sinha. Thans for ur quick reply.
I have given name for 1st textbox as TxtName. When i try to use this in the query, its giving me an error saying that it is not declared.



Author: Pannalal Sinha    07 Nov 2009Member Level: GoldRating: 2 out of 52 out of 5     Points: 2

you can generate dynamic textbox like this.

TextBox TextBox1= new TextBox();
TextBox1.Name = "TxtName";
this.flowLayoutPanel2.Controls.Add(TextBox1);

//now you can use it in your query


Thanks
Panna



Author: sirisha    07 Nov 2009Member Level: SilverRating: 2 out of 52 out of 5     Points: 2

I have added everything according to your suggestions.But still in the insert query

strQuery="Insert into table1 (col1,col2,col3,col4) values ('"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"','"+TextBox4.Text+"')"

it insists me saying that textbox1,textbox2,textbox3,textbox4 is not declared



Author: Pannalal Sinha    07 Nov 2009Member Level: GoldRating: 4 out of 54 out of 54 out of 54 out of 5     Points: 6

sorry i was wrong little bit.

I assume you are adding the textboxes in a flowlayoutpanel.
Then

void savebutton_click()
{
string value1="";
string value2="";
string value3="";
string value4="";

for(int i=0;i<flowLayoutPanel2.Controls.Count;i++)
{
switch(flowLayoutPanel2.Controls[i].Name)
{
case "TextBox1":
value1=flowLayoutPanel2.Controls[i].Text;
Break;

case "TextBox2":
value2=flowLayoutPanel2.Controls[i].Text;
Break;

case "TextBox3":
value3=flowLayoutPanel2.Controls[i].Text;
Break;

case "TextBox4":
value4=flowLayoutPanel2.Controls[i].Text;
Break;
}

}
strQuery="Insert into table1 (col1,col2,col3,col4) values ('"+value1+"','"+value2+"','"+value3+"','"+value4+"')"


}



Author: sirisha    07 Nov 2009Member Level: SilverRating: 2 out of 52 out of 5     Points: 2

ThankU so much Sinha. It worked.But i am still going to trouble u. I used the flow layout panel. How to set the controls position in this.


Author: Pannalal Sinha    07 Nov 2009Member Level: GoldRating: 2 out of 52 out of 5     Points: 2

Sirisa,
FlowLayOutPanel doesn't provide the facility of positioning the controls at a specific location. However,it has Flow Direction property which can be used according to your requirement.

I recommend you to visit the following links.

http://www.yevol.com/en/vcsharp/controls/flowlayoutpanel.htm
http://msdn.microsoft.com/en-us/library/z9w7ek2f(VS.80).aspx

Thanks
Panna



Post Reply
You must Sign In to post a response.
Next : Image in a listview column click
Previous : Query
Return to Discussion Forum
Post New Message
Category: .NET

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use