Click Button Event in VB that displays a message about user input - vb.net

I have a really quick and simple question, I’m learning programming in C# and VB and I need to create a GUI application in Windows Form with Visual Studio. This GUI will prompt the user to enter in an integer. I believe I have this part alright but then I need to have the user click a button that will convert the user's entry to an integer and display a message indicating whether the user was successful. I think I even have the conversion done correctly but I am having a problem displaying that message if the user was successful. Basically I need to know how to function the click method in VB that will allow this message to appear. Any help with this would be greatly appreciated. The following code is what I have already written for this project:
Public Class Form1
Private Sub EvaluateInput()
Dim InputValue As String
InputValue = ValueTextBox.Text
If IsNumeric(InputValue) Then
MessageBox.Show(InputValue & " is a number.")
Else
MessageBox.Show(InputValue & " is not a number.")
End If
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click 'Continue Button
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Exit Button
Dim button As DialogResult
button = MessageBox.Show _
("Are you sure you want to exit this application?", _
"Message", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
If button = Windows.Forms.DialogResult.Yes Then
Me.Close()
Else
'Do Nothing
End If
End Sub
End Class

If I understand your question correctly, then you would need a simply change:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) _
Handles Button2.Click 'Continue Button
EvaluateInput()
End Sub
When you press Button2, it will call the EvaluateInput sub and display a message accordingly.

Related

How to: Force users to focus a winform before selecting something from it

I am working on a project that uses WinForms, and I am running into concurrency problems when users click controls on a Form while it isn't the active window. My forms refresh whenever a user activates them to prevent them from showing old/dirty info, but if they click a button on an inactive Form the data doesn't get refreshed, which can cause a problem.
I know some programs in windows force you to select the window before you can preform any other actions, is there a way to do this in my VB.NET program?
This code run in my test, but some issues that must be handled to run as our need:
Dim mystts As Boolean = False
Private Sub Form2_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
'here your process, unfortunately this will be performed too after messagebox close back to form2
'for example the process are:
For myCnt As Integer = 1 To 4000
Debug.Print("Test") 'This only for waiting
Next
mystts = False
End Sub
Private Sub Form2_Deactivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Deactivate
'we must define the other criteria for make mystts=true, for example if
'we call messageBox.show, we can pass out from this for example we have msgFlag
'before call messageBox set msgFlag=true and after it set msgFlag=false
'and here you add: if msgFlag=true then exit sub
mystts = True
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'if we click this button and myStts=true, the next process will wait until process in form activate finish
MsgBox("this My Process, run after form2 activated and process finish")
End Sub

Expression is not a method

Hello Coders Im trying to code a Anti virus and the following problem was here!
Im trying to add MsgBoxStyle.YesNo and will the problem in this...
So im trying to add a Question for delete file or no with MsgBox.
My code:
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Label1_Click(sender As System.Object, e As System.EventArgs)
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
If My.Settings.ifTmpR = True Then
For Each aF In Directory.GetFiles(My.Computer.FileSystem.SpecialDirectories.Temp)
Try
MsgBoxStyle.YesNo()
MsgBox("Biztosan törli: " + aF)
IO.File.Delete(aF)
MsgBox("Temp kiürítése: " + aF)
MsgBox(aF + "kitötrölve")
Catch ex As Exception
End Try
Next
End If
If My.Settings.ifTmpR = False Then
Try
MsgBox("Kérlek kapcsold be a TEMP Queryt!")
Catch ex As Exception
End Try
End If
End Sub
Private Sub RadioButton1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton1.CheckedChanged
If RadioButton1.Checked = True Then
My.Settings.ifTmpR = True
End If
End Sub
Private Sub RadioButton2_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton2.CheckedChanged
If RadioButton2.Checked = False Then
My.Settings.ifTmpR = False
End If
End Sub
End Class
And what the problem?
Help?
So you're coding an Anti-Virus application but you seem to have no basic knowledge about how to use methods and parameters.
You are supposed to include MsgBoxStyle.YesNo as one of the parameters to the MsgBox() method. You cannot call it like you do since it's simply an Integer value.
As Steve suggested you should use MessageBox.Show() instead, due to that MsgBox only exists for backwards compability.
The first parameter is the message you want it to show, and the second parameter is what title the window should have. They're just plain strings.
The third parameter however is the one you pass to tell the method what buttons to include in the MessageBox, and it should be passed like this:
MessageBox.Show("I am a message", "I am a title", MessageBoxButtons.YesNo)
This will show a message box with the buttons "Yes" and "No" inside it.
Now to get use out of this you should put your MessageBox in an If-statement, otherwise the application won't care if you press either Yes or No.
If MessageBox.Show("Are you sure you want to delete this file?", "Confirmation", MessageBoxButtons.YesNo) = DialogResult.Yes Then
'Delete code here.
End If
There. Now I suggest that you would have started with something like a "Hello World"-application instead of an Anti-Virus application, as Anti-Virus is pretty much harder than just showing a simple MessageBox.

