On Visual Basic 6.0 you add a TextBox on form and it has a name TextBox1 after that I copy/paste that TextBox on form and I got TextBox1(0) paste again TextBox1(1)
How to do that on Visual Basic 2012 ???
I copy/paste text box and got Textbox1 Textbox2?
Do you understand the question?
I tried to copy paste TextBox1 and I got TextBox2
The code I want to use is to check the TextBoxes
something like
Dim i as integer
For i=1 to 5
textbox(i).text="Anel"
Next
You have to draw FIVE textboxes and ONE button on the form
(1) Declare a Collection at class level,
(2) In Form_Load event, add your textboxes to the Collection
(3) You can access all your textboxes with for loop as shown in following code
Dim AL As New Collection
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AL.Add(TextBox1)
AL.Add(TextBox2)
AL.Add(TextBox3)
AL.Add(TextBox4)
AL.Add(TextBox5)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
For i = 1 To AL.Count
AL(i).Text = "Hello"
Next
End Sub
First create a list of the number of textboxes you want to modify and name the textboxes in a good way
--> Exemple :
-----> I have three textboxes names as following : " ttttaksa1" , "asiuhi2" , "EEFUGUYug3"
-----> We have put a number at the end of the textbox so we can call mofidy each one
according to its number
-----> Now that we have 3 textboxes , we will make a list of strings with the numbers of our textboxes : in this case : 3 numbers ( NB : The list is of STRINGS ! )
The list will be like that : Dim n as New List(of string)=({"1","2","3"})
if we have 11 textboxes for exemple it will be like that :
Dim n as New List(of string)=({"1","2","3","4","5","6","7","8","9","10","11"})
Now , if you want to , for exemple , change the text of the two first textboxes (ttttaska1 and asiuhi2 ) , you will need to do that :
For Each textbox In Me.Controls
For Each s As String In n
If textbox.Name.ToString.EndsWith(n) Then
'Do what you want to your textbox ex : textbox.text="Anel"
textbox.forecolor=Color.Blue
End If
Next
Next
I hope that was useful and thank you :D
I am now making games and selling them , wanna join me ? facebook : Waterfull Idr
Good luck :D
Dim textBoxTemp As TextBox
Dim stringData As String
For index = 0 To 23
stringData = ("txtBoxReal" & index)
textBoxTemp = CType(Me.Controls(stringData), TextBox)
textBoxTemp.Text = 0
Next
Related
I have several arrays named : Array1, Array2, Array3, ... and also some comboboxes named: cboArray1, cboArray2, cboArray3, ....
How can i write a GENERAL code to add elements of each array to corresponding combox. I know following code works, but its not GENERAL and ABSTRACT.
For i = 0 To Array1.Length - 1
cboArray1.Items.Add(Array1(i))
Next
For i = 0 To Array2.Length - 1
cboArray2.Items.Add(Array2(i))
Next
...
Working procedure maybe as follows: 1. Find all comboboxes in form (easy) 2. extract name of a combox (easy) 3. find similar-named array from code (difficult) 4. ....
I can use other sets like List, ... if it makes sense.
Here's what you're asking for using Reflection...though I'm not sure how useful this really is:
Public Class Form1
Private Array1 As String() = {"cat", "dog", "fish"}
Private Array2 As String() = {"alpha", "beta", "gamma"}
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
WireComboBoxes(Me)
End Sub
Private Sub WireComboBoxes(ByVal container As Control)
For Each ctl As Control In container.Controls
If TypeOf ctl Is ComboBox AndAlso ctl.Name.ToUpper.StartsWith("CBO") Then
Dim cb As ComboBox = DirectCast(ctl, ComboBox)
Dim arrName As String = cb.Name.Substring(3)
Dim fi As System.Reflection.FieldInfo = Me.GetType.GetField(arrName, Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Public Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.IgnoreCase)
If Not IsNothing(fi) Then
cb.DataSource = fi.GetValue(Me)
End If
ElseIf ctl.HasChildren Then
WireComboBoxes(ctl)
End If
Next
End Sub
End Class
I am new to coding with visual basic.
Recently, I was tasked by my professor to write a programme that allows the user to enter five words. The words then should be sorted and displayed in alphabetical order.
To do this I decided the best approach would be to use an array.
My thinking was that if I created a counter at the start, I can create a different value for each column of the array when a button is clicked.
If the array exceeds five I have a message box pop-up that resets the code (although I realise I will also have to clear the contents of the array).
My problem arises in displaying the array. I have looked for solutions online, and none have helped me as of yet.
I need to sort the array into alphabetical order and then display it in a label box (lbl_DisplayArray). As I do not know the values of the array, this has proved tricky.
My code is below:
Public Class Form1
Dim i As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim Array(4) As String
Array(i) = txt_UserWords.Text
End Sub
Private Sub btn_Next_Click(sender As Object, e As EventArgs) Handles btn_Next.Click
i += 1
If i >= 5 Then
i = 0
MsgBox("Array Limit Exceeded. Code Reset")
txt_UserWords.Text = ""
End If
End Sub
Private Sub btn_Sort_Click(sender As Object, e As EventArgs) Handles btn_Sort.Click
lbl_DisplayArray.Text =
End Sub
End Class
You'd be better off using
private myList as new List(of String).
Then to sort them you just call the .Sort() method. Just call .Add(txt_userWords.Text) to add the new string and use .Count to see how many of them you have.
When you're adding them to the label you can use
lbl_DisplayArray.Text = String.Join(vbCrLf, myList)
You'll need the list of to be a member of the class instead of a local variable (as you have declared Array). This will keep it alive and allow you to access it in other methods.
---------- edit ----------
Public Class Form1
private myList as new List(of String)
Private Sub btn_Next_Click(sender As Object, e As EventArgs) Handles btn_Next.Click
If myList.Count >= 5 Then
myList.Clear
Else
myList.add(txt_UserWords.Text)
End If
txt_UserWords.Text = ""
End Sub
Private Sub btn_Sort_Click(sender As Object, e As EventArgs) Handles btn_Sort.Click
myList.Sort()
lbl_DisplayArray.Text = String.Join(vbcrlf, myList)
End Sub
End Class
Trying to knock out a project that's hanging my code without re-writing the entire thing. In it, I just need to populate an array with the information already in the Names combobox in order to proceed. As I'm not using .NET, my options are limited.
(The end result)*Add a button named “btnShowBalance” displaying the text “ShowBalance.” Write code in its event handler asking the user to enter a client’s name (InputBox). Search the names array for the name entered. If the name is found, use its location to retrieve the matching balance from the balances array. Display the client’s name and balance if the client exists; otherwise, display a not-found message. ****
Public Class Form1
Dim Balances(7) As Decimal
Dim Names(7) As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cboNames.Items.Add("John One")
cboNames.Items.Add("Jack Two")
cboNames.Items.Add("John Three")
cboNames.Items.Add("Jack Four")
cboNames.Items.Add("John Five")
cboNames.Items.Add("Jack Six")
cboNames.Items.Add("John Seven")
cboNames.Items.Add("Jack Eight")
cboBalances.Items.Add("235.50")
cboBalances.Items.Add("78943.98")
cboBalances.Items.Add("230781.10")
cboBalances.Items.Add("78362.00")
cboBalances.Items.Add("12097.20")
cboBalances.Items.Add("89267.34")
cboBalances.Items.Add("34959.06")
cboBalances.Items.Add("559284.50")
For i = 0 To cboNames.Items.Count - 1
Names(i) = cboNames.Text
Next
For i = 0 To cboBalances.Items.Count - 1
Names(i) = cboBalances.SelectedItem
Next
End Sub
Private Sub btnShowBalance_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowBalance.Click
Dim strResult As String
strResult = InputBox("Enter the name of the customer", "Balance Finder")
For i = 0 To Names.Length - 1
If strResult = Names(i) Then
MessageBox.Show(Names(i).ToString & " " & Balances(i).ToString)
End If
Next
End Sub
End Class
Does anyone know how to dynamically add an array to a bunch of comboboxes in VB.net? I could really use the help (I've been struggling with this all day). When I try to do it my way I get an error on form load.
My code:
Private Sub Form1_Load(ByVal sender as Object, ByVal e as EventArgs) Handles Me.Load
Dim MyArray() as String = {"a","b","c"}
For each ctl as ComboBox in Me.Controls
if ctl.tag = "yadda" then ctl.Items.AddRange(MyArray)
Next
End Sub
Error: "Unable to cast object of type '...Button' to type '...Combobox'."
I've tried so many variations to this code but I just can't get it to work. I will eventually have nearly a hundred similarly constructed comboboxes in my application, and I'd like to be able to programmatically initialize their items. Could someone please help?
Thanks,
Elias
This is the way to do it :
Public Class Form1
Function getControl(ByVal controlName As String) As Control
Dim numCtrls = Me.Controls.Count()
For I As Integer = 0 To numCtrls - 1
If Me.Controls.Item(I).Name = controlName Then
If TypeOf Me.Controls.Item(I) Is ComboBox Then
Return CType(Me.Controls(controlName), ComboBox)
End If
End If
Next
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim myArray As Array = {"a", "b", "c"}
Dim myComboBox As ComboBox
For Each ctl As Control In Me.Controls
If TypeOf ctl Is ComboBox Then
If ctl.Tag = "yadda" Then
myComboBox = getControl(ctl.Name)
myComboBox.Items.AddRange(myArray)
End If
End If
Next
End Sub
End Class
You loop through all controls (buttons, combo, etc ...) then you check if it is the type you want (ComboBox) and do whatever you need.
Good luck !
I have a wizard control in wich I am adding a user control containing a simple table with
some input fields based on users entry of how many children they have. ex: how many kids do you have so I add the user control ascx based on that loop
that goes into step 5 of my wizard wich is also in a masterpage.
I then use findcontrol to atttempt the get to those input boxes so i can save the data into my db, findcontrol allway comes up null, even though the user control in visable and recreated on page load after post back.
any help greatly appreciated.
find control button:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim numbchildren As Integer = CInt(Howmanychildren.Text)
For i As Integer = 1 To numbchildren - 1
Dim textbox As TextBox = TryCast(Me.Wizard1.FindControl("WizardStep5").FindControl("Minor_1_Child_Name"), TextBox)
'Dim textbox2 As TextBox = TryCast(Me.Wizard1.FindControl("WizardStep5").FindControl("Howmanychildren"), TextBox)
If textbox IsNot Nothing Then
Response.Write("Found TextBox1 <br>")
Dim val As String = textbox.Text
Response.Write(val & "<br>")
Else
Response.Write("not found" & "<br>")
End If
' Insert into DB
'SaveValueToDatabase(val)
Next
End Sub
user control added function on dropdown :
Protected Sub Doyouhavechildren_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Doyouhavechildren.SelectedIndexChanged
Dim numbchildren As Integer = CInt(Howmanychildren.Text)
Dim cnt As Integer = 1
'Panel1.Controls.Clear()
Select Case Doyouhavechildren.SelectedIndex
Case 0
ViewState.Add("Doyouhavechildren", numbchildren)
Do While cnt <= numbchildren
Dim uc As Web.UI.UserControl = DirectCast(Page.LoadControl("MinorChild.ascx"), Web.UI.UserControl)
uc.ID = "Minor_" + cnt.ToString()
Wizard1.ActiveStep.Controls.Add(uc)
cnt = cnt + 1
Loop
Exit Select
Case 1
Exit Select
End Select
End Sub
user control:
<%# Control Language="VB" AutoEventWireup="false" CodeFile="MinorChild.ascx.vb" Inherits="MinorChild" %>
Name
Age
SS#
DOB
the find control works in the howmanychildren field that is static
I figured it out myself
basicly, you have to referance the container, thats what everyone everywhere else where saying but I kept ignoring the answer
the correct code is
Dim textbox As TextBox = TryCast(Me.Wizard1.FindControl("WizardStep5").FindControl("Minor_1").FindControl("Child_Name"), TextBox)
you have to referance the user control name first then search within it , even though the client source is disceptive.