i created this sub
Sub CreateNewNode(tree As TreeView, e As NodeLabelEditEventArgs)
Dim nodeTxt As String
nodeTxt = e.Label
If e.Node.Level = 0 Then
Dim obj_carsType As New Cls_carsType
Dim Entity As New tblcarsType
Entity.Type = nodeTxt
obj_carsType .Insert(Entity)
Dim q = (From i In obj_logsType.Fill Select i.ID).Last
e.CancelEdit = True
tree.Nodes.Remove(e.Node)
tree.Nodes.Add(nodeTxt & " : " & q.tostring)
end sub
Sub TreeView1_NodeMouseClick()
e.Node.ContextMenuStrip =ContextMenuStrip1
end sub
Private Sub NEWITEmToolStripMenuItem_Click()
end sub
in last sub need to call first sub. also in last sub if user click on NEWITEM i must call the first sub how can i do it?? please help me
I think you need something like this:
Private Sub NEWITEmToolStripMenuItem_Click(byval sender as object, byval e as eventargs)
CreateNewNode(ctype(sender, TreeView), ctype(e,NodeLabelEditEventArgs))
end sub
Related
I am having trouble looping through my playlist of mp3 tracks.
Can anybody shed some light on where I am going wrong. This is the code. It play the first track, but then stops.
Code has been edited 011020
' The following Menu Item plays the full list of mp3s in the list box
Private Sub PlayListToolStripMenuItem_Click(sender As Object, e As EventArgs)
Handles PlayListToolStripMenuItem.Click
FromDictionary = False
PlaySongsFromListBox()
End Sub
Private Sub PlaySongsFromListBox()
'MsgBox("Index Counter = " & NextIndex)
'MsgBox("Total No of Tracks = " & ListBox1.Items.Count)
If NextIndex = ListBox1.Items.Count Then
NextIndex = 0
MessageBox.Show("All music has been played")
Exit Sub
End If
Dim item As KeyValuePair(Of String, String) =
DirectCast(ListBox1.Items(NextIndex), KeyValuePair(Of String, String))
Dim SongPath = Path.Combine(item.Value, item.Key)
PlayASong(SongPath)
End Sub
Private Sub PlayASong(SongPath As String)
MsgBox("Playing Track: " & SongPath)
CurrentMediaStreamName = SongPath
MediaPlayer1.URL = SongPath
MediaPlayer1.Ctlcontrols.play()
MsgBox("Playing Track: " & SongPath)
End Sub
Private Sub MediaPlayer1_PlayStateChange(sender As Object, e As
AxWMPLib._WMPOCXEvents_PlayStateChangeEvent) Handles
MediaPlayer1.PlayStateChange
If MediaPlayer1.playState = WMPLib.WMPPlayState.wmppsMediaEnded Then
NextIndex += 1
PlaySongsFromListBox()
End If
End Sub
At the class level (Form1) I declared 4 variables that are used in more than one method.
In the Form.Load I filled the MusicDictionary. You may be filling this from a database or text file. I also bound the ListBox to the dictionary. The ListBox.DataSource cannot take a Dictionary directly but a BindingSource can handle it.
PlayASong simply does what it says.
PlaySongsFromDictionary first checks if we have come to the end of the Dictionary. Note that I reset NextIndex back to 0. I used Path.Combine to get the path for the song. Notice that in one dictionary entry, I left out a back slash at the end of the directories and in the other entry I included the backslash. Path.Combine accepted both and added a back slash where necessary. Also notice that the index is applied to the Keys collection and the Values collection of the Dictionary not the Dictionary itself. If we tried to apply the index to the dictionary it would be trying to look for the Key inside the brackets. Default Public Property Item(key As TKey) As TValue
PlaySongsFromListBox casts the list box item (which is an object) back to its underlying type, KeyValuePair.
Finally, the important part! I used the MediaEnded event of the MediaPlayer1 to determine when to play the next song. Here I incremented the index and called the one of the PlaySongs... methods.
Private NextIndex As Integer
Private WithEvents MediaPlayer1 As New MediaPlayer
Private MusicDictionary As New Dictionary(Of String, String)
Private FromDictionary As Boolean
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MusicDictionary.Add("2018-11-27_-_Track_A_-_FesliyanStudios.com_By_Stephen_Bennett.mp3", "C:\Users\maryo\Documents\Marys Music")
MusicDictionary.Add("file_example_MP3_700KB.mp3", "C:\Users\maryo\Documents\Marys Music\")
ListBox1.DataSource = New BindingSource(MusicDictionary, Nothing)
ListBox1.DisplayMember = "Key"
ListBox1.ValueMember = "Value"
End Sub
Private Sub PlayASong(SongPath As String)
MediaPlayer1.Open(New Uri(SongPath))
MediaPlayer1.Play()
End Sub
Private Sub PlaySongsFromDictionary()
If NextIndex = MusicDictionary.Count Then
NextIndex = 0
MessageBox.Show("All music has been played")
Exit Sub
End If
Dim SongPath = Path.Combine(MusicDictionary.Values(NextIndex), MusicDictionary.Keys(NextIndex))
PlayASong(SongPath)
End Sub
Private Sub PlaySongsFromListBox()
If NextIndex = ListBox1.SelectedItems.Count Then
NextIndex = 0
MessageBox.Show("All music has been played")
Exit Sub
End If
Dim item As KeyValuePair(Of String, String) = DirectCast(ListBox1.SelectedItems(NextIndex), KeyValuePair(Of String, String))
Dim SongPath = Path.Combine(item.Value, item.Key)
PlayASong(SongPath)
End Sub
Public Sub MediaPlayers1_MediaEnded() Handles MediaPlayer1.MediaEnded
NextIndex += 1
If FromDictionary Then
PlaySongsFromDictionary()
Else
PlaySongsFromListBox()
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
FromDictionary = False
PlaySongsFromListBox()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
FromDictionary = True
PlaySongsFromDictionary()
End Sub
The problem with the MP3's not looping was solved using the following invoke method
If e.newState = WMPLib.WMPPlayState.wmppsStopped Then
Me.BeginInvoke(New Action(AddressOf NextSong))
End If
Found at the following location Function call only works when MessageBox.Show() is included?
This program is supposed to accept in valid candidates for voting, add the names typed in a text box to a list box. In the list box the user may double click on the candidate they choose. After the tally button is clicked a list box displaying the candidates' Names and votes will appear along side the other list box.
My problem is that the lstTallies only displays the last voted candidate.
Below is my code
Public Class Form1
Dim maxVotes As Integer
Dim winner As String
Dim votes() As Integer
Dim index As Integer
Dim candidates As String
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
If Not isValidInput(txtNewCandidate.Text) Then
Exit Sub
End If
lstCandidates.Items.Add(txtNewCandidate.Text)
txtNewCandidate.Clear()
txtNewCandidate.Focus()
ReDim Preserve votes(index)
index += 1
End Sub
Private Function isValidInput(ByRef firstName As String) As Boolean
If IsNumeric(txtNewCandidate.Text) Or txtNewCandidate.Text = "" Then
MsgBox("Please input a valid candidate name.")
txtNewCandidate.Focus()
Return False
Else
Return True
End If
End Function
Private Sub btnTally_Click(sender As Object, e As EventArgs) Handles btnTally.Click
lstTallies.Visible = True
lblTally.Visible = True
lstTallies.Items.Add(lstCandidates.Text & " " & votes(lstCandidates.SelectedIndex))
End Sub
Private Sub lstCandidates_DoubleClick(sender As Object, e As EventArgs) Handles lstCandidates.DoubleClick
If lstCandidates.SelectedIndex = -1 Then
MsgBox("Select a candidate by double-clicking")
End If
votes(lstCandidates.SelectedIndex) += 1
MsgBox("Vote Tallied")
End Sub
End Class
Try this:
Assuming the index of the Candidate and his/her Vote are the same:
Private Sub btnTally_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnTally.Click
lstTallies.Visible = True
lblTally.Visible = True
For i = 0 To lstCandidates.Items.Count - 1
lstTallies.Items.Add(lstCandidates.Items(i).ToString & " - " & votes(i))
Next
End Sub
You cannot get the contents of the ListBox unless you iterate it.
I want to be able to select an index on my ListBox.My ListBox is getting getting values depending on the selected index, however when I try to select an index in the listbox, it gets the values but stays unselected. I have to select the index a second time for it to be selected. Why is that?
here is my code on the selectedIndexChanged:
Private Sub listResults_SelectedIndexChanged(sender As Object, e As EventArgs) Handles listResults.SelectedIndexChanged
UpdateContactInformationFromRegistry()
End Sub
The UpdateContactInformationFromRegistry() method:
Private Sub UpdateContactInformationFromRegistry()
Dim contact As Contact = m_contacts.GetContact(listResults.SelectedIndex)
cmbCountries.SelectedIndex = DirectCast(contact.AddressData.Country, Integer)
txtFirstName.Text = contact.FirstName
txtLastName.Text = contact.LastName
txtStreet.Text = contact.AddressData.Street
txtZip.Text = contact.AddressData.ZipCode
txtCity.Text = contact.AddressData.City
End Sub
UPDATE V2
cmbCountries.SelectedIndexChanged event handler
Private Sub cmbCountries_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbCountries.SelectedIndexChanged
UpdateGUI()
End Sub
UpdateGUI() method
Private Sub UpdateGUI()
Dim strContacts() As String = m_contacts.GetContactInfo()
If (strContacts IsNot Nothing) Then
listResults.Items.Clear()
listResults.Items.AddRange(strContacts)
lstCount.Text = listResults.Items.Count.ToString()
End If
End Sub
I am trying to create a Function that does everything automatically. Here is my current code:
Public Sub IncrementValueBeta(SlideDescription As String, SlideNumber As Integer, FormName As String)
ChangeSlide (SlideNumber)
MsgBox ("Test: " + SlideDescription)
AddClicks = FormName.ClickedTimes.Text + 1
FormName.ClickedTimes.Text = AddClicks
End Sub
This will add a number to the value but I am trying to make the code more concise without having to do if FormName = "Slide1" a 1000 times as it is a huge questionnaire.
The user will type "FormName" e.g., "Form1". In the code, it will use it like FormName.ClickedTimes.Caption = AddClicks so in the slide if the slide was 25 clicks it would be 26 that's already working but only if I do Slide3 not FormName is there a way I can do this? If you know how, can you help me because it will be a real pain if I have to do If bah = "bah" then elseif bah = "bah" then 1000 times.
This will basically Change the text of the Form label, to Eg, the user has clicked yes i like sports, the code will load the module/class and change label1.text to +1 clicks and it will change label2.text to "Question 2" but it doesn't know what form it is so it will use the text in FormName that was given with the arguments to find out what form it is editing. thats what im trying to accomplish here
In short, i just want it to find the form from FormName and View FormName.Label1.text = "" as Form1.Label1.text = ""
*Assuming there is only one instance of the desired Form, try something like this:
Public Sub IncrementValueBeta(SlideDescription As String, SlideNumber As Integer, FormName As String)
ChangeSlide(SlideNumber)
Dim frmTarget As Form = Nothing
For Each frm As Form In Application.OpenForms
If frm.Name.ToUpper = FormName.ToUpper Then
frmTarget = frm
Exit For
End If
Next
If Not IsNothing(frmTarget) Then
Dim matches() As Control = frmTarget.Controls.Find("ClickedTimes", True)
If matches.Length > 0 Then
AddClicks = matches(0).Text + 1
matches(0).Text = AddClicks
End If
End If
End Sub
I would also go with the way of using dictionaries.
Public Class Form1
Private slides As New Dictionary(Of Integer, String)
Private currentSlideNumber As Integer
Private currentSlideDescription As String
Public Sub IncrementByClick()
currentSlideDescription = slides(currentSlideNumber + 1)
currentSlideNumber += 1
ChangeSlide(currentSlideDescription)
End Sub
Public Sub ChangeSlide(ByVal slideDescription As String)
currentSlideNumber = SelectSlideNumberByDesc(slideDescription)
currentSlideDescription = slides(currentSlideNumber)
MessageBox.Show("Successfully changed the slide to slide no. " & currentSlideNumber)
End Sub
Private Function SelectSlideNumberByDesc(ByVal slideDesc As String)
Dim slideNumber As Integer
For Each slide In slides
If slide.Value = slideDesc Then
slideNumber = slide.Key
Exit For
End If
Next
Return slideNumber
End Function
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
slides.Add(1, "SLIDE_ONE")
slides.Add(2, "SLIDE_TWO")
slides.Add(3, "SLIDE_THREE")
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ChangeSlide(TextBox1.Text)
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
IncrementByClick()
End Sub
End Class
With by clicking button1, the slide will change according to the slide description the user have entered in the textbox.
By clicking btnNext, next slide will load accordingly.
OO way is way better but this should be enough to show it.
I have an order form I created in VB.NET and I have a ListBox that is populated by order. You can double click on the order and it populates the order number in the order form. The problem I'm having is that it populates the TextBox with both the order number and the persons name. How can I use a delimiter to only pull out the order number and not the name also.
Imports Business_Objects
Public Class frmSummary
Private ctrl As Controller
Dim listID As ArrayList
Private Sub frmSummary_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ctrl = CType(MdiParent, frmMain).ctrl
Dim list As ArrayList
list = ctrl.GetOrders
Dim order As Business_Objects.Order
For Each order In list
lstOrders.Items.Add(order.ID & "," & " " & order.Server)
Next
End Sub
Private Sub lstOrders_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstOrders.DoubleClick
Dim result As Boolean = False
If lstOrders.Text <> "" Then
result = True
Dim frm As New OrderForm
frm.MdiParent = Me.MdiParent
frm.Show()
frm.txtOrderNo.Text = lstOrders.Text
frm.btnFetch.PerformClick()
Else
MessageBox.Show("there are no orders here to click")
End If
End Sub
Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
lstOrders.Items.Clear()
ctrl = CType(MdiParent, frmMain).ctrl
Dim list As ArrayList
list = ctrl.GetOrders
Dim order As Business_Objects.Order
For Each order In list
lstOrders.Items.Add(order.ID & " " & order.Server)
Next
End Sub
End Class
If all of your data is being stored as a single field, or something like:
4322305 John Smith Carrots $3.00
845825 Sam White Oranges $1.25
Then you can read each record as a string, and then use split that into an array based on " " as your delimiter.
The code would look something like:
dim myArray as string() = myLongTextRecord.Split(" ")
And in that format,
textBoxName.Text = myArray[1]
You're almost there. You could use the split function, but another approach would be to add the Order object directly to the listbox and not text.
Private Sub frmSummary_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
ctrl = CType(MdiParent, frmMain).ctrl
Dim list As ArrayList
list = ctrl.GetOrders
Dim order As Business_Objects.Order
For Each order In list
lstOrders.Items.Add(order)
Next
End Sub
Private Sub lstOrders_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstOrders.DoubleClick
Dim result As Boolean = False
If lstOrders.Text <> "" Then
result = True
Dim frm As New OrderForm
frm.MdiParent = Me.MdiParent
frm.Show()
frm.txtOrderNo.Text = DirectCast(lstOrders.SelectedItem, Order).ID.ToString
frm.btnFetch.PerformClick()
Else
MessageBox.Show("there are no orders here to click")
End If
End Sub
You'll need to go into the Order object and override the .ToString function so that the text in the Listbox displays whatever value you want (ie. Return ID & "," & " " & Server)