Lanc1988 Posted August 6, 2004 Posted August 6, 2004 Currently im using the code for an input box to get information from the user and then once they enter something and click ok then it does some things with what they entered, but if they click cancel im having a problem because it will continue to carry out the rest of the events anyway. Here is an example code im using: MyFavoritesURL = InputBox("Enter the URL of the webpage you would like to add to your favorites.") So everything works as long as they click Ok but if they click Cancel it will still do the same thing as if the user clicked Ok. Anyone know how to get around this? Like somehow using If Then and Else? Quote
*Experts* mutant Posted August 6, 2004 *Experts* Posted August 6, 2004 My answer is... don't use an InputBox, it is obsolete. Instead create your own form, which will act as a dialog and assign dialog return values to your buttons. Quote
Lanc1988 Posted August 6, 2004 Author Posted August 6, 2004 Is that the only way to do it? I haven't yet figured out how to transfer variables from one form to another.. :S Quote
*Experts* mutant Posted August 6, 2004 *Experts* Posted August 6, 2004 (edited) Creating your own form similar to an InputBox gives you a lot more freedom thatn using the ugly looking InputBox. Getting the entered info back to your original form is very easy! Simply create your dialog form, choose buttons that will represent cancel and OK and set their DialogResult property to the wanted one, create a variable that will store the value and a property that will expose the variable, then after the form is closed you can access the property with the wanted value (since dialogs are not diposed automatically). When showing your form show it using the ShowDialog method. That method will return a dialog result, if the user clicked your OK button OK will be returned etc. Don't forget to set the value of the variable, probably during the closing of the form. Now here is some sample code :): Public Class YourOwnInputBox Inherits Form 'store the value Private yourvalue As Object 'Provide access to the value Public ReadOnly Property TheValue Get Return yourvalue End Get End Property End Class 'now to show the form as see what it returns Dim dlg As New YourOwnInputBox() If dlg.ShowDialog() = DialogResult.OK Then 'process the data by using the defined property DoSOmething(dlg.TheValue) End If dlg.Dispose() Edited August 6, 2004 by mutant Quote
Lanc1988 Posted August 6, 2004 Author Posted August 6, 2004 Seems a little complicated.. but I will try it before I just give up on it Quote
Joe Mamma Posted August 6, 2004 Posted August 6, 2004 (edited) Seems a little complicated.. but I will try it before I just give up on itadd this class to your app (code follows, post back if I messed something up in the VB translation from C#). usage: ______________________________________________ Public Shared Function ObjTec.InputForm.PromptString(ByVal args As ObjTec.PromptFormArgs) As String parameters: args - instance of ObjTec.PromptFormArgs (see below) return: on 'OK' - the User Specified Text, on 'Cancel' - nothing dim s as string s = ObjTec.InputForm.PromptString() ______________________________________________ Public Shared Function ObjTec.InputForm.PromptQuery(ByVal args As ObjTec.PromptFormArgs, ByRef result As String) As Boolean parameters: args - instance of ObjTec.PromptFormArgs (see below) result [out] - null on user cancel else the input value return: on 'OK'- true on 'Cancel' - false ______________________________________________ class ObjTec.PromptFormArgs constructr ObjTec.PromptFormArgs(ByVal title As String, ByVal prompt As String, ByVal defaultValue As String) property shared readonly ObjTec.PromptFormArgs Empty [null actually] defaults the form to: Title = Application.ProductName prompt = "Value Required" Default = "[not set'" ______________________________________________ example: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim s1 As String = ObjTec.InputForm.PromptString(New ObjTec.PromptFormArgs("A Value Needed", "Enter any Value", "Default")) Dim s2 As String If ObjTec.InputForm.PromptQuery(ObjTec.PromptFormArgs.Empty, s2) Then MessageBox.Show(s2) Else If Not (s1 Is Nothing) Then MessageBox.Show(s1) Else MessageBox.Show("User Cancelled") End If End If End Sub The Class Code - Imports System Imports System.Drawing Imports System.Collections Imports System.ComponentModel Imports System.Windows.Forms Namespace ObjTec Public Class InputForm Inherits System.Windows.Forms.Form Private WithEvents okButton As System.Windows.Forms.Button Private WithEvents cancelBtn As System.Windows.Forms.Button Friend WithEvents label1 As System.Windows.Forms.Label Friend WithEvents textBox1 As System.Windows.Forms.TextBox Private components As System.ComponentModel.Container = Nothing Private Sub New() InitializeComponent() End Sub 'New Protected Overloads Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Dispose Private Sub InitializeComponent() Me.okButton = New System.Windows.Forms.Button Me.cancelBtn = New System.Windows.Forms.Button Me.label1 = New System.Windows.Forms.Label Me.textBox1 = New System.Windows.Forms.TextBox Me.SuspendLayout() ' 'okButton ' Me.okButton.DialogResult = System.Windows.Forms.DialogResult.OK Me.okButton.Enabled = False Me.okButton.Location = New System.Drawing.Point(96, 56) Me.okButton.Name = "okButton" Me.okButton.TabIndex = 0 Me.okButton.Text = "&OK" ' 'cancelBtn ' Me.cancelBtn.DialogResult = System.Windows.Forms.DialogResult.Cancel Me.cancelBtn.Location = New System.Drawing.Point(200, 56) Me.cancelBtn.Name = "cancelBtn" Me.cancelBtn.TabIndex = 1 Me.cancelBtn.Text = "&Cancel" ' 'label1 ' Me.label1.Location = New System.Drawing.Point(32, 8) Me.label1.Name = "label1" Me.label1.Size = New System.Drawing.Size(288, 16) Me.label1.TabIndex = 2 Me.label1.Text = "label1" Me.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft ' 'textBox1 ' Me.textBox1.Location = New System.Drawing.Point(32, 32) Me.textBox1.Name = "textBox1" Me.textBox1.Size = New System.Drawing.Size(296, 20) Me.textBox1.TabIndex = 3 Me.textBox1.Text = "textBox1" ' 'InputForm ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(352, 93) Me.Controls.Add(Me.textBox1) Me.Controls.Add(Me.label1) Me.Controls.Add(Me.cancelBtn) Me.Controls.Add(Me.okButton) Me.Name = "InputForm" Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen Me.Text = "InputForm" Me.ResumeLayout(False) End Sub 'InitializeComponent Private Sub SetFromArgs(ByVal args As PromptFormArgs) Me.Text = args._title Me.label1.Text = args._prompt Me.textBox1.Text = args._default End Sub Public Shared Function PromptQuery(ByVal args As PromptFormArgs, ByRef result As String) As Boolean Dim frm As New InputForm Try result = Nothing If args Is Nothing Then frm.SetFromArgs(New PromptFormArgs) Else frm.SetFromArgs(DirectCast(args, PromptFormArgs)) End If If frm.ShowDialog() = System.Windows.Forms.DialogResult.OK Then result = frm.textBox1.Text Return True Else Return False End If Finally frm.Dispose() End Try End Function 'PromptQuery Public Shared Function PromptString(ByVal args As PromptFormArgs) As String Dim frm As New InputForm Try If args Is Nothing Then frm.SetFromArgs(New PromptFormArgs) Else frm.SetFromArgs(DirectCast(args, PromptFormArgs)) End If If frm.ShowDialog() = System.Windows.Forms.DialogResult.OK Then Return frm.textBox1.Text Else Return Nothing End If Finally frm.Dispose() End Try End Function 'PromptString Private Sub textBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textBox1.TextChanged okButton.Enabled = textBox1.Text <> "" End Sub End Class 'InputForm Public Class PromptFormArgs Friend _title As String = System.Windows.Forms.Application.ProductName Friend _prompt As String = "Value Required" Friend _default As String = "[Not Set]" Friend Sub New() End Sub Public Sub New(ByVal title As String, ByVal prompt As String, ByVal defaultValue As String) _title = title _prompt = prompt _default = defaultValue End Sub 'New Public Shared ReadOnly Property Empty() As PromptFormArgs Get Return Nothing End Get End Property End Class 'PromptFormArgs End Namespace Edited August 6, 2004 by Joe Mamma Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.