Making checked boxes put multiline text into a label - vb.net

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

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

search and count occurances of a word in a textbox string

Right now I have two textboxes. One is the input and the other the text string. It functions perfectly. Is there a way to get rid of the input box and have the "searched" word in the code so when I hit a button it gives the result.
Private Function FindWords(ByVal TextSearched As String, ByVal Paragraph As String) As Integer
Dim location As Integer = 0
Dim occurances As Integer = 0
Do
location = TextSearched.IndexOf(Paragraph, location)
If location <> -1 Then
occurances += 1
location += Paragraph.Length
End If
Loop Until location = -1
Return occurances
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = TextBox2.Text ' display result
Label2.Text = FindWords(TextBox1.Text, TextBox2.Text)
' MsgBox("The word " & TextBox2.Text & " has occured " & FindWords(TextBox1.Text, TextBox2.Text) & " times!!")
End Sub
There are a couple of small problems that you'll might see of you turn on option strict in your project's compile settings. Your function returns an integer. To properly assign it to a textbox label, you should really add .ToString to the end of the assignment like this
Label2.Text = FindWords(TextBox1.Text, TextBox2.Text).ToString
So leaving you code as you wrote it, in this sub, you just want to pass a string as the parameter for the text to find?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = TextBox2.Text ' display result
Label2.Text = FindWords(TextBox1.Text, TextBox2.Text)
' MsgBox("The word " & TextBox2.Text & " has occured " & FindWords(TextBox1.Text, TextBox2.Text) & " times!!")
End Sub
Just change it to this
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dim searchString as string="Hello" 'or whatever string you want to use
Label1.Text = TextBox2.Text ' display result
Label2.Text = FindWords(searchString, TextBox2.Text)
' MsgBox("The word " & TextBox2.Text & " has occured " & FindWords(TextBox1.Text, TextBox2.Text) & " times!!")
End Sub
but it might be better to change it a bit more so that your function only gets executed once if you want to display a messagebox with the number of occurences - like this
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dim searchCount as Integer
dim searchString as string="Hello" 'or whatever string you want to use
Label1.Text = TextBox2.Text ' display result
searchCount = FindWords(searchString, TextBox2.Text)
Label2.Text = searchcount.ToString
' MsgBox("The word " & TextBox2.Text & " has occured " & searchcount.Tostring & " times!!")
End Sub

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.

Add text to richbox without deleting

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

IMAPI2 in VB 2008 progress bar

I start making program that will burn CD/DVDs, and everything is okay.
I found way to burn with IMAPI2 API, but now I have problem:
I can't get progress bar of that burning.
Here is code:
Dim CDD1 As New IMAPI2.MsftDiscMaster2
Dim CDD2 As New IMAPI2.MsftDiscRecorder2
Dim FSI As New IMAPI2FS.MsftFileSystemImage
Dim CDD3 As New IMAPI2.MsftDiscFormat2Data
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Index = 0
Dim UniqueID = ""
Dim Directory
Dim Path = "C:\lll"
Dim result
Dim Stream
Label1.Text = "----- Started -----."
UniqueID = CDD1.Item(Index)
Label1.Text = Label1.Text & vbCrLf & "ID found: " & UniqueID
CDD2.InitializeDiscRecorder(UniqueID)
Label1.Text = Label1.Text & vbCrLf & "Recorder selected!"
Directory = FSI.Root
Label1.Text = Label1.Text & vbCrLf & "Directory is here: " & Directory.ToString
CDD3.Recorder = CDD2
Label1.Text = Label1.Text & vbCrLf & "Recorder 2 selected!"
CDD3.ClientName = "IMAPI2 TEST"
Label1.Text = Label1.Text & vbCrLf & "Client Name Selected!"
FSI.ChooseImageDefaults(CDD2)
Label1.Text = Label1.Text & vbCrLf & "Default selected!"
Directory.AddTree(Path, False)
Label1.Text = Label1.Text & vbCrLf & "Directory added!"
result = FSI.CreateResultImage()
Stream = result.ImageStream
Label1.Text = Label1.Text & vbCrLf & "Writing content to disc..."
If (CDD3.IsCurrentMediaSupported(CDD2) = True) Then
If (CDD3.IsRecorderSupported(CDD2) = True) Then
CDD3.Write(Stream)
Else
MsgBox("Not Suported Recorder!")
End If
Else
MsgBox("Not Suported Media!")
End If
Label1.Text = Label1.Text & vbCrLf & "----- Finished -----"
End Sub
When command
CDD3.Write(Stream)
is triggered, program freeze, and don't respond until data is burned completely.
Is there any way to stop this, to stop program freezing and enabling progress bar?
Thanks.
You need to use threading. So in your button click event handler you start off a new thread that does the actual burning and while that's going on in it's separate thread, the main thread can continue to update the GUI (including your progress bar).
See Thread.Start for a simple sample and if you want further information I'd suggest starting here: Managed Threading