Pop up message on selectindexchanged in vb - vb.net

I was wondering how can I display a pop up warning message every time the selected item in dropdown list has changed. I've managed to get it working , but the message will unfortunately also pop up when loading the form as in some cases the default value has already been set and is not empty. So far i have this:
Private Sub cboNew_DropDown(sender As Object, e As EventArgs) Handles cboNew.SelectedIndexChanged
If cboNew.SelectedItem = "%" Then
MessageBox.Show(String.Format(" You Selected = " & cboNew.SelectedItem, MessageBoxButtons.OK))
End If
If cboNew.SelectedItem = "Value" Then
MessageBox.Show(String.Format(" You Selected = " & cboNew.SelectedItem, MessageBoxButtons.OK))
End If
If cboNew.SelectedItem = "REVOKE" Then
MessageBox.Show(String.Format(" You Selected = " & cboNew.SelectedItem, MessageBoxButtons.OK))
End If
End Sub

Sometime it's hard to determine where the error came from. You could add a boolean flag to help you out.
Private _isLoading As Boolean = False
Private Sub YourLoadingSub
_isLoading = True
' Do your loading code
_isLoading = False
End Sub
Private Sub cboNew_DropDown(sender As Object, e As EventArgs) Handles cboNew.SelectedIndexChanged
If Not _isLoading Then
' Show message box
End If
End Sub
An other option is to remove the event and put it back when needed.

