Auto formatting textbox - vb.net

I am trying to add multiple texts from a datagridview to a textbox.My multiple texts i mean, the textbox will contain the value of cell(12) of all the rows of the dgvw.Here's my code :
Private Sub Button6_Click_1(sender As Object, e As EventArgs) Handles Button6.Click
For Each row In Selected.dg2.Rows
SendMail.totxt.Text &= row.Cells(12).Value
SendMail.Show()
Next
Now, cell 12 contains E-MAILS.My application is also an EMAIL app. What I want is, in the textbox, a comma(,)will be added automatically after every email address/every row's cell value. Any solution?

Private Sub Button6_Click_1(sender As Object, e As EventArgs) Handles Button6.Click
For Each row In Selected.dg2.Rows
SendMail.totxt.Text &= row.Cells(12).Value
SendMail.totxt.Text &= ", "
Next
totxt.Text = totxt.Text.Remove(totxt.TextLength - 2, 2)
You might also want to have a look at giving your email column a name, so that you can refer to it with row.cells("email").value instead of refering to it by index. This way, if you the number of columns in your datagrid ever change, your program wont break.

Related

How can I write data into a row in Virtual Studio? (MS Access Database file"

I would like to make a small application what can make easier my work.
I have to make one ordering process some labels where I have a plan and from the plan I have to one excel list making with the labelnames what will be printed by the Manufacturer. We have a lot of type of the label (names) but they are fixing. (160 pieces differentnames).
So I make this always if I get a new plan then I make new label-list. I do it alway by hand cell by cell.. Sometime I have to filling out 400 cell for one plan... I don’t have a lot’s of time to this..
So I started making my first Windows app with Visual Studio(in Windows From – Visual Basic).
I found some cool video, so I already have the „basics”.
I connected one MS Access database to my Project where I would like to store the datas.
And now i have trouble.. I have some Comboboxes. I would like to choose some cases and then I would like to update the database where the rows will be filled with the correct values. If its done, i can this exporting to excel and sendig forward to the Manufacturer.
I have a textbox where I write the Plan name (it linked to the database first Column)
And then:
So the Combobox1 have Sektor A, B, C, D ,E, F (six value)
The Combobox2 have 09RRU, 09RSU, 18RRU ... empty - (eight value)
The Combobox3 have 21RRU, 21RSU, 26RRU ... empty - (eight value)
The Combobox4 have ja or nein (only two value)
and so on.. I have 8 Combobox.
My Idea:
If I select the Sektor A and 09RRU , the another two is empty and nein, then I click on the Update button I would like to get back in database 09.SekA1, 09.SekA2, AISG.SekA
If I select the Sektor A and 09RRU and 21RRU and ja, then after click Update, I would like get 09.SekA1, 09.SekA2, 21.SekA1, 21.SekA2, 09.21.SekA1, 09.21SekA2, AISG.09.21.SekA
….
I can every type of labels line by line writing if needed, I think I have to do this. I don’t think so can I dynamic Arrays making which can the different String e.g. ( Text (09) & Text (.SekA) & Text(1)) creating. I just now started the VB..
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox8.SelectedIndexChanged
SektorForm1()
End Sub
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
End Sub
Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
End Sub
Private Sub ComboBox4_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox3.SelectedIndexChanged
End Sub
Private Sub SektorForm1()
BEschriftungenDataSet.Standort.RRU18Column = "18.SekA1"
BEschriftungenDataSet.Standort.RRU21Column = "18.SekA2"
BEschriftungenDataSet.Standort.RRU26Column = "AISG.18.SekA"
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.StandortTableAdapter.Fill(Me.BEschriftungenDataSet.Standort)
ComboBox1.SelectedIndex = 5
ComboBox2.SelectedIndex = 3
ComboBox3.SelectedIndex = 3
ComboBox4.SelectedIndex = 1
ComboBox5.SelectedIndex = 1
ComboBox6.SelectedIndex = 1
ComboBox7.SelectedIndex = 1
ComboBox8.SelectedIndex = 0
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
On Error GoTo SaveErr
StandortBindingSource.EndEdit()
StandortTableAdapter.Update(BEschriftungenDataSet.Standort)
MessageBox.Show("Saved")
End Sub
Could some one for me Helping what is wrong in my code? I tried some data inserting to tha database when I choose someting in Combobox1 but it doesn't work..
I get failures: BC30526 Property'RRU18Column' is 'ReadOnly' and BC30311 Value 'String' cannot be converted to 'DataColumn'..
And giving some help how I sould staring building up my cases to my Combobox chooses..
Thanks guys

Updating Values in a Data Grid View With Key Press Data Entry in Numeric Up and Down

I am setting up a Data Grid View that contains a list of items, with one column as the Item Number (1, 2, 3) and the second number as the name of the items. I am controlling the input of the Item Number column using a Numeric Up and Down, so when I increase it, the rows are added and the Item Number column is automatically updated (so the user doesn't have to enter this in). However, this only works if I use the numeric up and down by clicking the arrows. If I type in the number (say 4 items), I get an exception (System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index') and the procedure doesn't work. The code is below:
Private Sub numItemNumber_ValueChanged(sender As Object, e As EventArgs) Handles numItemNumber.ValueChanged
While (dgvItems.Rows.Count < numItemNumber.Value)
dgvItems.Rows.Add()
i = numItemNumber.Value
dgvItems.Rows(i).Cells(0).Value = i + 1 'This is where the exception is
End While
End Sub
I added in a KeyPress event to see if that could handle the item entered in but it doesn't.
Private Sub numItemNumber_KeyPress(sender As Object, e As EventArgs) Handles numItemNumber.KeyPress
For i = 0 To numItemNumber.Value - 1
dgvItems.Rows(i).Cells(0).Value = i + 1
Next
End Sub
How can I edit this to include both events (the user using the up and down keys or just entering the number in directly)? Thank you.
Don't you still have to Add the new row before you can enter a value in it?
Private Sub numItemNumber_KeyPress(sender As Object, e As EventArgs) Handles numItemNumber.KeyPress
For i = 0 To numItemNumber.Value - 1
dgvItems.Rows.Add()
dgvItems.Rows(i).Cells(0).Value = i + 1
Next
End Sub
Figured out this works, essentially adding a loop through the rows of the data grid view in the Value Changed event so that it can handle any number > 0. Adding the code here for anyone who possibly needs it:
Private Sub numItemNumber_ValueChanged(sender As Object, e As EventArgs) Handles numItemNumber.ValueChanged
While (dgvItems.Rows.Count < numItemNumber.Value)
For i=0 to numItemNumber.Value-1
dgvItems.Rows.Add()
dgvItems.Rows(i).Cells(0).Value = i + 1
Next
End While
End Sub
The KeyPress Event is not needed.

Visual Basic - Displaying a combo box entry over a textbox

to keep it short and simple, I am attempting to display a number selected through a combo box on a second form through a label.
Here are the bits that are relevant to the issue I am having:
for i = 1 To 31
cmb_days.Items.Add(i)
next
' Populating combo box
days = cmb_days.text
frm_result.lbl_renting = "Renting for " & days & " Days"
I have tried using other variants such as cmb_days.selecteditem to no avail
I am also kinda having issues with like telling my code to do things with whatever number is actually selected in the combo box, idk I am veryyyyy new
That looks more or less correct.. But the question is where your call to update the label occurs in your code...
This is a minimal working example, with just a ComboBox on a form:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i = 1 To 31
ComboBox1.Items.Add(i)
Next
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Me.Text = ComboBox1.Text
End Sub
End Class
The update of the label (in this case the text of the form, but it works exactly the same with a label on a different form) occurs in the SelectedIndexChanged event of ComboBox1.
If you copied your code straight from the project, you haven't had time to select anything yet, so the Text property will be empty.

vb.net matching textbox.text with contents of listbox data

What I want to do is type a number in textbox1 and then match the word that corresponds to it for example
if textbox1.text = 6
then textbox2.text = searchable
but this list could be 10000 items long, so don't want to hard code it.
listbox contains the following data.I am open to changing the layout slightly if needed.
1 example
2 word
3 to
4 find
5 by
6 searchable
7 numbers
then upon button2 click my textbox2 would contain searchable (but not the number)
Thanks
Add this code in your button's click event
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Val(TextBox1.Text) <= ListBox1.Items.Count - 1 Then '<--- check the textbox contains a number less than the item count of the list box
TextBox2.Text = ListBox1.Items(TextBox1.Text) '<---- replace the number with carasponding item in the listbox
End If
End Sub
Maybe use Listbox.Findstring(textbox1.text.trim & " ") or Listbox.FindString(Val(TextBox1.Text))
These return the index to the item you are looking for...

while clicking down arrow cursor need to come other control

i given code like this:
Private Sub txtemployeename_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtemployeename.KeyDown
keyval = e.KeyData
Dim keyData As System.Windows.Forms.Keys = e.KeyData
If keyData = Keys.Down Then
LstEmployee.Visible = True
LstEmployee.Focus()
End If
End Sub
while i am cliking down arrow first time that is not focusing to listbox,second time am clicking down arrow that is focusing..also once cursor come to tha list box,if i clik enter that should be displayed in text box..for that i given code like this..
Private Sub LstEmployee_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LstEmployee.Enter
txtemployeename.Text = LstEmployee.SelectedItem
End Sub
but this is not working properly..for loading list box i given code like this:
Private Sub txtemployeename_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtemployeename.KeyPress
Dim s As String = txtemployeename.Text
LstEmployee.Visible = True
loadlistbox(LstEmployee, "select Ename from EmployeeMaster_tbl where Ename LIKE'%" & s & "%' ")
End Sub
You should rely on the KeyUp event rather than on the KeyDown one. Also for the ListBox you just need the SelectedIndexChanged event. Additionally, your code has quite a few errors (wrong query (-> you don't need to call your DB every time to order the items in the ListBox), relies on SelectedIndex rather than on SelectedItem...). Here you have an updated version:
Private Sub txtemployeename_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles txtemployeename.KeyUp
Dim s As String = txtemployeename.Text
LstEmployee.Visible = True
Dim list = LstEmployee.Items.Cast(Of String)()
Dim query = From item As String In list Where item.Length >= s.Length AndAlso item.ToLower().Substring(0, s.Length) = s.ToLower() Select item
If (query.Count > 0) Then
Dim newItems = New List(Of String)()
For Each result In query
newItems.Add(result)
Next
LstEmployee.Items.Clear()
For Each newItem In newItems
LstEmployee.Items.Add(newItem)
Next
End If
End Sub
Private Sub LstEmployee_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles LstEmployee.SelectedIndexChanged
txtemployeename.Text = LstEmployee.SelectedItem
End Sub
The code above checks for occurrences (i.e., if the whole string in the txtemployeename matches (caps do not matter) the starting substring of, at least, one element in LstEmployee) every time a new character is introduced in txtemployeename. The ListBox is updated with these ocurrences. txtemployeename displays the name of the selected item in LstEmployee.
I hope that this will be enough to help you to build the code required to deliver the exact functionalities you are after.
NOTE: bear in mind that this approach (deleting/adding Items) is incompatible with cases where the ListView is populated with a DataSource. If you rely on a DataSource you would have to update this code accordingly.
NOTE2: the proposed approach deals with the elements in the ListView. You have to introduce these elements at the start from whatever source you are using; this code only updates existing information (items in the ListBox). Also bear in mind that this code is expected to be corrected to match your exact requirements; for example: list has to be associated with the total number of items (the ones retrieved from your datasource at the start), not with the current ones (as displayed in the code; it just represents a simplified version of the problem): every time a new population occurs all the items (other than the target ones) are deleted and thus the ListBox does not represent a reliable source. Example to understand this: at the start, you have "aaaa", "bbbb", "cccc"; if you type "a", all the elements except "aaaa" would be deleted. If you type now "b" and consider the actual elements in the ListBox, no change would occur as far as the only element is "aaaa"; you would have to consider all the original elements (which, as suggested via comment, might be stored at the start in an array/list of strings).