I'm trying to make the label's time change every second to match the time of the media being played.
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim remain As Int32 = AxWindowsMediaPlayer1.currentMedia.duration - AxWindowsMediaPlayer1.Ctlcontrols.currentPosition
Dim Minutes As String
Dim Secs As String
Dim ElapsedTime As String
Minutes = (remain / 60).ToString("00")
Secs = (remain Mod 60).ToString("00.00")
ElapsedTime = Minutes & "." & Secs
Label1.Text = ElapsedTime ' Display the remaining time for the .wav file in a label in mm:ss format
TrackBar2.Value = ElapsedTime (Error with this line: Conversion from string "11.26.00" to type 'Integer' is not valid.)
End Sub
Do I change the strings to integers instead?
I think its a matter of the compiler not being able to implicitly convert the values, I would either change the format of the string or do something like this:
Dim totalTime as Integer = 0
For Each thing In ElapsedTime.Split(".")
totalTime += CInt(thing) 'So, "11.26.00" will turn into 11 + 26 + 0 = 37 for totalTime
Next
That's just a simple outline, you can do any additional arithmetic as needed.
As per the docs here, a trackbar needs to have its value set to an integer somewhere between the minimum and maximum.
That will be 0 and 10 if you're using the default minimum and maximum, or something else if you've changed them (obviously).
You need to figure out how to change the time string (or time remaining integer, remain) into a value between that minimum and maximum, then set the value to that.
Since you have the duration and position, you can possibly use something like:
Dim pos as Int32 = AxWindowsMediaPlayer1.Ctlcontrols.currentPosition
Dim tot as Int32 = AxWindowsMediaPlayer1.currentMedia.duration
Dim tenthsToGo as Int32 = 10 * (tot - pos) / tot
' Now use tenthsToGo (or 10-tenthsToGo depending on direction) to set trackbar '
If you have set the maximum to be the duration (and left the minimum at zero) as seems to be indicated in a comment, then you can simply set the trackbar value to the current position (which varies from 0 to the maximum).
can you use this code to display time of media player when played and duration of song or video it play sory 4 bad english
TrackBar2.Maximum = AxWindowsMediaPlayer1.Ctlcontrols.currentItem.duration
Label3.Text = AxWindowsMediaPlayer1.Ctlcontrols.currentPositionString
TrackBar2.Value = AxWindowsMediaPlayer1.Ctlcontrols.currentPosition
Label4.Text = AxWindowsMediaPlayer1.Ctlcontrols.currentItem.durationString
Related
First of all, I would like you to know that I am really rookie on this platform and in vb.net language. I have a question, I wanted to ask you, since I couldn't make any progress in about 3 hours. I think it's very simple for someone who knows.
If the number entered from the textbox is odd, multiply by 3 and add 1, if it is double, this process will be divided by 2, and this process should continue until the number is "1". I try to write the code of the program that finds how many steps this process takes (number of processes), the maximum value of the number during the process and the number that the number always reaches 1 in pairs with VB.NET.
Is there anyone who can help? I want you to know that I was really struggling, trying to learn, but not enough
As I said, I scribbled something, but I am not even on the right track.
enter image description here
enter code here
Dim number1 As Double
Dim tislem As Double
Dim result As Double
Dim çislem As Double
Dim i As Double
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
number1 = TextBox1.Text
çislem = number1 / 2
tislem = number1 * 3 + 1
If number1 Mod (2) = 1 Then
result = tislem
MessageBox.Show("sayı tektir" + result.ToString())
For i = 1 To result = 0
result = number1 / 2
Next i
MessageBox.Show("sayı tektir" + result.ToString())
Else MessageBox.Show("sayı çifttir")
End If
End Sub
I'm not sure if I understood exactly what you're trying to do (neither of us have english as a first language it seems). I'm still trying, but make sure that you understand what I'm doing or I may lead you off target.
It seems to me that the worst problem here is using a For loop instead of a While loop. You could also do without some of these variables. Here's some code to give you a hand:
' I suggest that you add these two lines at the very top of your code
' It'll force you to type your variables, which is a great way to avoid all kind of mistakes
Option Explicit On
Option Strict On
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Here I'm protecting you against errors caused if the user enters a non-numeric
' Your teached probably didn't ask for this but it's good to know nonetheless
If Not String.IsNullOrWhiteSpace(TextBox1.Text) AndAlso IsNumeric(TextBox1.Text) Then
' Converting text to double (it was done implicitely in your code, but it's better to understand what's happening to better control it)
Dim myNumber As Double = CDbl(TextBox1.Text) ' CDbl as in 'Convert DouBLe'
Dim maxNumber as Double = myNumber
' If the number is odd, we multiply by 3 and add 1
If myNumber Mod 2 = 1 Then
myNumber = (myNumber * 3) + 1
If myNumber > maxNumber Then maxNumber = myNumber
End If
' Let's count how many divisions before there's only 1 left
Dim i As Integer = 0
While (myNumber > 1)
myNumber = myNumber / 2
i += 1
End While
MessageBox.Show("It took " & i.ToString & " divisions until my number was reduced to 1. The max number was " & maxNumber.ToString & ".")
Else
' This will appear if you try to use non-numeric, like letters
MessageBox.Show("Use only numbers.")
End If
End Sub
Have fun!
Try something like this,
Dim number1 As Double
Dim maxNumber As Double = 0
Dim steps as Double = 0
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
number1 = TextBox1.Text
MessageBox.Show($"Sayı {If( (number1 Mod 2) = 1, "tektir", "çifttir") }")
While number1 > 1
number1 = If((number1 Mod 2) = 1, number1 * 3 + 1, number1 * 2)
maxNumber = Math.Max(number1, maxNumber)
steps += 1
End While
MessageBox.Show($"Steps: {steps}{Environment.NewLine}Max number reached: {maxNumber}")
End Sub
You should write a proper loop in order to repeat this process until you reach 1. Moreover, it is better for you that if you get used to use & symbol instead of + symbol to concat strings.
I have a masked textbox with time value ("hh:mm" the duration the task wil take) and a combo box with 1-10.
How do I mutilply the masked textbox with the combo box to get the total dutation it would take in "hh:mm.
I have txttime1 as the first textbox that holds the time. (masked textbox)
Cmb1 with 1-10, this indicates how many times the txttime1 must be multiplied.
Txtdura1 as the answer to the multiplication. Basically the total duration of the event.
Any direction would be great.
Convert the string from the textbox to a TimeSpan. Then change to TotalMinutes which is a Double so you can perform multiplication. Take the result of the multiplication and change it to a TimeSpan .FromMinutes. You can then format the result in various ways.
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim ts As TimeSpan = TimeSpan.ParseExact(TextBox2.Text, "h\:m", CultureInfo.CurrentCulture)
Dim dblTotalMinutes As Double = ts.TotalMinutes
Dim Multiplier As Integer = CInt(ComboBox1.SelectedItem)
Dim MultipliedMinutes As Double = dblTotalMinutes * Multiplier
Dim MultipliedTimeSpan As TimeSpan = TimeSpan.FromMinutes(MultipliedMinutes)
'For days:hours:minutes
TextBox3.Text = MultipliedTimeSpan.ToString("d\:hh\:mm")
'Total Hours as double
TextBox4.Text = MultipliedTimeSpan.TotalHours.ToString
End Sub
Split the hours and minutes apart from each other, create a TimeSpan object and multiply that by the factor from the ComboBox:
Dim duration As TimeSpan = TimeSpan.Parse(txttime1.Text)
Txtdura1 = duration.Multiply(Integer.Parse(Cmb1.Text)).ToString("hh\:mm")
Relevant documentation:
TimeSpan.Parse()
TimeSpan.Multiply()
TimeSpan.ToString()
I am working on project for school, using VB, I am working in Visual Studio 2017.
I have a DataGridView which I need to export to a Text File.
I could use some help with an export feature from VB to a Text file. Here is the code I am using:
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim numCols As Integer = dgvApplianceList.ColumnCount
Dim numRows As Integer = dgvApplianceList.RowCount - 1
Dim strDestinationFile As String = "exportappliance.txt"
Dim tw As TextWriter = New StreamWriter(strDestinationFile)
'writing the header
For count As Integer = 0 To numCols - 1
tw.Write(dgvApplianceList.Columns(count).HeaderText)
If (count <> numCols - 1) Then
tw.Write(vbTab)
End If
Next
tw.WriteLine()
For count As Integer = 0 To numRows - 1
For count2 As Integer = 0 To numCols - 1
tw.Write(dgvApplianceList.Rows(count).Cells(count2).Value)
If (count2 <> numCols) Then
tw.Write(vbTab)
End If
Next
tw.WriteLine()
Next
tw.Close()
End Sub
Since you are writing to a “text” file, one way to line up text properly can be accomplished using spaces as others have suggested. This would require that you have a “defined” column “width” for each column. Using your picture as an example, column 0 (zero) would be “Appliance Type” and we could give that column a max “width” of… say twenty five (25) characters wide. Column 2 “kwh” could be set with a maximum column width of 15 and so on for each column.
With the “column widths” established, it should be a simple matter of adding X number of spaces needed to fill the string to the columns width. Example, to make sure column 2 lines up with the next column 2, each column 1 string MUST be all the same length. By filling each column 1 string with spaces to “fill” the string to column 1’s length, will ensure column 2’s text will line up correctly. Obviously, the same logic applies to subsequent columns.
The GetBufferedString method (below) demonstrates one way to buffer the strings to a specified column width. The method takes a string originalString, an int maxLength and a justification type. The method will return a string of length maxLength such that, if the justification type is LEFT, the method will fill the given string with spaces at the end. If the justification type is RIGHT, the method will return a string of maxLength such that spaces are added to the front of the string. Finally, if the justification type is CENTER, then the method will return a string with half the spaces in front of the string and the other half at the end. If the given string’s length is greater than maxLength, then the returned string will be a maxLength truncation of the given string.
This should enable you to set each columns justification type independently. The code below simply sets each rows justification type to right.
This is an example and I hope it helps, however there is no error checking for a possible mismatch on the number of actual columns in the grid and the number of column widths.
Some global variables… an integer array columnLengths is used to hold each columns width… also an enumeration for the justification type; RIGHT, LEFT, CENTER.
Dim columnLengths(6) As Integer
Enum JustifyType
LEFT
RIGHT
CENTER
End Enum
Set each columns width…
Private Sub FillColumnLength()
columnLengths(0) = 25
columnLengths(1) = 12
columnLengths(2) = 12
columnLengths(3) = 12
columnLengths(4) = 12
columnLengths(5) = 12
End Sub
An updated save button click event to use the GetBufferedString method.
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim numCols As Integer = dgvApplianceList.ColumnCount
Dim numRows As Integer = dgvApplianceList.RowCount - 1
Dim strDestinationFile As String = "D:\Test\exportappliance.txt"
Dim tw As TextWriter = New StreamWriter(strDestinationFile)
Dim textToOutput = ""
For count As Integer = 0 To numCols - 1
textToOutput = GetBufferedString(dgvApplianceList.Columns(count).HeaderText, columnLengths(count), JustifyType.CENTER)
tw.Write(textToOutput)
Next
tw.WriteLine()
For count As Integer = 0 To numRows - 1
For count2 As Integer = 0 To numCols - 1
textToOutput = GetBufferedString(dgvApplianceList.Rows(count).Cells(count2).Value, columnLengths(count2), JustifyType.RIGHT)
tw.Write(textToOutput)
Next
tw.WriteLine()
Next
tw.Close()
End Sub
Finally, the GetBufferedString method.
Private Function GetBufferedString(originalString As String, maxLength As Int16, justifyType As JustifyType) As String
If (originalString.Length < maxLength) Then
Dim bufString = Space(maxLength - originalString.Length)
Select Case justifyType
Case JustifyType.LEFT
Return originalString + bufString
Case JustifyType.RIGHT
Return bufString + originalString
Case JustifyType.CENTER
Dim halfString = bufString.Substring(bufString.Length / 2)
originalString = halfString + originalString
bufString = Space(maxLength - originalString.Length)
Return originalString + bufString
Case Else
Return ""
End Select
Else
Return originalString.Substring(0, maxLength)
End If
End Function
Hope this helps.
I am getting an integer string. It could be 512 or it could be 15215534. The first one would be 5 min. 12 seconds. The 2nd number would be 1521 hours, 55 minutes, and 34 seconds. This is what I am using now:
Dim input As String
Dim time As TimeSpan
Dim length As Integer
length = input.Length
'Return 0 if null
If length = 0 Then
result = 0
Return result
End If
time = New TimeSpan(0, input.Substring(0, length - 4), input.Substring(length - 4, 2), input.Substring(length - 2, 2), 0)
The code now errors for inputs with 4 character lengths or smaller. Is my best solution to use if statements for the different lengths or is there a better, built in way to do this?
I would do it as follows:
Dim input as String = "2133"
input = input.PadLeft(5, '0')
Dim seconds = Convert.ToInt32(input.Substring(input.Length - 2, 2))
Dim minutes = Convert.ToInt32(input.Substring(input.Length - 4, 2))
Dim hours = Convert.ToInt32(input.Substring(0, input.Length - 4))
Dim time = new TimeSpan(hours, minutes, seconds)
Basically, you know you'll have at most 2 digits for seconds at the end, the next 2 would be minutes and anything preceding that would be hours.
The challenge would be if you don't have minutes or hours in your string (or even just single seconds) - So, you pad your string with enough 0s at the start to ensure you won't get any issues and then parse out the numbers you need.
I wrote it in C# and converted it, but I hope it still works.
Forget that code John Bustos did the job way better!
Easiest would probably be to just make your input become a total seconds value and then do the maths.
If you cant you gotta make sure you have a syntax reading the number from right to left.
Dim Number as String = ActualNumber.ToString
Dim Seconds as Integer = 0
If Number.Length = 0 Then GoTo SkipWhatEver
If Number.Length < 3 Then
Seconds = CInt(Number)
Else
Seconds = Cint(Number.Substring(Number.Length-2,2)
End If
Number = Number.Substring(0, Number.Length-2)
Dim Minutes as Integer = 0
and so on... lots of code.
It will get pretty unreadable.
The following code assumes that the input is in TextBox1. It converts the entire string to an integer and then separates the hours, minutes and seconds before creating the Timespan.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim num, hhhh, mm, ss As Integer
Dim input As String = TextBox1.Text
If Not Integer.TryParse(input, num) Then
MessageBox.Show("Invalid time")
Exit Sub
End If
ss = num Mod 100
mm = (num \ 100) Mod 100
hhhh = num \ 10000
If ss > 59 Or mm > 59 Then
MessageBox.Show("Invalid time")
Exit Sub
End If
Dim time As TimeSpan = New TimeSpan(hhhh, mm, ss)
End Sub
I include checks to make sure that the input is really an integer and that the minutes and seconds are not more than 60.
I am trying to send a single byte to a PIC from a computer (Note: the pic only accepts a single byte else it will only accept the first byte of the data send)
This shouldn't be much of a problem because I only want to manage a total of 8 LEDS so I only need it to go from 0 to 255 but I am having issues achieving this.
If I try to send the value 1 to the pic my program sends 31 if I try to send 5 it sends 35 If I try to send 255 it sends 3*2*3*5*3*5* so for every single digit I try to send it adds a 3 in front of it. I am using the following code to determine the value and to send it:
Dim t As Integer = 0
Dim result As Integer = 0
For Each chk As CheckBox In GroupBox1.Controls
If chk.Checked = True Then
result = result + 2 ^ t
End If
t = t + 1
Next
publisher.Connect(IPTo, PortTo)
Dim sendbytes() As Byte = ASCII.GetBytes(result)
publisher.Send(sendbytes, sendbytes.Length)
I think the issue is with the convert to ASCII.
I also try to receive the inputs from the PIC to my pc for this I have the following script inside a timer:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Try
Dim ep As IPEndPoint = New IPEndPoint(IPAddress.Any, 0)
Dim rcvbytes() As Byte = subscriber.Receive(ep)
Dim translate As String
translate = System.BitConverter.ToInt32(rcvbytes, 0)
TBRcv.Text = translate
Catch ex As Exception
End Try
End Sub
ASCII.GetBytes expects a string (encoded in ASCII). Giving it an Int32 doesn't make sense.
Check out System.BitConverter.GetBytes() and System.BitConverter.ToInt32() (for the receiving end)
So the only change you'd need to make is:
Dim sendbytes() As Byte = BitConverter.GetBytes(result)
Further discussion:
What has happened is that your result (let's say 5), is first converted to string (now it's "5"), then ASCII.GetBytes will now return a byte array with a single value 0x35, because Asc("5") is 0x35.
The ASCII value of the character 1 is 0x31, etc. For reference :