The mistake is in this statement MessageBox.Show( change this as msgbox() then your code will be like the following:
Private Sub cboNew_DropDown(ByVal sender As Object, ByVal e As EventArgs) Handles cboNew.SelectedIndexChanged
If cboNew.SelectedItem = "%" Then
MsgBox(String.Format(" You Selected = " & cboNew.SelectedItem, MessageBoxButtons.OK))
End If
If cboNew.SelectedItem = "Value" Then
MsgBox(String.Format(" You Selected = " & cboNew.SelectedItem, MessageBoxButtons.OK))
End If
If cboNew.SelectedItem = "REVOKE" Then
MsgBox(String.Format(" You Selected = " & cboNew.SelectedItem, MessageBoxButtons.OK))
End If
End Sub

Related

Combobox value is not being passed to the next form(which is a module), stays empty

Form1 has a user select a value from combobox, then they click next button to go to StatusModule. I want that item selected from Form1 to be used in an If/else statement in the module to create a client and return the results. Currently, with my break point, everything works besides the value is not being passed from Welcome to StatusModule.
Form1:
Public Class Welcome
Private Sub NextButton_Click(sender As Object, e As EventArgs) Handles NextButton.Click
If AdapterSelection.Text = "USB" Or AdapterSelection.Text = "USBC" Or AdapterSelection.Text = "HDMI" Then
If ELocation.Text = String.Empty Then
EValidFalg = False
MessageBox.Show("Select an E file.")
ELocation.Text = "Select E"
Else
Me.Hide()
Form2.Show()
End If
Else
DValidFlag = False
MessageBox.Show("Select D")
End If
End Sub
Private Sub AdapterSelection_SelectedIndexChanged(sender As Object, e As EventArgs) Handles AdapterSelection.SelectedIndexChanged
If AdapterSelection.Items(0) <> "" Then
Else
AdapterSelection.Items.Add("USB")
AdapterSelection.Items.Add("USBC")
AdapterSelection.Items.Add("HDMI")
End If
End Sub
End Class
Module:
Module StatusM
Function Start()
client = CreateObject("E File")
If Err.Number <> 0 Then
'Display some error information
Screen.Display.Text = "ERROR: Fail to create client"
Else
Debug.Print("Completed")
Debug.Print(Welcome.AdapterSelection.Text) 'NOT SHOWING ANY VALUE
If Welcome.AdapterSelection.Text = "USB" Then
"My Code"
ElseIf Welcome.AdapterSelection.Text = "USBC" Then
"MyCode"
ElseIf Welcome.AdapterSelection.Text = "USBC" Then
End If
End Function
End Module

How to Auto Open or refresh or reload my Form when the date i set in reminders the same with the date in my computer?

I made a program Reminder System that Auto send email when the date i set in my apps much the date in my computer. the problem is i need to restart my apps so that the reminder will show or the form will be loaded. if not it will not alert. so i need your help guys if the date change in my computer my apps will auto reload or refresh so that if have reminders it will be send to email. btw my program will be installed in a server that will always be open so i just need to auto reload my form every time the date change. below is my code in setting the reminders.
Imports System.IO
Public Class Reminder
Dim m As MsgBoxResult
Dim t As String
Private Sub Reminder_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Label1.Text = TimeOfDay.ToString("h:mm:ss tt")
Dim m1 As MsgBoxResult
t = MonthCalendar1.SelectionRange.Start.Month.ToString & MonthCalendar1.SelectionRange.Start.Day.ToString
If Date.Today = MonthCalendar1.TodayDate And File.Exists(t & ".txt") = True Then
m1 = MsgBox("You set a reminders for today. Would you like to send to EMAIL ?", MsgBoxStyle.YesNo)
If m1 = MsgBoxResult.Yes Then
MonthCalendar1.Enabled = False
MonthCalendar1.Hide()
Notetxt.Enabled = True
Notetxt.Show()
SaveBtn.Enabled = True
SaveBtn.Show()
Backbtn.Enabled = True
Backbtn.Show()
Addbtn.Enabled = True
Addbtn.Show()
delbtn.Enabled = True
delbtn.Show()
Notetxt.Text = File.ReadAllText(t & ".txt")
sendEmail.Show()
sendEmail.emailtxt.Text = Notetxt.Text
sendEmail.Hide()
MsgBox("SEND EMAIL SUCCESSFUL", vbOKOnly)
lblMessage.Hide()
End If
End If
End Sub
Private Sub MonthCalendar1_DateChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles MonthCalendar1.DateChanged
t = MonthCalendar1.SelectionRange.Start.Month.ToString & MonthCalendar1.SelectionRange.Start.Day.ToString
Try
If File.Exists(t & ".txt") = True Then
MonthCalendar1.Enabled = False
MonthCalendar1.Hide()
Notetxt.Enabled = True
Notetxt.Show()
SaveBtn.Enabled = True
SaveBtn.Show()
Backbtn.Enabled = True
Backbtn.Show()
Notetxt.Text = File.ReadAllText(t & ".txt")
Addbtn.Enabled = True
Addbtn.Show()
delbtn.Enabled = True
delbtn.Show()
lblMessage.Hide()
Else
m = MsgBox("Would you like to enter a reminders for this date?", MsgBoxStyle.YesNo)
If m = MsgBoxResult.Yes Then
MonthCalendar1.Enabled = False
MonthCalendar1.Hide()
Notetxt.Enabled = True
Notetxt.Show()
Notetxt.Text = ""
SaveBtn.Enabled = True
SaveBtn.Show()
Backbtn.Enabled = True
Backbtn.Show()
Addbtn.Enabled = True
Addbtn.Show()
delbtn.Enabled = True
delbtn.Show()
lblMessage.Hide()
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Backbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Backbtn.Click
Notetxt.Enabled = False
Notetxt.Hide()
MonthCalendar1.Enabled = True
MonthCalendar1.Show()
SaveBtn.Hide()
Addbtn.Hide()
delbtn.Hide()
Backbtn.Hide()
lblMessage.Show()
End Sub
Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
t = MonthCalendar1.SelectionRange.Start.Month.ToString & MonthCalendar1.SelectionRange.Start.Day.ToString
Try
If Notetxt.Text = "" Then
If File.Exists(t & ".txt") = True Then
File.Delete(t & ".txt")
End If
End If
If Notetxt.Text.Length > 0 Then
File.WriteAllText(t & ".txt", Notetxt.Text)
MsgBox("SAVE SUCCESS", vbOKOnly)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Addbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Addbtn.Click
Notetxt.Text = "Hi QC Engineer, Golden Sample meet Due date already please change." & Environment.NewLine & "MODEL: " & Environment.NewLine & "START DATE: " & Environment.NewLine & "DUE DATE: " & Environment.NewLine & "APPROVED BY: EAKKACHAI"
End Sub
Private Sub delbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles delbtn.Click
Notetxt.Clear()
File.Delete(t & ".txt")
End Sub
End Class
and below is my code for sending email.
Public Class sendEmail
Private Sub sendEmail_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Content As String
Content = emailtxt.Text
emailtxt.Text = Reminder.Notetxt.Text
Dim Outlook As New Microsoft.Office.Interop.Outlook.Application
Dim MailItem As Microsoft.Office.Interop.Outlook.MailItem
MailItem = Outlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)
With MailItem
.HTMLBody = emailtxt.Text
.Subject = "GOLDEN SAMPLE DUE DATE EXPIRED"
'use the below to change from the default email account
.SentOnBehalfOfName = "your#email.com"
'you can add multiple recipients using .Add()
.Recipients.Add("your#email.com")
'examples of other optional arguments that can be included
' .Attachments.Add([file])
.Importance = Microsoft.Office.Interop.Outlook.OlImportance.olImportanceHigh
.Send() 'opens the email for checking prior to sending or use .Send()
End With
End Sub
End Class

