Add text to richbox without deleting - vb.net

i try to mekae a program how tell me in a richbox what i do for examle, i have 4 CheckBoxes when i click one enable in RichBox must apear
apple enabled...
pear disabled...
So i have apple pear carrot peanut
and richbox need look like,
Pear disabled...
Carrot disabled...
Peanut Enabled...
Pear Enabled....
SUBMIT SUCCES !
Pear disabled...
I try that for every checkbox but when i check one richbox is reset and apear only one line
If CheckBox2.Checked = True Then
RichTextBox1.Text = "pear enabled..."
Else
RichTextBox1.Text = "pear disabled..."
End If
What can i do ?
Thanks !

Maybe you need this:
If CheckBox2.Checked = True Then
RichTextBox1.Text &= "pear enabled..." & Environment.NewLine()
Else
RichTextBox1.Text &= "pear disabled..." & Environment.NewLine()
End If

You could simply use the SelectedText property of the Rich Text Box control to append text properly. Please see the following code..
Public Class Form1
Private Sub CheckBox1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox1.CheckedChanged
If (CheckBox1.Checked) Then
RichTextBox1.SelectedText = CheckBox1.Text & " Enabled" & vbCrLf
Else
RichTextBox1.SelectedText = CheckBox1.Text & " Disabled" & vbCrLf
End If
End Sub
Private Sub CheckBox2_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox2.CheckedChanged
If (CheckBox1.Checked) Then
RichTextBox1.SelectedText = CheckBox2.Text & " Enabled" & vbCrLf
Else
RichTextBox1.SelectedText = CheckBox2.Text & " Disabled" & vbCrLf
End If
End Sub
Private Sub CheckBox3_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox3.CheckedChanged
If (CheckBox1.Checked) Then
RichTextBox1.SelectedText = CheckBox2.Text & " Enabled" & vbCrLf
Else
RichTextBox1.SelectedText = CheckBox2.Text & " Disabled" & vbCrLf
End If
End Sub
End Class

The word you're looking for is "append" text.
RichTextBox1.AppendText("pear enabled...")

Related

Index Val List VB.Net

I have a Listbox called ListTxtDrawR4. Every time I select an item, I want to display the value corresponding to the index in a textbox with name TxtNumberListScan.Lines(i). for example, if I select the 5th line in ListTxtDrawR4, I want to display the value of line 5 from TxtNumberListScan in another Textbox. how do I do that?
TxtTop10HighestResult.Text &= Environment.NewLine & TxtNumberListScan.Lines(TxtTop10HighestCount.Lines(0))
TxtTop10HighestResult.Text &= Environment.NewLine & TxtNumberListScan.Lines(TxtTop10HighestCount.Lines(1))
TxtTop10HighestResult.Text &= Environment.NewLine & TxtNumberListScan.Lines(TxtTop10HighestCount.Lines(2))
TxtTop10HighestResult.Text &= Environment.NewLine & TxtNumberListScan.Lines(TxtTop10HighestCount.Lines(3))
TxtTop10HighestResult.Text &= Environment.NewLine & TxtNumberListScan.Lines(TxtTop10HighestCount.Lines(4))
TxtTop10HighestResult.Text &= Environment.NewLine & TxtNumberListScan.Lines(TxtTop10HighestCount.Lines(5))
Names of controls have been changed to protect the innocent. (I watch Dragnet :-)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'This is just some data to test with
Dim A() As String = {"Mathew", "Mark", "Luke", "John"}
Dim B() As String = {"Peter", "Paul", "James", "Judas"}
ListBox1.Items.AddRange(A)
For Each s In B
TextBox1.Text &= s & Environment.NewLine
Next
TextBox1.Text.TrimEnd() 'Remove final new line
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
Dim index As Integer = ListBox1.SelectedIndex
'Check that the index does not exceed the number of lines.
If index < TextBox1.Lines.Count Then
Dim StringToCopy = TextBox1.Lines(index)
TextBox2.Text &= StringToCopy & Environment.NewLine
End If
End Sub

