I have a list with 2 columns, ID and Display Name.
ID
Display Name
1
Customer A
2
Customer B
3
Customer C
When the user select Customer B, I want the text to display 2, not customer B. I thought I've done this before but can't seem to get it working.
Protected Sub ddlDelegatedTo_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlDelegatedTo.SelectedIndexChanged
Me.ddlDelegatedTo.Text = Me.ddlDelegatedTo.SelectedItem.Text
' Me.ddlDelegatedTo.SelectedValue
End Sub
From what you are describing I think this is what you are trying to achieve
Private Sub ddlDelegate_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlDelegate.SelectedIndexChanged
ddlDelegate.Items().FindByValue(ddlDelegate.SelectedValue).Text = ddlDelegate.SelectedValue
End Sub
dim Number as integer = (dropdownlist.indexof(dropdownlist1.selecteditem) + 1)
Related
Amateur her so bear with me. I am trying to compile VBA code which runs a userform with list box populated with a 9 column and 100 row table from worksheet1. The user selects the only the items in the list box he needs for a report and they are to copied to worksheet 2.
With help from a 6 year old post on this site, I have managed to do this using an array and a public function to select the chosen rows and then output them to worksheets2. As follows:
Private Sub CommandButton1_Click()
Dim SelectedItems() As Variant
Dim i As Integer
Dim emptyrow As Integer
wsTarget.Activate
Range("A1").Select
SelectedItems = GetSelectedRisks(RiskList)
emptyrow = 15
For i = LBound(SelectedItems) To UBound(SelectedItems)
wsTarget.Cells(emptyrow, 2).Value = SelectedItems(i)
emptyrow = emptyrow + 1
Next
End Sub
Public Function GetSelectedRisks(lBox As MSForms.ListBox) As Variant
Dim tmpArray() As Variant
Dim i As Integer
Dim SelectionCounter As Integer
SelectionCounter = -1
For i = 0 To lBox.ListCount - 1
If lBox.Selected(i) = True Then
SelectionCounter = SelectionCounter + 1
ReDim Preserve tmpArray(SelectionCounter)
tmpArray(SelectionCounter) = lBox.List(i)
End If
Next
However I can only work out how to do this for the 1st column. I just can't work out how to get the other columns into the array and then back out again.
Should I be using an array or am I making this to complicated i.e. should I just be using loops and if selected, entirerow.copy type stuff?
I have two textbox dates in asp.net and I am using vb.net. I want to find number of days between these two dates provided that all months are thirty days long.
Hello I did a small app that compares two dates to see how many days between those dates that's what I think you where asking.
APP THAT COMPARES TO DATES IN TEXTBOX
This is the code from the botton the difference btw days will be displayed in the text Txtdifbtwdays
Private Sub btncompare_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncompare.Click
Dim date1 As Date
Dim date2 As Date
Dim countdays As Integer
Dim day1 As Integer
Dim day2 As Integer
date1 = Me.Txtdate1.Text.ToString
date2 = Me.Txtdate2.Text.ToString
Dim getdays As Integer
getdays = date2.Subtract(date1).TotalDays
Me.Txtdifbtwdays.Text = getdays
End Sub
ounce you have the difference in days btw dates you can add the conditions you want
if getdays >30 then
'''do this
else
''''do this
end if
Code:
I have a class, Pricing.vb, that accepts an IEnumerable(Of tblAccount) in its new method, where tblAccount is an Entity Framework class with an added partial class that adds non-mapped properties.
Pricing.vb:
Public Property accounts As IEnumerable(Of tblAccount)
Public Sub New(ByVal accts As IEnumerable(Of tblAccount))
Me.accounts = accts
End Sub
tblAccount.vb:
Partial Public Class tblAccount
<NotMapped>
Public Property irr As Double
End Class
The Pricing.vb class then has a method, CalcBalance:
Public Sub CalcBalance(reqPrice As Double)
Dim acctsByIRR As List(Of Long)
Dim i As Long = 0
Me.FillCashFlows(reqPrice)
Me.FillMetrics()
acctsByIRR = Me.accounts.OrderBy(Function(t) t.irr).Select(Function(t) t.acct_id).ToList()
'do ... while that removes accounts 1 by 1 by lowest ray until it is above min
Do While Me.netIRR < Me.minimumIRR
Me.accounts = Me.accounts.Where(Function(t) t.acct_id <> acctsByIRR(i))
i = i + 1
If Me.accounts.Count = 0 Then Exit Sub
Me.FillCashFlows(reqPrice)
Me.FillMetrics()
Loop
End Sub
Basically, the CalcBalance() method is designed to remove tblAccount objects one-by-one from its property Me.Accounts, in order of lowest IRR, until the IRR of the remaining accounts (Me.netIRR) meets a minimum criteria (Me.minimumIRR). The Me.netIRR property is calculated by methods FillCashFlows() and FillMetrics().
Problem:
The problem that I'm seeing, though, is that Me.accounts essentially resets itself each time the do while...loop loops. So if I have tblAccounts with acct_ids 1 through 5, and let's say that the order of lowest IRRs is 1, 2, 3, 4, and 5, then the first time the method loops it will properly remove acct_id=1 from Me.accounts. BUT, the next time it loops, acct_id=1 will reappear in Me.accounts, and it will remove acct_id=2. It will continue this weird process until of course it hits an index out of range exception.
For a little visualization, here's what the IEnumerable(Of tblAccount) looks like, by acct_id after each loop:
After 1st loop:
2
3
4
5
After 2nd loop:
1
3
4
5
After 3rd loop:
1
2
4
5
After 4th loop:
1
2
3
5
After 5th loop:
1
2
3
4
It should look like this:
After 1st loop:
2
3
4
5
After 2nd loop:
3
4
5
After 3rd loop:
4
5
After 4th loop:
5
After 5th loop:
empty, and Exit Sub
It's because you're using IEnumerable<T> and not List<T>. LINQ query is lazy loaded, so your accounts property stores the query information (how to get the results) not a list of accounts. In every iteration of Do/While you just change the definition, and later on it's called against initial data source over and over again.
Make sure to call ToList whenever you assign to accounts:
Public Sub New(ByVal accts As IEnumerable(Of tblAccount))
Me.accounts = accts.ToList()
End Sub
and
Public Sub CalcBalance(reqPrice As Double)
Dim acctsByIRR As List(Of Long)
Dim i As Long = 0
Me.FillCashFlows(reqPrice)
Me.FillMetrics()
acctsByIRR = Me.accounts.OrderBy(Function(t) t.irr).Select(Function(t) t.acct_id).ToList()
'do ... while that removes accounts 1 by 1 by lowest ray until it is above min
Do While Me.netIRR < Me.minimumIRR
Me.accounts = Me.accounts.Where(Function(t) t.acct_id <> acctsByIRR(i)).ToList()
i = i + 1
If Me.accounts.Count = 0 Then Exit Sub
Me.FillCashFlows(reqPrice)
Me.FillMetrics()
Loop
End Sub
I am trying to format the listbox output to conform to my formatter. It just lists them. Any Ideas?
Private Sub Formatter_Click(sender As Object, e As EventArgs) Handles Formatter.Click
Dim formatter As String = CStr("{0,-7} {1,6:C2} {3,5}")
Dim name As String
name = (First.Text) + (Second.Text)
Dim x, y As Integer
x = ((InputBox("Enter a Number")))
y = (InputBox("Enter another Number"))
With Values.Items
.Add(x)
.Add(y)
.Add(name)
End With
You need to use String.Format to format the values and concatenate them into a single string. Then you can call ListBox.Items.Add just once per item. Like this:
Values.Add(String.Format(formatter, name, x, y))
I'm not sure of the order in which you intended to output those values, so that's just an example.
I am trying to change the list item values of a dropdown list based on values of an other dropdown list. The list values of drpAdult range from 0-9 and list values of drpInfant range from 0-(Value of drpAdult selected).
So, for example, if I select 5 in the drpAdult dropdown, the range of list item values of drpInfant will be from 0-5.
I have written the code below, but it is not populating the values in the drpInfant dropdown, which I am trying to insert on drpAdult_SelectedIndexChanged event.
Protected Sub drpAdult_SelectedIndexChanged(ByVal sender As Object,
ByVal e As EventArgs) Handles drpAdult.SelectedIndexChanged
Dim count As Integer
count = drpAdult.Items.Count
Dim i As Integer
i = 0
While count > 0
i = i + 1
drpInfant.Items.Add(New ListItem(i, i))
count = count - 1
End While
End Sub
What might cause this problem, and how can I resolve it?
Not sure what "is not working" means, but this seems to be easier anyway:
Dim newCount = drpAdult.Items.Count + 1
For i As Int32 = 0 To newCount
Dim newItem As New ListItem(i.ToString, i.ToString)
drpInfant.Items.Add(newItem)
Next
You can try this. I have tested this & working fine:
Protected Sub drpAdult_SelectedIndexChanged(sender As Object, e As EventArgs)
drpInfant.Items.Clear()
Dim count As Integer = drpAdult.SelectedIndex
Dim i As Integer = 0
While count >= 0
drpInfant.Items.Add(New ListItem(i.ToString(), i.ToString()))
i = i + 1
count = count - 1
End While
End Sub
Something along these lines...
drpInfant.Items.Clear()
dim n as Integer
Integer.TryParse(drpAdult.SelectedValue, n)
For i as integer = 1 to n
if n < i Then Exit For 'it's not fun when this condition happens in VB
drpInfant.Items.Add(New ListItem(i, i))
Next