Answered by:
What references should I add to make "Dim", "For", "Integer" and Next" to work in my UI?

Question
-
Hello,
I'm creating a VB UI and I added a text fields and a button to rest the text fields. I'm using the following code to reset the text field using a button.
"
private void button2_Click(object sender, EventArgs e)
{
Dim i as Integer
Dim tboxes(4) as String = New String() {"Text1","Text2","Text3","Text4"}
For i = 0 To 3
'set the value to ""
Next i
}"
But none of the functions are working and its saying the following message when I hover over it.
"The type or namespace "Dim" could not be found (are you missing a using directive or an assembly reference? )"
Any help would be appreciated.
Thanks
Shaped
Thursday, October 13, 2016 7:15 PM
Answers
-
The following makes a List(Of TextBox) at form level but could be private to the procedure. This is better than a list of string that you would then need to cast to a TextBox.
Private tboxes As New List(Of TextBox) Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load tboxes.AddRange({TextBox1, TextBox2, TextBox3, TextBox4}) End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click tboxes.ForEach(Sub(tb) tb.Clear() End Sub) End Sub
C#
using System; using System.Collections.Generic; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Button1_Click(object sender, EventArgs e) { tBoxes.ForEach(tb => tb.Clear()); } private List<TextBox> tBoxes; private void Form1_Load(object sender, EventArgs e) { tBoxes = new List<TextBox>() {TextBox1,TextBox2,TextBox3, TextBox4 }; } } }
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
- Edited by KareninstructorMVP Thursday, October 13, 2016 7:32 PM
- Marked as answer by ShapedWorld Thursday, October 13, 2016 10:27 PM
Thursday, October 13, 2016 7:25 PM
All replies
-
This is the VB.Net forum, you want the C# forum at the link below if this is the code you are using which is C# code.
If you say it can`t be done then i`ll try it
- Edited by IronRazerz Thursday, October 13, 2016 7:27 PM
Thursday, October 13, 2016 7:24 PM -
The following makes a List(Of TextBox) at form level but could be private to the procedure. This is better than a list of string that you would then need to cast to a TextBox.
Private tboxes As New List(Of TextBox) Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load tboxes.AddRange({TextBox1, TextBox2, TextBox3, TextBox4}) End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click tboxes.ForEach(Sub(tb) tb.Clear() End Sub) End Sub
C#
using System; using System.Collections.Generic; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Button1_Click(object sender, EventArgs e) { tBoxes.ForEach(tb => tb.Clear()); } private List<TextBox> tBoxes; private void Form1_Load(object sender, EventArgs e) { tBoxes = new List<TextBox>() {TextBox1,TextBox2,TextBox3, TextBox4 }; } } }
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
- Edited by KareninstructorMVP Thursday, October 13, 2016 7:32 PM
- Marked as answer by ShapedWorld Thursday, October 13, 2016 10:27 PM
Thursday, October 13, 2016 7:25 PM -
My apologies. I'll post it in the right forum next time.Thursday, October 13, 2016 9:34 PM
-
@Kareninstructor, C# code example you provided worked. Thank you.Thursday, October 13, 2016 9:36 PM
-
@Kareninstructor, C# code example you provided worked. Thank you.
Your welcome.Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator
Thursday, October 13, 2016 10:41 PM