Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello to all
I am beginner in visual basic and generally in programming
I want someone can help me to make a project of my Thesis
I want a little program that always saves the last sentence that when begins with 04 and always does the same function plus who I write when 04 is found it displays the text sequence.
example to clarify
when I insert "blablabalabla04BLABLABBLALA04lALALALALA"
it only gives me "lALALALALA"

What I have tried:

i start learning at visual basic and this is what i can do but it was false
Please help me. Thank you
Public Class Form1

Dim tram As String
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

While TextBox1.Text.StartsWith("04")
tram = TextBox1.Text
End While

End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
MessageBox.Show(tram)
End Sub
End Class
Posted
Updated 29-Apr-22 0:26am
Comments
Member 13566383 29-Apr-22 12:57pm    
"Good evening everyone" is not really a good headline for a question.

1 solution

Um. That isn't going to work:
VB
While TextBox1.Text.StartsWith("04")
   tram = TextBox1.Text
End While
Because TextBox1.Text doesn't change at all inside the loop, it will run forever, doing exactly the same thing.

And because Events are processed by a message based system, no other events will be serviced until your TextChanged event handler exists - which it'll never do, so the text box will never get updated, so it's text property will never change!

Plus, the String.StartsWith method does exactly what it says on the tin: it checks if the string starts with a specific other string.
"04 hello" starts with "04" but "x04 Hello" doesn't.

And even if that code did work, tram is a single string variable, which you overwrite each time round your loop - throwing away any previous data.

What would I do? I'd throw away the TextChanged handler, and use String.Split[^] to break the TextBox content into an array of strings using "04" as the separator.
 
Share this answer
 
Comments
Dammak Karim 29-Apr-22 6:56am    
in my project I make a serial communication between my pc and another device I receive the data in my pc in series and do the accumulation with each other
so I want to limit the size of this flux so I want to do when it finds "04" it starts accumulating data then it stops with the next "04" and like the example I describe above
Dammak Karim 29-Apr-22 6:59am    
Imports System.IO.Ports

Public Class Form1
Dim dataIN As String
Dim dataOUT As String
Private Delegate Sub AccesDelegue(ByVal AjouterText As String)

Private Sub AccesFormPrinpal(ByVal TextForms As String)
dataOUT = TextForms
dataIN=Replace(dataIN,"04", Environment.NewLine & "04")
TxtDonnesRecus.Text += dataOUT
End Sub

Private Sub PortAcceesInteruption(ByVal bufferin As String)

Dim textinteruption As Object = {bufferin}

Dim delegateinteruption As AccesDelegue
delegateinteruption = New AccesDelegue(AddressOf AccesFormPrinpal)
MyBase.Invoke(delegateinteruption, bufferin)

End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
dataIN = ""
dataOUT = ""
BtnConnecter.Enabled = False
BtnEnvoyesData.Enabled = False


End Sub

Private Sub BtnChercherPorts_Click(sender As Object, e As EventArgs) Handles BtnChercherPorts.Click
CboxPorts.Items.Clear()
For Each PortDinsponible As String In My.Computer.Ports.SerialPortNames
CboxPorts.Items.Add(PortDinsponible)
Next
If CboxPorts.Items.Count > 0 Then
CboxPorts.Text = CboxPorts.Items(0)
MessageBox.Show("selectionner le port de travail")
BtnConnecter.Enabled = True
Else
MessageBox.Show("NON Port Trouve")
BtnConnecter.Enabled = False
BtnEnvoyesData.Enabled = False
CboxPorts.Items.Clear()
End If

End Sub

Private Sub BtnConnecter_Click(sender As Object, e As EventArgs) Handles BtnConnecter.Click
If BtnConnecter.Text = "Connecter" Then
Try
With SerialPort1
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
.StopBits = IO.Ports.StopBits.One
.DataBits = 8
.PortName = CboxPorts.Text
.Open()
End With
BtnConnecter.Text = "deconnecter"
BtnEnvoyesData.Enabled = True
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
ElseIf BtnConnecter.Text = "deconnecter" Then
BtnConnecter.Text = "Connecter"
BtnEnvoyesData.Enabled = False
SerialPort1.Close()
End If



End Sub

Private Sub BtnEnvoyesData_Click(sender As Object, e As EventArgs) Handles BtnEnvoyesData.Click
SerialPort1.DiscardOutBuffer()
dataIN = TxtEnvoyerDonnes.Text
SerialPort1.Write(dataIN)
End Sub

Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim datainterruption As String
datainterruption = SerialPort1.ReadExisting
PortAcceesInteruption(datainterruption)
End Sub
End Class
this is the serial communication

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900