ToolStripTextBox action on pressing Enter key

I've got a similar issue - i've created a Windows Form Application which basically looks up data from a SQL server based on a code entered into the ToolStripTextBox. This works great when the button is clicked however I'd like to add the function of hitting enter after typing and that brings the results through.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'DataSet.Table' table. You can move, or remove it, as needed.
Me.TableTableAdapter.Fill(Me.DataSet.Table)
End Sub
Private Sub FillBySearchToolStripButton_Click(sender As Object, e As EventArgs) Handles FillBySearchToolStripButton.Click
Try
Me.TableTableAdapter.FillBySearch(Me.DataSet.Table, TYPEToolStripTextBox.Text)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
End Class
Appologies as I am a newbie.
Just use the KeyDown event:
Private Sub ToolStripTextBox1_KeyDown(sender As Object, e As KeyEventArgs) _
Handles ToolStripTextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
'run your code
End If
End Sub
The "run your code" block could just call the click event:
FillBySearchToolStripButton.PerformClick()

Drop a file from shell

I am trying to drop a file from windows explorer to my form and I am almost successful :)
After opening explorer window in shell I can drop a file to my form where I have one message box/dialog before accepting.
Problem is in fact that my messagebox with question opens in back of explorer window.
Here is a code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.AllowDrop = True
End Sub
Private Sub Form1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
Dim Files() As String
Files = e.Data.GetData(DataFormats.FileDrop)
If Files.Length > 0 Then
Dim ret As Integer = MsgBox("Would you like to upload file?" & vbNewLine & Files(0), MsgBoxStyle.OkCancel + MsgBoxStyle.Question, "Decide please")
If ret = DialogResult.OK Then
myModule.UploadF()
End If
End If
End If
End Sub
Private Sub Form1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.All
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub btn_open_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_open.Click
Shell("explorer c:\", AppWinStyle.NormalFocus)
End Sub
End Class
1) Is here any way to get messagebox to pop in front of explorer window?
2) How to close opened explorer window from my program after usage?
EDIT: Solution for...
1) is to place Me.Activate before MsgBox!
2) for that I still don't find a solution.
I know you have already accepted an answer, but i agree with Hans that this implementation is flawed. If the user must click an upload button, then launch an openfiledialog. Opening a plain explorer window that they must drag from is incredibly counter intuitive.
You can keep the drag and drop functionality, but let the user open their own explorer window to use that.
A more intuitive implementation would be to add the dragdrop functionality to a panel, and have a label and a button so the user can choose either method.
Something like this:
Try this:
Add a Dialog to your project. (Found in the list of addable things like form, class etc).
Add a label to it and change the text in the constructor.
In the Dialogs paint event add this:
Me.BringToFront()
Then use this dialog instead of MsgBox, it should provide what you need.
It's somewhat of a workaround but it should work.
Edit:
Right, found a better solution.
Add this to the code written in this question before calling MsgBox:
Me.BringToFront()
Me.TopMost = True
And you will be fine.

Yes/No function in a messagebox

I want to add a yes/no function to a message box (Are you sure you want to exit?) in InfoPath 2007. If the user clicks 'Yes' the InfoPath form closes, if no, then the user is taken back to the form. From what I have read this will not happen in InfoPath. So, I added a new windows form that has the Yes/No buttons.
For the 'No' button, I have (me.close) which closes the windows form and the user is left with the InfoPath form. I need help when the user clicks 'Yes' meaning they want to close the windows form AND the InfoPath form. Below is my code so far. Many thanks in advance.
Imports Microsoft.Office.InfoPath
Imports System
Imports System.Xml
Imports System.Xml.XPath
Imports System.Diagnostics
Public Class Confirm_Close
Private Sub btnNo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNo.Click
Me.Close()
End Sub
Private Sub btnYes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnYes.Click
Try
<need help here>
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
End Sub
End Class
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
If MessageBox.Show("Do you want to exit?", "Title", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
Me.Close()
End If
End Sub
THe following is how i sovled this:
If MsgBox("Prompt", MsgBoxStyle.YesNoCancel, "Title") = MsgBoxResult.Yes Then
' execute command
End If
If MessageBox.Show("Do you want to Exit?", "EXIT MESSAGE", MessageBoxButtons.YesNo) = DialogResult.Yes Then
Me.Close()
End If
Dim result = MessageBox.Show("Are you sure you want to close?", "are you sure?", MessageBoxButtons.YesNoCancel)
If result = DialogResult.Yes Then
Me.Close()
End If
End Sub