call sub to change USERname color

I want to change USERname color inside RitchTextBox, I am using this code below to call the SUB but all the text now in red?
UPDATE
Sub AddMessage(txtUsername As String, txtSend As String)
box.SelectionColor = Color.Red
box.AppendText(vbCrLf & txtUsername & "$ ")
box.SelectionColor = Color.Black
box.AppendText(txtSend)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdSend.Click
' Shell("net send " & txtcomputer.Text & " " & txtmessage.Text)
Try
If txtPCIPadd.Text = "" Or txtUsername.Text = "" Or txtSend.Text = "" Then
MsgBox("wright a message!", "MsgBox")
Else
client = New TcpClient(txtPCIPadd.Text, 44444)
Dim writer As New StreamWriter(client.GetStream())
txttempmsg.Text = (txtSend.Text)
writer.Write(txtUsername.Text + " # " + txtSend.Text)
AddMessage(txtUsername.Text, txttempmsg.Text + vbCrLf)
'txtmsg.Text="You:" + txtmessage.Text)
writer.Flush()
txtSend.Text = ""
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
When you have a problem trying to get something to work in your program. It is easier to create a test example that you can then use to figure out what is happening without dealing with a lot of other variables in your larger program. In your case I created a VB Winforms app, added a RichTextBox, two TextBox's and a Button. By doing so I am able to show that the function is working.
Public Class Form1
Sub AddMessage(txtUsername As String, txtSend As String)
box.SelectionColor = Color.Red
box.AppendText(vbCrLf & txtUsername & " :") 'Note added colon
box.SelectionColor = Color.Black
box.AppendText(txtSend) 'Note changed variable name to parameter name
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
AddMessage(txtUsername.Text, txttempmsg.Text)
End Sub
End Class
Example:

How do you change the value in my.settings in a form when you enter numbers in a textbox in VB.Net