How to show download progress in progressbar? Visual Basic

im so confused because this code didn't work. It downloaded the file successfully but don't report the progress to the ProgressBar. I already started Timer1 using Timer1.Start() before BackgroundWorker2.RunWorkerAsync() .
Dim size As Double
Private Sub BackgroundWorker2_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker2.DoWork
Try
Dim G As Integer = 150
Dim Increase As Boolean = True
Do Until Clicked = True
If Increase = True Then
If Not G = 255 Then
G += 1
Threading.Thread.Sleep(10)
Else
Increase = False
End If
Else
If Not G = 150 Then
G -= 1
Threading.Thread.Sleep(10)
Else
Increase = True
End If
End If
Label6.ForeColor = Color.FromArgb(0, G, 0)
Loop
Label6.Cursor = Cursors.Default
Label6.Text = "Initializing"
Label6.ForeColor = Color.Lime
MessageBox.Show("Description :" & Environment.NewLine & Description & Environment.NewLine & Environment.NewLine & "Total Size: " & Environment.NewLine & TotalSize & Environment.NewLine & Environment.NewLine & "Download Link (Global): " & Environment.NewLine & DownlaodLink, "BIOS Update Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
'WebBrowser1.Navigate(DownlaodLink)
'BackgroundWorker1.RunWorkerAsync()
ProgressBar1.Visible = True
size = TotalSize.Replace(" MBytes", "")
Me.Refresh()
Dim wc As New WebClient
wc.DownloadFileAsync(New Uri(DownlaodLink), My.Computer.FileSystem.SpecialDirectories.Desktop & "\A55BM-E BIOS " & LatestVersion.ToString.Replace(" ", "") & ".zip")
Catch ex As Exception
MsgBox(ex.Message)
End Try
And the code to show me the progress of my download
Dim cursize As Double
Dim finsize As Double
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If System.IO.File.Exists(My.Computer.FileSystem.SpecialDirectories.Desktop & "\A55BM-E BIOS " & LatestVersion.ToString.Replace(" ", "") & ".zip") Then
cursize = My.Computer.FileSystem.GetFileInfo(My.Computer.FileSystem.SpecialDirectories.Desktop & "\A55BM-E BIOS " & LatestVersion.ToString.Replace(" ", "") & ".zip").Length
finsize = cursize / size * 100
If Not ProgressBar1.Value = ProgressBar1.Maximum Then
ProgressBar1.Value = finsize
ProgressBar1.Refresh()
Else
ProgressBar1.Value = finsize
ProgressBar1.Refresh()
Timer1.Stop()
MsgBox("Finished Downloading")
End If
End If
End Sub
I can't figure out how to make this work. Can someone help me?
Finally! I made it work but didn't go with the BackgroundWorker. The code below is what I've used to make this thing work. And it's so efficient and easy to use too.
Public WithEvents downloader As WebClient
Public Sub DownloadStart()
Label6.Cursor = Cursors.Default
Label6.Text = "Initializing"
Label6.ForeColor = Color.Lime
MessageBox.Show("Description :" & Environment.NewLine & Description & Environment.NewLine & Environment.NewLine & "Total Size: " & Environment.NewLine & TotalSize & Environment.NewLine & Environment.NewLine & "Download Link (Global): " & Environment.NewLine & DownlaodLink, "BIOS Update Information", MessageBoxButtons.OK, MessageBoxIcon.Information)
ProgressBar1.Visible = True
downloader = New WebClient
downloader.DownloadFileAsync(New Uri(DownlaodLink), My.Computer.FileSystem.SpecialDirectories.Desktop & "\A55BM-E BIOS " & LatestVersion.ToString.Replace(" ", "") & ".zip")
End Sub
Private Sub downloader_DownloadProgressChanged(sender As Object, e As DownloadProgressChangedEventArgs) Handles downloader.DownloadProgressChanged
ProgressBar1.Value = e.ProgressPercentage
End Sub
Thanks everyone for helping me!

Navigate and wait for webbrowser response with sendkeys

Is it possible to open webpage, navigate once/click button and navigate page for the second time without having to use three buttons (everytihing is working fine with three buttons)?
I tried sleep (doesn't work) and SendKeys.SendWait (also not working).
Here is the code :
Dim browser As New WebBrowser
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
With browser
browser.Name = "mybrowser"
.Dock = DockStyle.Fill
.Url = New Uri("http://pretraga2.apr.gov.rs/ObjedinjenePretrage/Search/Search")
.Visible = True
End With
Me.Controls.Add(browser)
browser.Focus()
'browser.Focus()
SendKeys.SendWait("{TAB}" & "{TAB}" & "08727988" & "{ENTER}") ' & "{TAB}" & "{ENTER}")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
browser.Focus()
SendKeys.Send("{TAB}" & "{TAB}" & "20084693" & "{ENTER}") ' & "{TAB}" & "{ENTER}")
SendKeys.SendWait("{TAB}" & "{TAB}" & "{TAB}" & "{TAB}" & "{ENTER}")
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
browser.Focus()
SendKeys.Send("{TAB}" & "{TAB}" & "{TAB}" & "{TAB}" & "{ENTER}")
End Sub
Yes I think this is possible, but you have to wait till the browser finished loading. What browser-control do you use? I used SHDocVw.InternetExplorer in this example:
Public Sub browse(ByVal szURL As String, Optional ByVal bWaitTillDone As Boolean = True)
Dim nTickStart As Long
Dim nTickEnde As Long = 0
Dim i As Integer = 0
Do
Try
IE.Navigate(szURL)
If bWaitTillDone Then
nTickStart = System.Environment.TickCount
While IE.Busy
fnSleepMS(15)
nTickEnde = System.Environment.TickCount
If nTickEnde - nTickStart > IEBrowser.nMaxWaitSeconds * 1000 Then
Throw New Exception("Browse hat zu lange gedauert!")
End If
End While
End If
Exit Do
Catch ex As Exception
i += 1
If i >= 5 Then
Throw New Exception("IEBrowser.browse(): " & szURL & "--> " & ex.Message)
Else
LOG.add("IEBrowser.browse() - Versuch (" & i & ")" & ex.Message)
fnSleep(1, LOG)
End If
End Try
Loop
End Sub
Using this browser-control you can read out all ahref-Tags and Buttons and click them directly, so you are able to build up a sequence.
Hope I understood you right and this helps a bit. But please tell which browser-control you are using an in which environment (Framework + Visual Studio)

Form is sent to back after function runs

I've got the following section of code:
Public Sub SendTestEmail()
Try
Dim Mail As New MailMessage
Mail.Subject = "Test email"
Mail.To.Add(smtpTXTsendto.Text)
Mail.From = New MailAddress(smtpTXTusername.Text)
Mail.Body = "This is a test message"
Dim SMTP As New SmtpClient(smtpTXTserver.Text)
If smtpCHECKssl.Checked = True Then
SMTP.EnableSsl = True
Else
SMTP.EnableSsl = False
End If
SMTP.Credentials = New System.Net.NetworkCredential(smtpTXTusername.Text, smtpTXTpassword.Text)
SMTP.Port = smtpTXTport.Text
SMTP.Send(Mail)
MessageBox.Show("A test email has been sent." & Environment.NewLine & Environment.NewLine & "To: " & smtpTXTsendto.Text & Environment.NewLine & "From: " & smtpTXTusername.Text & "." & Environment.NewLine & Environment.NewLine & "If you did not recieve an email, please check your settings and try again.", "Test Email")
Catch ex1 As Exception
MessageBox.Show(ex1.Message)
Return
End Try
End Sub
The Sub SendTestEmail is called inside of a Background worker.
The odd issue I'm having, as that when a MessageBox appears, and I click OK the form gets sent to the back of the screen, behind all applications ...
I've tried adding a Me.focus, but it gives me issues about Cross Tread violations.
Any ideas why this is happening?
Background Worker Code:
Private Sub BGWSendTestEmail_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BGWSendTestEmail.DoWork
SendTestEmail()
End Sub
Private Sub BGWSendTestEmail_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BGWSendTestEmail.RunWorkerCompleted
If (e.Cancelled) Then
MsgBox("Something went wrong!")
Else
GroupBoxTesting.Visible = False
Me.Enabled = True
End If
End Sub
Private Sub SMTPButtonTest_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SMTPButtonTest.Click
GroupBoxTesting.Visible = True
Me.Enabled = False
BGWSendTestEmail.RunWorkerAsync()
End Sub
Change BGWSendTestEmail_RunWorkerCompleted to something like this and remove exception handling from SendTestEmail
Me.Enabled = True ' Always enable form first after completed
If e.Error Is Nothing Then
If Not e.Cancelled Then
MessageBox.Show("A test email has been sent." _
& Environment.NewLine _
& Environment.NewLine _
& "To: " & smtpTXTsendto.Text _
& Environment.NewLine _
& "From: " & smtpTXTusername.Text _
& "." & Environment.NewLine _
& Environment.NewLine _
& "If you did not recieve an email, please check your settings and try again.", _
"Test Email")
GroupBoxTesting.Visible = False 'Maybe put this also into start of method?
Else
MsgBox("Something went wrong!")
End If
Else
MessageBox.Show("Email sending failed. Exception: " & e.Error.Message)
End If
If you debug this program your debugger will attach exceptions in do_work method, but if you run it without debugger then exceptions are handled at completed method.

Making checked boxes put multiline text into a label

So I have 4 check boxes and if one of them is checked, I would like to put that text into a label.
My problem is when one box is checked, it just has that text in the label but not the other checked ones. I want it to have a new line for each box that is checked to put new line text into the label.
How would I be able to have the program go through the check boxes, and if they are checked put that text into a label and for the next box checked start a new line and put that text there?
To be put under a button click event.
Quick and dirty, but if I understand what you are asking for this should work.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Label1.Text = ""
If (CheckBox1.Checked) Then
Label1.Text &= CheckBox1.Text & vbCrLf
End If
If (CheckBox2.Checked) Then
Label1.Text &= CheckBox2.Text & vbCrLf
End If
If (CheckBox3.Checked) Then
Label1.Text &= CheckBox3.Text & vbCrLf
End If
If (CheckBox4.Checked) Then
Label1.Text &= CheckBox4.Text & vbCrLf
End If
End Sub
Put all your CheckBoxes in the same container. In this example, they are all contained within Panel1. This gets all the Text properties of the CheckBoxes which are checked, and puts the Text in Label1.Text. When nothing is selected, an Exception is thrown (Sequence contains no elements).
Try
Label1.Text = Panel1.Controls.OfType(Of CheckBox). _
Where(Function(arg) arg.Checked). _
Select(Function(arg) arg.Text). _
Aggregate(Function(aggregate, nextItem) aggregate & Environment.NewLine & nextItem)
Catch ex As Exception
Label1.Text = "Nothing selected"
End Try
Try this code,
[Note: Tested with IDE]
Private Sub Common_Cheked_Change_Handler(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles _
CheckBox1.CheckedChanged, CheckBox2.CheckedChanged, _
CheckBox3.CheckedChanged, CheckBox4.CheckedChanged
Label1.Text = String.Empty
Label1.Text = If(CheckBox1.Checked = True, CheckBox1.Text & vbCrLf, String.Empty) & _
If(CheckBox2.Checked = True, CheckBox2.Text & vbCrLf, String.Empty) & _
If(CheckBox3.Checked = True, CheckBox3.Text & vbCrLf, String.Empty) & _
If(CheckBox4.Checked = True, CheckBox4.Text, String.Empty)
End Sub