Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have an array of 6 elements , and I want to fill 18 textboxes with those members randomly each time . I want only to repeat the item 2 times . I have this code :
FlatTextBox1.Text = (subjects.Item(Int(Rnd() * (subjects.Count - 1))))
FlatTextBox2.Text = (subjects.Item(Int(Rnd() * (subjects.Count - 1))))
.
.
.
.
.
.
That continues till the end . Now the problem is that some items get's repeated 3 times and others 1 time so there's no equality . How can I fix this problem ?
Assuming you made a typo and wanted the items repeated 3 times, instead of 2, since 3*6=18:
Public Class Form1
Private R As New Random
Private subjects() As String = {"cat", "dog", "fish", "hamster", "lizard", "bird"}
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim values As New List(Of String)
values.AddRange(subjects)
values.AddRange(subjects)
values.AddRange(subjects)
Dim index As Integer
Dim matches() As Control
For i As Integer = 1 To 18
index = R.Next(values.Count)
matches = Me.Controls.Find("FlatTextBox" & i, True)
matches(0).Text = values(index)
values.RemoveAt(index)
Next
End Sub
End Class
Related
This question already has an answer here:
i want to make a new variable that says shot1 shot2 shot3 so on and forth how do i do this?
(1 answer)
Closed 1 year ago.
i want there to be a new variable name for every new shot, the shot name should be shot1 , shot2 up to 1000
Dim shotlist(1000) As Boolean
Sub Main()
For i As Integer = 0 To 1000
Dim istring As String = i.ToString
shotlist(i) = "shot" & istring
Next
End Sub
Any help would be great thanks
You just assigned String ( i.e., istring ) to a Boolean ( i.e., shotlist(i) ), i hope this helps now.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I wanted to design a vb.net program which finds the longest word from a string. With the help of other users i managed to do that and i added other string operations. Overall the program now counts the number of symbols, has word count, interval count, finds longest and shortest word and the average word size. I thought the code could help others who have the same issues with those operations like me so i posted it below.
Here is a image of the program: https://i.stack.imgur.com/KcPNa.png
This is how the final code looks:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim a As String = TextBox1.Text
Dim a1 As Integer = Len(a) ' string size
Dim a3 As String = a.Split(" ").Length - 1 ' interval count
Dim a4 As String = a.Split(" ").OrderByDescending(Function(j) j.Length).FirstOrDefault 'longest word
Dim a5 As String = a.Split(" ").OrderByDescending(Function(j) j.Length).LastOrDefault 'shortest word
Dim a6 As String = a.Split(" ").Average(Function(j) j.Length) 'average word count
TextBox2.Text = a1
TextBox4.Text = a3
TextBox3.Text = a3 + 1 'word count is just 1 more than interval count
TextBox5.Text = a4
TextBox6.Text = a5
TextBox7.Text = a6
End Sub
I can not provide an solution in vb but maybe my c# implementation will give you a hint.
string source = "some string";
string longest = string.empty;
foreach(string s in source.Split(' ')) {
if (s.Length > longest.Length) {
longest = s;
}
}
Console.WriteLine(longest);
Tip: Next Time it would be helpfull to ask more specific questions.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How can I restrict a textbox to only accept lowercase letters with one uppercase letter, or at least 1 uppercase letter and a number?
To check if a string contains at least one upper character, one lower character and one number, you can use the Char.IsUpper / IsLower / IsNumber methods.
Private Function IsValidPasswordFormat(ByVal text As String) As Boolean
If String.IsNullOrEmpty(text) Then
Return False
End If
If text.Any(Function(c) Char.IsUpper(c)) AndAlso
text.Any(Function(c) Char.IsLower(c)) AndAlso
text.Any(Function(c) Char.IsNumber(c)) Then
Return True
End If
Return False
End Function
suppose that str is the textbox.text
Dim ucount as integer
ucount=0
For Each c As Char In str
Dim charCode As Integer = AscW(c)
If charCode >= 65 AndAlso charCode < 91 Then
ucount += 1
End If
Next
if ucount>1
'do something
End If
In the do something part, for example you can put textbox.text=""
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to convert this little code from Vba to Vb.net without success.
Would like some help please.
Dim str As String, i As Long
For i = 0 To lstPages.ListCount - 1
If lstPages.Selected(i) Then
If str <> vbNullString Then str = str & "-"
str = str & lstPages.List(i)
End If
Next
So you want the third column, from all selected rows of the ListView, in one string separated by "-"?
Yes that's right.
Then do:
Dim values As New List(Of String)
For Each lvi As ListViewItem In lstPages.SelectedItems
values.Add(lvi.SubItems(2).Text)
Next
Dim str As String = String.Join("-", values)
Debug.Print(str)
I think you could do something like this:
For i as integer = 0 To lstPages.ListCount - 1
If lstPages.Selected(i) Then
If Not String.IsNullOrEmpty(str) Then
str &= "-"
str &= lstPages.List(i)
End If
Next
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
In my program l am using timers to make pictureboxes visible and after certain time l make them not visible by using picturebox(1-9).visible = true/false.
Finally l have to count how many pictureboxes are visible at any given time.
l can't figure it out l have tried adding them to a variable but the timer does it over and over every second. l have literally no idea how to do it
This code is for Images on a worksheet, but can easily be modified for wherever your pictureboxes are:
'Count the visible number before starting.
Dim NumberVisible As Integer
NumberVisible = 0
For i = 1 To 9
If myWorkSheet.Shapes(i).Visible = msoTrue Then
NumberVisible = NumberVisible + 1
End If
Next
'....
'Start timer, do timer code here.
'....
myWorkSheet.Shapes(i).Visible = msoTrue
NumberVisible = NumberVisible + 1
'...
myWorkSheet.Shapes(i).Visible = msoFalse
NumberVisible = NumberVisible - 1
You need to check picturebox(1-9).visible itself. The same variable can be set and can also be checked for a given value.
Just loop through all the picture boxes in the form and check their visible property.
Sub foo()
Dim ct As Control
Dim sum As Integer
sum = 0
For Each ct In Me.Controls
If TypeName(ct) = "Image" Then
If ct.Visible = False Then
sum = sum + 1
End If
End If
Next
MsgBox sum
End Sub