Hi, I'm working on a Electronic Raffle Draw project - vb.net

I'm working on a ELECTRONIC RAFFLE DRAW project using VISUAL STUDIO 2010. I have a list of name in my sql database, and displayed it to a listview, along with certain conditions on what names should be displayed. Now, what I want to do is randomly select names from this list view. And I'm trying to convert this items from listview and convert it to a string array. I also have timer where when it's enable=true it should display these string array to a label.
And when ticker is enable=false. It should stop and display one specific name. All my codes are working except I get an InvalidCastException was unhandled in this specific line of code
ReDim strng(Name_ListView.Items.Count - 1)
Name_ListView.Items.CopyTo(strng, 0)
Dim strng() As String
Private Sub Frm_Raffle_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
ReDim strng(Name_ListView.Items.Count - 1)
Name_ListView.Items.CopyTo(strng, 0)
End Sub
Public Sub myTimer_Tick(sender As System.Object, e As System.EventArgs) Handles myTimer.Tick
Dim index As Integer = rnd.Next(Name_ListView.Items.Count)
mylblResult.Text = strng(index)
End Sub

Related

send x value(number) and y value(number) and other letter value (numbers) to thire textboxes

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.ShowDialog()
End Sub
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim reader As New IO.StreamReader(OpenFileDialog1.FileName)
TextBox1.Text = reader.ReadToEnd
End Sub
End Class
the program will be like this when press open the open file diloge well open the file from the computer and the well show in textbox1 . what I want to do is to send xvalue(the number after(x)) is send to textbox(x) and send yvalue(the number after(y)) is send to textbox(y) and each letter value well send value (number) well send like this and this code is for open the file I think the array must used in this case but I dont know how use the arry
If I've understood you correctly, I think the following will work:
Dim input As String="N14G73X315.2Y83.7I40.0J6.4A0.0H3K75T11F5M0C0.0"
Dim matches = System.Text.RegularExpressions.Regex.Matches(input, "\w[0-9.]+")
Dim parts = Matches.Cast(Of System.Text.RegularExpressions.Match).Select(Function(p) New With {.Key=p.Value.Substring(0,1), .Value=p.Value.Substring(1)}).ToDictionary(Function(p) p.Key, Function(p) p.Value)
txtX.text = parts("X")
txtY.text = parts("Y")
etc
We use a regular expression to split the string into groups of a single letter followed by a number including an optional decimal point. We then use Linq to turn this into a dictionary of numbers (well string representations of numbers) keyed on their corresponding letter from the input. You can then pull these from the dictionary to set each text field
You could also use capture groups on the regular expression instead of splitting the output in the linq statement

Take Text from Textbox and used it in a C

Visual Basics 2010:
Two numbers are generated automatically. For example if the Random numbers generated are 2 and 3, you should press the button in 2nd row and 3rd column. I have created the random number generator:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim i As Integer
For i = 0 To 5
TextBox1.Text = (Convert.ToString(random.Next(1, 5)))
TextBox2.Text = (Convert.ToString(random.Next(1, 5)))
Next
End Sub
But I can't use it to press the correct button. If the correct button is pressed the color changes. If not there are no changes. How can I do this. Please help me...
Note:
It is a board game. The game is played on a 4 by 4 grid To play the game, a player generates two random numbers and colors in an area on the grid indicated by the numbers. For example if the generated number is a 2 and a 3 the player clicks in the 2x3 square which results in the color of the square being changed. A player should only be able to click and change the color of the square represented by the random numbers only.
You should create a class-level variable for both the row and column chosen:
Private randomRow as Integer
Private randomCol as Integer
Then in your code, assign the values:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
randomCol = random.Next(1, 5)
randomRow = random.Next(1, 5)
TextBox1.Text = randomCol.ToString()
TextBox2.Text = randomRow.ToString()
End Sub
Now in your code the sees the user click a box, check to see if the box column and row are the same as the randomCol and randomRow variables.

Visual Basic 2010 - Multiple Clicks on button cycles throught different background images

I am trying to make a button cycle through different local background images in Vb 2010
However I am a complete novice at VB and cant figure out how to do this. I want the program to change its background image (cycle through the images in the folder), each time I click the button.
It seems like it should be easy though for the life of me I can't figure it out.
The code below will change it 'once'
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
BackgroundImage = Image.FromFile("C:ProgramIcons\2.jpg")
End Sub
End Class
You could declare a global variable near the top of your form like
Dim imageCounter as Integer = 0
Then in your Button Click you increment the imageCounter and then convert it as String using ToString() method in order it could be concatenated with File Location and File Extensions which are both are strings.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
imageCounter += 1 ' Increment Image Counter variable
if (imageCounter> 5) Then ' greater than 5
imageCounter = 1 ' Reset counter to 1
End If
'Convert to string and then concatenate with other string
BackgroundImage = Image.FromFile("C:ProgramIcons\"+imageCounter.ToString()+".jpg")
End Sub
Now, this assumes that you have five (5) images thus the comparison if (imageCounter > 5)

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).

Form_Load doesn't execute in application

I am new to Visual Basic. I have installed Microsoft Visual Studio 2010. Created a new Windows Form Application. As an example, I made a simple program which will ask the end user to input 2 numbers and allow them to either add them or subtract the second number from the first one and display the output in a Textbox.
Now, I added another Subroutine which would be executed automatically when the Windows Form loads. This would calculate the width of the output Textbox and the Form Width and display at the bottom.
This is how the code looks like right now:
Public Class Form1
' Run this Subroutine initially to display the Form and Text box width
Private Sub Form_Load()
Label5.Text = TextBox3.Width
Label7.Text = Me.Width
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As Integer
Dim b As Integer
a = TextBox1.Text
b = TextBox2.Text
TextBox3.Text = a + b
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim a As Integer
Dim b As Integer
a = TextBox1.Text
b = TextBox2.Text
TextBox3.Text = a - b
End Sub
End Class
While everything works correctly for the addition and subtraction, it does not display the Form and output Textbox width in the Windows Form.
I think, Form_Load() is not executing properly.
I also tried, Form_Activate() but that did not work either.
Once I am able to do this, I would like to extend this concept to resize the output Textbox along with the Form resize. However, for the purpose of understanding I wanted to see if I can execute Form_Load() successfully.
Thanks.
Form_Load doesn’t execute. For now, it’s just any other method. In order to tell VB to make this method handle the Load event, you need to tell it so:
Private Sub Form_Load(sender As Object, e As EventArgs) Handles MyBase.Loasd
Label5.Text = TextBox3.Width
Label7.Text = Me.Width
End Sub
(And add the required parameters for the event.)
A few other remarks:
Ensure that Option Strict On is enabled in your project options at all times. This will make the compiler much stricter with your code and flag more errors. This is a good thing since these errors are potential bugs. In particular, your code is very lax with conversions between different data types, these should be made explicit.
Initialise variables when you declare them, don’t assign a value in a separate statement. That is, write this:
Dim a As Integer = Integer.Parse(TextBox1.Text)
(Explicit conversion added as well.)
If you want to make a control fill the form, you can just set its Dock property appropriately in the forms editor, instead of having to program this manually.
You need to add the Handle so the app executes it automatically:
Private Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
'...
End Sub