Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Dynamically add controls to the Form - VB.Net / VS 2005

Posted on 2010-08-23
14
Medium Priority
?
578 Views
Last Modified: 2012-08-14


I have VB.Net Form in VS 2005.

In which i am entering Patient's Physician or Doctor details ..!

In the form, i have kept 3 rows ..

Like

Physican Name + TextBox , Phone Number + TextBox, FaxNumber + TextBox.

Some patients have more than 3 Doctors .. so, if i need to add more than 3 rows .. i just need to press Add button, which will automatically create a new row ..

How to add more controls dynamically on run time mode ..!
0
Comment
Question by:chokka
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 3
  • +1
14 Comments
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 33502747
Dim atextbox As New System.Windows.Forms.TextBox()
FormName.Controls.Add(atextbox)
'change FormName To Whatever You Named Your form name where you want to create a textbox.

Try something like this.

0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33503431
In your case you have to make a 1 to many relation between patient and doctor ....
Patient will be in details ...and doctor in a datagridview....so for 1 patient many doctors...
0
 
LVL 5

Accepted Solution

by:
sanket_1985 earned 2000 total points
ID: 33504967
Create the control, set its size and location and add it to this.Controls and it should work.

A sample code is:


private void AddTextBox(int i)
{
this.SuspendLayout();

TextBox myTextBox = new TextBox();
myTextBox.Name = "TextBox" + i.ToString();
myTextBox.Size = new Size(100, 100);
myTextBox.Location = new Point(120, 120);

this.Controls.Add(myTextBox);

this.ResumeLayout(false);
this.PerformLayout();
}

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33505085
@sanket_1985 And @lucky85 Your codes for me are correct....The author is talking about Rows....I am a little bit Confused....Maybe @chokka must be more specific...Am i wrong?
0
 

Author Comment

by:chokka
ID: 33505104
yes, like in ASP.net grid view - we have Insert template .. which add new row dynamically ..
similarly, i am looking forward to add new row dynamically .. !

0
 
LVL 5

Expert Comment

by:sanket_1985
ID: 33505107
@jtoutou agreed with your statement - probably this is just a sample code that can help to replicate three textbox. I might be wrong, might be right.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33505113
Are you using a datagridview?
0
 
LVL 5

Expert Comment

by:sanket_1985
ID: 33505121
@chokka - win form or web form?
0
 
LVL 5

Expert Comment

by:sanket_1985
ID: 33505182
Had it been ASP.NET Web Form, the sample code would have been:

Imports System.Collections.Generic
Imports System.Web.UI
Imports OfficeFileReader

Public Partial Class ResumeWizard
	Inherits System.Web.UI.Page
	#Region "Private Fields"

	Private experienceDynamicControls As ExperienceDetailsControl()

	#End Region

	#Region "Event Handlers"

	Protected Sub Page_PreInit(sender As Object, e As EventArgs)
		Dim myControl As Control = GetExperiencePostBackControl(Me.Page)

		If (myControl IsNot Nothing) Then
			If (myControl.ClientID.ToString() = "wizResumeBuilder_btnAddExperience") Then
				Try
					Session("thisExperienceControlsCount") = Convert.ToInt32(Session("thisExperienceControlsCount")) + 1
				Catch generatedExceptionName As Exception
					Session("thisExperienceControlsCount") = 2
				End Try
			End If
		End If
	End Sub

	Protected Overrides Sub OnInit(e As EventArgs)
		MyBase.OnInit(e)

		Dim experienceControlsCount As Integer = 1

		Try
			experienceControlsCount = Convert.ToInt32(Session("thisExperienceControlsCount"))

			If experienceControlsCount = 0 Then
				experienceControlsCount = 1
				Session("thisExperienceControlsCount") = 1
			End If
		Catch generatedExceptionName As Exception
			Session("thisExperienceControlsCount") = 1
		End Try

		experienceDynamicControls = New ExperienceDetailsControl(experienceControlsCount - 1) {}

		For ctr As Integer = 0 To experienceDynamicControls.Length - 1
			Dim userControl As ExperienceDetailsControl = DirectCast(Page.LoadControl("~/ResumeControls/ExperienceDetailsControl.ascx"), ExperienceDetailsControl)
			userControl.ID = "ExperienceDetailsControl" & ctr.ToString()
			userControl.Index = (ctr + 1)
			plhExperienceDetails.Controls.Add(userControl)
			experienceDynamicControls(ctr) = userControl
			Dim literalBreak As New LiteralControl("<br /><br />")
			plhExperienceDetails.Controls.Add(literalBreak)
		Next
	End Sub

	Protected Sub btnAddExperience_Click(sender As Object, e As EventArgs)

	End Sub

	Protected Sub btnGenerateResume_Click(sender As Object, e As EventArgs)
		If radFormatBW1.Checked Then
			Dim experienceDetails As New List(Of JobDetails)()

			For Each currentControl As EducationDetailsControl In educationDynamicControls
				Dim er As New EducationRecord()
				er.CourseAndDatesOfAttendance = currentControl.ClassOrDegree + "(" + currentControl.AttendanceDates & ")"
				er.SchoolName = currentControl.SchoolName
				er.SchoolCityState = currentControl.SchoolCityState
				educationDetails.Add(er)
			Next
		End If
	End Sub

	#End Region

	#Region "Private Methods"

	Private Function GetExperiencePostBackControl(thePage As Page) As Control
		Dim myControl As Control = Nothing
		Dim ctrlName As String = thePage.Request.Params.[Get]("__EVENTTARGET")
		If ((ctrlName IsNot Nothing) And (ctrlName <> String.Empty)) Then
			myControl = thePage.FindControl(ctrlName)
		Else
			For Each Item As String In thePage.Request.Form
				Dim c As Control = thePage.FindControl(Item)
				If (TypeOf (c) Is System.Web.UI.WebControls.Button) Then
					If c.ID = "btnAddExperience" Then
						myControl = c
					End If
				End If

			Next
		End If
		Return myControl
	End Function

	#End Region
End Class

Open in new window

0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33505193
@sanket_1985 I didn't mean anything about your code which is correct...but seems to me like i said in my first comment parent-child relation...
0
 
LVL 5

Expert Comment

by:sanket_1985
ID: 33505216
@jtoutou - didn't meant to offend you. Please accept my apologies if you felt so.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33505332
no worries!!!!!!!!!!!!..................I just wondered if author's question could be more clearly  :)
0
 

Author Comment

by:chokka
ID: 33533772
Ya, i am just working on it .. and also visualizing the difference between GridView and Dynamically Adding controls ..!
0
 

Author Closing Comment

by:chokka
ID: 33726327
Thanks
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question