I was wondering if you could help me? My question is that, is there a way of changing the value in my.Settings in a form if you enter a number/decimal in a textbox and click a button and then update in the settings to be then changed in another from which is linked to my.Settings in a variable?!
Form 1:
Public Class frmConverter
Dim input As String
Dim result As Decimal
Dim EUR_Rate As Decimal = My.Settings.EUR_Rates
Dim USD_Rate As Decimal = 1.6
Dim JYP_Rate As Decimal = 179.65
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
input = txtInput.Text
Try
If ComboBox1.Text = "£" Then
Pounds()
ElseIf ComboBox1.Text = "€" Then
Euros()
ElseIf ComboBox1.Text = "$" Then
Dollars()
ElseIf ComboBox1.Text = "¥" Then
Yen()
End If
Catch es As Exception
MsgBox("Error!")
End Try
End Sub
Private Sub btnSettings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSettings.Click
Me.Hide()
frmExchange.Show()
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
txtInput.Text = ""
lblResult.Text = ""
End Sub
Function Pounds()
If ComboBox1.Text = "£" And ComboBox2.Text = "€" Then
result = (input * EUR_Rate)
lblResult.Text = FormatNumber(result, 2) & " " & ComboBox2.Text
ElseIf ComboBox1.Text = "£" And ComboBox2.Text = "$" Then
result = (input * USD_Rate)
lblResult.Text = FormatNumber(result, 2) & " " & ComboBox2.Text
ElseIf ComboBox1.Text = "£" And ComboBox2.Text = "¥" Then
result = (input * JYP_Rate)
lblResult.Text = FormatNumber(result, 2) & " " & ComboBox2.Text
End If
Return 0
End Function
Form 2:
Public Class frmExchange
Private Sub frmExchange_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
My.Settings.EUR_Rates = (txtinput.Text)
My.Settings.Save()
My.Settings.Reload()
End Sub
Public Sub SetNewRate(ByVal rate As Decimal)
txtinput.Text = rate.ToString
End Sub
Private Sub btnchange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnchange.Click
If ComboBox1.Text = "€" Then
My.Settings.USD_Rates = (txtinput.Text)
frmConverter.SetNewRate(txtinput.Text)
End If
End Sub
End class
It sounds like you are trying to use My.Settings as some sort of set of global reference variables. Thats not what they are for, not how they work and not what they are.
First, turn on Option Strict as it looks like it may be Off. This will store the decimal value of a textbox to a Settings variable which is defined as a Decimal:
My.Settings.USD_Rates = CDec(SomeTextBox.Text)
Thats all it will do. It wont save the value and it wont pass it around or share it with other forms and variables.
My.Settings.Save 'saves current settings to disk for next time
My.Settings.Reload 'Load settings from last time
This is all covered on MSDN. There is no linkage anywhere. If you have code in another form like this:
txtRate.Text = My.Settings.USD_Rates.ToString
txtRate will not automatically update when you post a new value to Settings. There are just values not Objects (see Value Types and Reference Types). To pass the new value to another form:
' in other form:
Public Sub SetNewRate(rate As Decimal)
' use the new value:
soemTextBox.Text = rate.ToString
End Sub
in form which gets the change:
Private Sub btnchangeRate(....
' save to settings which has nothing to do with passing the data
My.Settings.USD_Rates = CDec(RateTextBox.Text)
otherForm.SetNewRate(CDec(RateTextBox.Text))
End Sub
You may run into problems if you are using the default form instance, but that is a different problem.
You botched the instructions. The 2 procedures are supposed to go in 2 different forms - one to SEND the new value, one to RECEIVE the new value. With the edit and more complete picture, there is an easier way.
Private Sub btnSettings_Click(...) Handles btnSettings.Click
' rather than EMULATE a dialog, lets DO a dialog:
'Me.Hide()
'frmExchange.Show()
Using frm As New frmExchange ' the proper way to use a form
frm.ShowDialog
' Im guessing 'result' is the xchg rate var
result = CDec(frm.txtInput.Text)
End Using ' dispose of form, release resources
End Sub
In the other form
Private Sub btnchange_Click(....)
' do the normal stuff with Settings, if needed then:
Me.Hide
End Sub

Why does loop doesnt stop at .showdialog in formloadevent

Hello here is a very strange bug I cannot solve. I have a while loop in my formload that looks this way :
While loginOK = False And pstop = False
LoginForm1.ShowDialog()
If NewReg = True Then
While RegErfolgreich = False
Registrierung.ShowDialog()
Dim con As New SqlConnection(My.Settings.SLXADRIUMDEVConnectionString)
con.Open()
Dim cmd = New SqlCommand("Insert Into sysdba.PL_Userverwaltung (Benutzername, Passwort, [E-Mail-Adresse], Profil_OK) Values('" & RegBenutzername & "', '" & RegPassword & "', '" & RegEMailAdresse & "', 'f')", con)
If cmd.ExecuteNonQuery() = 1 Then
My.Settings.Benutzername = RegBenutzername
My.Settings.Passwort = RegPassword
My.Settings.Save()
Me.Close()
RegErfolgreich = True
Else
MsgBox("Es ist ein Fehler aufgetreten.")
End If
End While
Else
NewReg = False
End If
End While
The problem is that after closing loginform1 he jumps back in the loop, and when he starts looping again he opens loginform1 and closes it instantly. and loops again in the while loop.
That means I have a endless loop, which shows the loginform1 and closes it a millisecond later.
One thing to mention is that a friend of mine opened my project in an older vs studio version and this mysterious bug doesnt happen??
Hope for input cheers
EDIT:
Here is loginform1 code
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
If UsernameTextBox.Text <> "" And PasswordTextBox.Text <> "" Then
Startseite.LoginUsername = UsernameTextBox.Text
Startseite.LoginPassword = PasswordTextBox.Text
Startseite.loginOK = True
Me.Close()
Else
MsgBox("Bitte Benutzername und Passwort eingeben!")
End If
End Sub
Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
Startseite.pstop = True
Me.Close()
End Sub
Private Sub LoginForm1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If My.Settings.Benutzername <> "" And My.Settings.Passwort <> "" Then
'MsgBox(My.Settings.Benutzername & " " & My.Settings.Passwort)
UsernameTextBox.Text = My.Settings.Benutzername
PasswordTextBox.Text = My.Settings.Passwort
End If
End Sub
Private Sub lblNeuRegistrieren_Click(sender As Object, e As EventArgs) Handles lblNeuRegistrieren.Click
Startseite.NewReg = True
Me.Close()
End Sub
About your error, probably you didn't initialize your startseite variable to be your main form, loginOK And pstop never get changed and that's why the loop never stops. But on the rest of your code, some general tips.
This is unnecesary
If NewReg = True Then
''
Else
NewReg = False
End If
If NewReg is not true, then it's false by definition. Should be
If NewReg Then
''
End If
Also, overloading the showDialog on loginForm1 is a better solution than adding a yourmainclass object on the second form. It is easier to debug and provides better reusability.
try exiting loop on message box
If cmd.ExecuteNonQuery() = 1 Then
My.Settings.Benutzername = RegBenutzername
My.Settings.Passwort = RegPassword
My.Settings.Save()
Me.Close()
RegErfolgreich = True
Else
MsgBox("Es ist ein Fehler aufgetreten.")
Exit While
End If