6 Replies - 13541 Views - Last Post: 18 March 2008 - 07:56 PM Rate Topic: -----

#1 gram999  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 89
  • Joined: 21-January 08

Adding controls Dynamically between forms

Posted 12 March 2008 - 03:01 PM

As I mentioned in an earlier post I have been playing around with some very simple code trying to get an idea of how to move items between forms in VB.NET. Below is a very simple bit of code that lets the user enter text into a textbox on Form1 and the text entered writes to a label in Form2 on a command button click event.

I am trying to figure out if it is possible to dynamically create a new empty label on Form2 when the user clicks 'Yes' button in the message box and then write new information from the text box to the new label creating a list made up of labels with data in them. I know that this is possible if you are doing everything on one form but not sure about passing between forms. I have been expirimenting with this using a function (AddNewLabel) and I think that I am on the right track but am not certain. Here is the code (a large portion courtesy of PsychoCoder. Thanks PsychoCoder!) Any ideas are welcome. I am continuing to play with this so if I find a solution before someone suggests one I will post it. Thanks!


Public Class Form1

	Private Items As String = String.Empty

	Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click

		'Create a DialogResult Variable

		Dim StrNote As DialogResult
		Dim str As String

		'Get the value from the MessageBox

		StrNote = MessageBox.Show("Add another item?", "MyApplication", _
								  MessageBoxButtons.YesNo, MessageBoxIcon.Question)

		'Check to see if user selected Yes or No

		If StrNote = DialogResult.Yes Then
			'Add items

			AddNewLabel()
			str = SetValue(Items)

			'Empty the The TextBox

			txtfrm1.Text = String.Empty
			'Set focus to the TextBox
			txtfrm1.Focus()
		Else
			str = SetValue(Items)
			'Set the text property of the label to the items variable

			Form2.Label1.Text = str
			Form2.Show()


		End If

	End Sub

	Public Function SetValue(ByVal value As String) As String
		If String.IsNullOrEmpty(value) Then
			'No Value present so add a value
			Items = txtfrm1.Text + Environment.NewLine
		End If
		Return Items
	End Function

	Public Function AddNewLabel() _
	As System.Windows.Forms.Label
		' Create a new instance of the Label class.
		Dim aLabel As New System.Windows.Forms.Label
		Form2.Controls.Add(aLabel)
		aLabel.Size = New Size(39, 13)
		aLabel.Location = New Point(51, 34)
		aLabel.Text = txtfrm1.Text
		Return aLabel
	End Function

End Class




Is This A Good Question/Topic? 1

Replies To: Adding controls Dynamically between forms

#2 gram999  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 89
  • Joined: 21-January 08

Re: Adding controls Dynamically between forms

Posted 13 March 2008 - 12:25 PM

After some trial and error I have managed to create a label dynamically on a seperate form. I am still trying to figure out how to make the text change from label to label but this is the new code:

Public Class Form1

	Private Items As String = String.Empty
	Private frm2 As New Form2   

	Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click

		'Create a DialogResult Variable

		Dim StrNote As DialogResult
		Dim str As String
		Dim NewLabel As New Label


		'Get the value from the MessageBox

		StrNote = MessageBox.Show("Add another item?", "MyApplication", _
								  MessageBoxButtons.YesNo, MessageBoxIcon.Question)

		'Check to see if user selected Yes or No

		If StrNote = DialogResult.Yes Then
			'Add items

			AddNewLabel()
			str = SetValue(Items)

			'Empty the The TextBox

			txtfrm1.Text = String.Empty
			'Set focus to the TextBox
			txtfrm1.Focus()
		Else
			str = SetValue(Items)
			'Set the text property of the label to the items variable

			frm2.Label1.Text = str
			frm2.Show()

		End If

	End Sub

	Public Function SetValue(ByVal value As String) As String
		If String.IsNullOrEmpty(value) Then
			'No Value present so add a value
			Items = txtfrm1.Text + Environment.NewLine
		End If
		Return Items
	End Function

	Public Sub AddNewLabel()
		' Create a new instance of the Label class
		Dim aLabel As New System.Windows.Forms.Label
		frm2.Controls.Add(aLabel)
		aLabel.Size = New Size(39, 13)
		aLabel.Location = New Point(51, 34)
		aLabel.Name = "New Lable"
		aLabel.Text = txtfrm1.Text
		
	End Sub

End Class



Basically I took the kernal that PsychoCoder suggested and then added the instance of Form2 to the control collection. Then I changed the function to a sub as well. As I mentioned this will add the label but I am still working on the text part.

This post has been edited by gram999: 13 March 2008 - 12:31 PM

Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Adding controls Dynamically between forms

Posted 13 March 2008 - 12:33 PM

Take a look at what Im doing below. I create the new label, set the propeties I want to set, then I add the label. From there I access the new label by it's name

Public Sub AddNewLabel()
      ' Create a new instance of the Label class
	Dim aLabel As New System.Windows.Forms.Label
	'Set the properties of the new label
	aLabel.Size = New Size(39, 13)
	aLabel.Location = New Point(51, 34)
	aLabel.Name = "NewLabel"
        'Add the new label
	frm2.Controls.Add(aLabel)
        'Now try and set the text
        Form2.NewLabel.Text = txtfrm1.Text
End Sub



Also, you have a Return statement in your sub procedure, I'm surprised you aren't getting an error because of that, because you cant have a Return statement unless it's in a function.

Hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#4 gram999  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 89
  • Joined: 21-January 08

Re: Adding controls Dynamically between forms

Posted 13 March 2008 - 01:06 PM

Thanks again for the guidance PsychoCoder. I removed the return when I changed the Function to a Sub and initially neglected to remove it from the pasted post that I put in above. I went back and edited the post after I realized that it was still there (I think that same time that you were looking at the post). I will definitely incorporate your suggestions. I got caught up in another issue which is if I want to add multiple lables instead of having a fixed X,Y coordinate for the new label it really needs to be something like X+10, Y+10. So I am working on that and figured I would go back to the text issue. I will keep you guys posted on how I am making out on this.
Was This Post Helpful? 0
  • +
  • -

#5 gram999  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 89
  • Joined: 21-January 08

Re: Adding controls Dynamically between forms

Posted 18 March 2008 - 03:00 PM

The saga continues. I believe that I have figured out how to dynamically add the lables to Form2 on the fly but have been unable to get them to work in terms of showing data. My goal is when a user enters text into the text box on Form1 and clicks the command button the data in the text box is written to a lable on Form2 that is created dynamically. When the command button is clicked a message box comes up and asks the user if they have more data to add. If the answers is yes then they are taken back to the text box on Form1 to add more data. When the command button is clicked again to add the additional data a new label is created with the new data in it so as long as the user keeps adding new data new labels will be added to Form2 containing the new data in 1,2,3 list order. If the message box answer is No then whatever is in the textbox on Form1 when No is clicked writes to Form2 and that is it. I have been playing with a loop to add the labels but I am stuck on getting the textbox to write to them. Here is what I have at the moment:


Public Class Form1

	Private Items As String = String.Empty
	Private frm2 As New Form2

	Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click

		'Create a DialogResult Variable

		Dim StrNote As DialogResult
		Dim str As String
		Dim aLabel As New Label


		'Get the value from the MessageBox

		StrNote = MessageBox.Show("Add another item?", "MyApplication", _
								  MessageBoxButtons.YesNo, MessageBoxIcon.Question)

		'Check to see if user selected Yes or No

		If StrNote = DialogResult.Yes Then
			'Add items

			For i As Integer = 0 To 5
				aLabel.Location = New Drawing.Point(24, 21 + i * 10)
				aLabel.TabIndex = i
				aLabel.Text = i.ToString
				aLabel.Tag = i.ToString
			Next

			str = SetValue(Items)

			'Empty the The TextBox

			txtfrm1.Text = String.Empty
			'Set focus to the TextBox
			txtfrm1.Focus()
		Else
			str = SetValue(Items)
			'Set the text property of the label to the items variable

			frm2.Label1.Text = str
			frm2.Show()

		End If

	End Sub

	Public Function SetValue(ByVal value As String) As String
		If String.IsNullOrEmpty(value) Then
			'No Value present so add a value
			Items = txtfrm1.Text + Environment.NewLine
		End If
		Return Items
	End Function

End Class




I am continuing to expiriment so if I solve it I will post the solution. Thanks!
Was This Post Helpful? 0
  • +
  • -

#6 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Adding controls Dynamically between forms

Posted 18 March 2008 - 04:09 PM

Is the following line where you are attempting to write to the Label? If so, why are you writing the index of the For loop, instead of the contents of str?
aLabel.Text = i.ToString


Where are you actually adding the label to the frm2 controls?

Perhaps you should re-read PsychoCoders last post on how to add Controls to the second form.
Was This Post Helpful? 0
  • +
  • -

#7 gram999  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 89
  • Joined: 21-January 08

Re: Adding controls Dynamically between forms

Posted 18 March 2008 - 07:56 PM

View Postjayman9, on 18 Mar, 2008 - 04:09 PM, said:

Is the following line where you are attempting to write to the Label? If so, why are you writing the index of the For loop, instead of the contents of str?
aLabel.Text = i.ToString


Where are you actually adding the label to the frm2 controls?

Perhaps you should re-read PsychoCoders last post on how to add Controls to the second form.


Thanks Jayman. Yes, I need this line (frm2.Controls.Add(aLabel)

When I look at PsychoCoders snippet the issue that I am having is where the property labels are set. PsychoCoders snippet adds the label and writes to it to Form 2 but it places the label in the exact same spot each time. It does not create a 1,2,3 list becase the label location values are fixed values. What I was trying to do with the loop (and I tried modifying PsychoCoders suggestion first) is basically set the margin (X) the same for every new label but have the (Y) location increase each time the 'Yes' button is clicked by a line width. That is why I was playing with something like this:

aLabel.Location = New Drawing.Point(24, 21 + i * 10)

The problem with the line above is that the i * 10 is for the tab and it should be for the line. To modify PsychoCoders example I think that I need to declare an X,Y starting location for the first label and then when the add another item message box 'yes' button is clicked it adds a label beneath the first X,Y location. So if the initial label location is something (24, 21) which can be a fixed location (as in PsychoCoders code) when the user clicks yes on the add another item message box the new label should located at (24, 26) then if they add another item (24, 31) and so on.

Like this:

Lable 1

Label 2

Label 3

So I have been expirimenting with something along the lines of

aLabel.Location = New Drawing.Point(24, i * 10)

I will keep on trying. As always thanks for the suggestions.

This post has been edited by gram999: 18 March 2008 - 08:04 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1