Err Posted January 12, 2007 Share Posted January 12, 2007 How can I catch empty values on 3 text boxes that need values.I have three text boxes: txtGrade1, txtGrade2 and txtGrade3.and I have a label for any message.This is for a simple calculator that I'm working on, if you need more code on this, then please say so. If txtGrade1.Text = "" Or txtGrade2.Text = "" Or txtGrade3.Text = "" Then lblMsg.Text = "Numbers must be inputed to all 3 boxes."End If Link to comment Share on other sites More sharing options...
aspnetguy Posted January 12, 2007 Share Posted January 12, 2007 I have read that checking to to see if a string = "" is not a good practice (not sure why). There are 3 other methods you can try. If txtGrade1.Text = String.Empty Then If txtGrade1.Text.Length < 1 Then If String.Compare(txtGrade1.Text,String.Empty) = 0 Then Does the code you posted not work. It seems to me that it should. You can also try one of the methods I posted. Link to comment Share on other sites More sharing options...
jesh Posted January 12, 2007 Share Posted January 12, 2007 I recently found another method: if(String.IsNullOrEmpty(txtGrade1.Text)){} EDIT: Of course, my sample is in C#... Link to comment Share on other sites More sharing options...
aspnetguy Posted January 12, 2007 Share Posted January 12, 2007 I recently found another method:if(String.IsNullOrEmpty(txtGrade1.Text)){} EDIT: Of course, my sample is in C#...which is the best language in the world! IMO Link to comment Share on other sites More sharing options...
Err Posted January 17, 2007 Author Share Posted January 17, 2007 Cool. I'll be sure to try that, I'm working on this at school since I still don't have the program for it. No it doesn't work with the above code I posted. I also tried putting all 3 boxes in separate If statements, but that didn't work either. Link to comment Share on other sites More sharing options...
aspnetguy Posted January 17, 2007 Share Posted January 17, 2007 you can download Visual Web Developer Express for free. It is a stripped down "for web" version of Visual Studio 2005. Link to comment Share on other sites More sharing options...
pulpfiction Posted January 17, 2007 Share Posted January 17, 2007 you can also use validator controls to check empty textbox, "requiredfieldvalidator"http://www.w3schools.com/aspnet/control_re...ldvalidator.asp Link to comment Share on other sites More sharing options...
Err Posted January 19, 2007 Author Share Posted January 19, 2007 It's still not working, please look at my code and tell me where I went wrong. Public Class Form1Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click Me.Close() End Sub Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click txtGrade1.Clear() Me.txtGrade2.Clear() Me.txtGrade3.Clear() lblLetter.ResetText() 'lblAvg.Text = "" lblAvg.ForeColor = Color.Empty lblAvg.BackColor = Color.Empty lblAvg.ResetText() lblMsg.ResetText() txtName.ResetText() txtAbs.ResetText() lblAvg.BackColor = Color.White txtGrade1.Focus() End Sub Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click Dim shoGrade1 As Short = 0 Dim shoGrade2 As Short Dim shoGrade3 As Short Dim sngAvg As Single 'Input shoGrade1 = Val(txtGrade1.Text) shoGrade2 = Val(txtGrade2.Text) shoGrade3 = Val(txtGrade3.Text) 'Processing sngAvg = (shoGrade1 + shoGrade2 + shoGrade3) / 3 'If Not IsNumeric(txtGrade1.Text) Then 'lblMsg.Text = "Please enter numbers only." 'End If If String.Compare(txtGrade1.Text, String.Empty) = 0 Then lblMsg.Text = "Numbers must be inputed for box 1" End If 'If txtGrade1.Text = "" Or txtGrade2.Text = "" Or txtGrade3.Text = "" Then 'lblMsg.Text = "Numbers must be inputed to all 3 boxes." 'End If If sngAvg < 0 Then lblLetter.Text = "X" lblMsg.Text = "Please enter positive numbers only" ElseIf sngAvg < 60 Then lblLetter.Text = "F" lblMsg.Text = "You are teh fail." lblAvg.BackColor = Color.Red ElseIf sngAvg < 70 Then lblLetter.Text = "D" lblMsg.Text = "Almost, but you're still a failure." lblAvg.BackColor = Color.Yellow lblAvg.ForeColor = Color.Black ElseIf sngAvg < 80 Then lblLetter.Text = "C" lblMsg.Text = "Passing." lblAvg.BackColor = Color.Yellow ElseIf sngAvg < 90 Then lblLetter.Text = "B" lblMsg.Text = "Good." lblAvg.BackColor = Color.LightGreen lblAvg.ForeColor = Color.Black ElseIf sngAvg <= 100 Then lblLetter.Text = "A" lblMsg.Text = "Winnar." lblAvg.BackColor = Color.Green lblAvg.ForeColor = Color.FloralWhite ElseIf sngAvg > 100 Then lblMsg.Text = "Impossable." Else lblLetter.Text = "Invalid" lblMsg.Text = "Invalid Inputs" End If 'Output lblAvg.Text = FormatNumber(sngAvg, 1) 'lblAvg.ForeColor = Color.FloralWhite 'lblAvg.BackColor = Color.Firebrick End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'lblDate.Text = FormatDateTime(Today, DateFormat.LongDate) 'lblTime.Text = FormatDateTime(Now, DateFormat.ShortTime) lblDate.Text = Now.ToShortDateString lblTime.Text = TimeOfDay.ToLongTimeString 'changes color of lblAvg, when the program starts, to white Me.lblAvg.BackColor = Color.White End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick lblTime.Text = TimeOfDay.ToLongTimeString End SubEnd Class And the Form code: #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides 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 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents Label3 As System.Windows.Forms.Label Friend WithEvents Label4 As System.Windows.Forms.Label Friend WithEvents Label5 As System.Windows.Forms.Label Friend WithEvents txtGrade1 As System.Windows.Forms.TextBox Friend WithEvents txtGrade2 As System.Windows.Forms.TextBox Friend WithEvents txtGrade3 As System.Windows.Forms.TextBox Friend WithEvents lblAvg As System.Windows.Forms.Label Friend WithEvents btnExit As System.Windows.Forms.Button Friend WithEvents btnClear As System.Windows.Forms.Button Friend WithEvents btnCalculate As System.Windows.Forms.Button Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox Friend WithEvents lblDate As System.Windows.Forms.Label Friend WithEvents lblTime As System.Windows.Forms.Label Friend WithEvents Timer1 As System.Windows.Forms.Timer Friend WithEvents lblMsg As System.Windows.Forms.Label Friend WithEvents lblLetter As System.Windows.Forms.Label Friend WithEvents Label7 As System.Windows.Forms.Label Friend WithEvents txtAbs As System.Windows.Forms.TextBox Friend WithEvents Label6 As System.Windows.Forms.Label Friend WithEvents txtName As System.Windows.Forms.TextBox Friend WithEvents Label8 As System.Windows.Forms.Label <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.components = New System.ComponentModel.Container Me.Label1 = New System.Windows.Forms.Label Me.Label2 = New System.Windows.Forms.Label Me.Label3 = New System.Windows.Forms.Label Me.Label4 = New System.Windows.Forms.Label Me.Label5 = New System.Windows.Forms.Label Me.txtGrade1 = New System.Windows.Forms.TextBox Me.txtGrade2 = New System.Windows.Forms.TextBox Me.txtGrade3 = New System.Windows.Forms.TextBox Me.lblAvg = New System.Windows.Forms.Label Me.btnExit = New System.Windows.Forms.Button Me.btnClear = New System.Windows.Forms.Button Me.btnCalculate = New System.Windows.Forms.Button Me.GroupBox1 = New System.Windows.Forms.GroupBox Me.lblTime = New System.Windows.Forms.Label Me.lblDate = New System.Windows.Forms.Label Me.Timer1 = New System.Windows.Forms.Timer(Me.components) Me.lblMsg = New System.Windows.Forms.Label Me.lblLetter = New System.Windows.Forms.Label Me.Label7 = New System.Windows.Forms.Label Me.txtAbs = New System.Windows.Forms.TextBox Me.Label6 = New System.Windows.Forms.Label Me.txtName = New System.Windows.Forms.TextBox Me.Label8 = New System.Windows.Forms.Label Me.GroupBox1.SuspendLayout() Me.SuspendLayout() ' 'Label1 ' Me.Label1.AutoSize = True Me.Label1.Font = New System.Drawing.Font("Comic Sans MS", 14.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label1.Location = New System.Drawing.Point(56, 16) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(225, 30) Me.Label1.TabIndex = 0 Me.Label1.Text = "Letter Grade Calculator" Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ' 'Label2 ' Me.Label2.Location = New System.Drawing.Point(16, 144) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(72, 32) Me.Label2.TabIndex = 4 Me.Label2.Text = "Grade &1:" Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight ' 'Label3 ' Me.Label3.Location = New System.Drawing.Point(16, 280) Me.Label3.Name = "Label3" Me.Label3.Size = New System.Drawing.Size(72, 32) Me.Label3.TabIndex = 10 Me.Label3.Text = "Average:" Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight ' 'Label4 ' Me.Label4.Location = New System.Drawing.Point(16, 232) Me.Label4.Name = "Label4" Me.Label4.Size = New System.Drawing.Size(72, 32) Me.Label4.TabIndex = 8 Me.Label4.Text = "Grade &3:" Me.Label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight ' 'Label5 ' Me.Label5.Location = New System.Drawing.Point(16, 192) Me.Label5.Name = "Label5" Me.Label5.Size = New System.Drawing.Size(72, 32) Me.Label5.TabIndex = 6 Me.Label5.Text = "Grade &2:" Me.Label5.TextAlign = System.Drawing.ContentAlignment.MiddleRight ' 'txtGrade1 ' Me.txtGrade1.Location = New System.Drawing.Point(112, 144) Me.txtGrade1.Name = "txtGrade1" Me.txtGrade1.Size = New System.Drawing.Size(72, 28) Me.txtGrade1.TabIndex = 5 Me.txtGrade1.Text = "" ' 'txtGrade2 ' Me.txtGrade2.Location = New System.Drawing.Point(112, 192) Me.txtGrade2.Name = "txtGrade2" Me.txtGrade2.Size = New System.Drawing.Size(72, 28) Me.txtGrade2.TabIndex = 7 Me.txtGrade2.Text = "" ' 'txtGrade3 ' Me.txtGrade3.Location = New System.Drawing.Point(112, 240) Me.txtGrade3.Name = "txtGrade3" Me.txtGrade3.Size = New System.Drawing.Size(72, 28) Me.txtGrade3.TabIndex = 9 Me.txtGrade3.Text = "" ' 'lblAvg ' Me.lblAvg.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblAvg.Location = New System.Drawing.Point(112, 280) Me.lblAvg.Name = "lblAvg" Me.lblAvg.Size = New System.Drawing.Size(72, 28) Me.lblAvg.TabIndex = 0 Me.lblAvg.TextAlign = System.Drawing.ContentAlignment.MiddleRight ' 'btnExit ' Me.btnExit.Location = New System.Drawing.Point(272, 384) Me.btnExit.Name = "btnExit" Me.btnExit.Size = New System.Drawing.Size(88, 32) Me.btnExit.TabIndex = 13 Me.btnExit.Text = "E&xit" ' 'btnClear ' Me.btnClear.Location = New System.Drawing.Point(152, 384) Me.btnClear.Name = "btnClear" Me.btnClear.Size = New System.Drawing.Size(88, 32) Me.btnClear.TabIndex = 12 Me.btnClear.Text = "Clea&r" ' 'btnCalculate ' Me.btnCalculate.Location = New System.Drawing.Point(40, 384) Me.btnCalculate.Name = "btnCalculate" Me.btnCalculate.Size = New System.Drawing.Size(88, 32) Me.btnCalculate.TabIndex = 11 Me.btnCalculate.Text = "&Calculate" ' 'GroupBox1 ' Me.GroupBox1.Controls.Add(Me.lblTime) Me.GroupBox1.Controls.Add(Me.lblDate) Me.GroupBox1.Location = New System.Drawing.Point(216, 104) Me.GroupBox1.Name = "GroupBox1" Me.GroupBox1.Size = New System.Drawing.Size(208, 136) Me.GroupBox1.TabIndex = 9 Me.GroupBox1.TabStop = False Me.GroupBox1.Text = "Date and Time" ' 'lblTime ' Me.lblTime.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblTime.Location = New System.Drawing.Point(16, 80) Me.lblTime.Name = "lblTime" Me.lblTime.Size = New System.Drawing.Size(176, 40) Me.lblTime.TabIndex = 1 Me.lblTime.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ' 'lblDate ' Me.lblDate.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblDate.Location = New System.Drawing.Point(16, 32) Me.lblDate.Name = "lblDate" Me.lblDate.Size = New System.Drawing.Size(176, 40) Me.lblDate.TabIndex = 0 Me.lblDate.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ' 'Timer1 ' Me.Timer1.Enabled = True Me.Timer1.Interval = 1000 ' 'lblMsg ' Me.lblMsg.Location = New System.Drawing.Point(216, 248) Me.lblMsg.Name = "lblMsg" Me.lblMsg.Size = New System.Drawing.Size(208, 80) Me.lblMsg.TabIndex = 10 Me.lblMsg.TextAlign = System.Drawing.ContentAlignment.MiddleCenter ' 'lblLetter ' Me.lblLetter.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblLetter.Location = New System.Drawing.Point(112, 328) Me.lblLetter.Name = "lblLetter" Me.lblLetter.Size = New System.Drawing.Size(72, 28) Me.lblLetter.TabIndex = 12 Me.lblLetter.TextAlign = System.Drawing.ContentAlignment.MiddleRight ' 'Label7 ' Me.Label7.Location = New System.Drawing.Point(16, 328) Me.Label7.Name = "Label7" Me.Label7.Size = New System.Drawing.Size(72, 32) Me.Label7.TabIndex = 11 Me.Label7.Text = "Grade:" Me.Label7.TextAlign = System.Drawing.ContentAlignment.MiddleRight ' 'txtAbs ' Me.txtAbs.Location = New System.Drawing.Point(112, 96) Me.txtAbs.Name = "txtAbs" Me.txtAbs.Size = New System.Drawing.Size(72, 28) Me.txtAbs.TabIndex = 3 Me.txtAbs.Text = "" ' 'Label6 ' Me.Label6.Location = New System.Drawing.Point(8, 96) Me.Label6.Name = "Label6" Me.Label6.Size = New System.Drawing.Size(80, 32) Me.Label6.TabIndex = 2 Me.Label6.Text = "&Absences:" Me.Label6.TextAlign = System.Drawing.ContentAlignment.MiddleRight ' 'txtName ' Me.txtName.Location = New System.Drawing.Point(112, 48) Me.txtName.Name = "txtName" Me.txtName.Size = New System.Drawing.Size(208, 28) Me.txtName.TabIndex = 1 Me.txtName.Text = "" ' 'Label8 ' Me.Label8.Location = New System.Drawing.Point(16, 48) Me.Label8.Name = "Label8" Me.Label8.Size = New System.Drawing.Size(72, 32) Me.Label8.TabIndex = 0 Me.Label8.Text = "Name:" Me.Label8.TextAlign = System.Drawing.ContentAlignment.MiddleRight ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(8, 21) Me.ClientSize = New System.Drawing.Size(440, 430) Me.Controls.Add(Me.txtName) Me.Controls.Add(Me.Label8) Me.Controls.Add(Me.txtAbs) Me.Controls.Add(Me.Label6) Me.Controls.Add(Me.lblLetter) Me.Controls.Add(Me.Label7) Me.Controls.Add(Me.lblMsg) Me.Controls.Add(Me.GroupBox1) Me.Controls.Add(Me.btnCalculate) Me.Controls.Add(Me.btnClear) Me.Controls.Add(Me.btnExit) Me.Controls.Add(Me.lblAvg) Me.Controls.Add(Me.txtGrade3) Me.Controls.Add(Me.txtGrade2) Me.Controls.Add(Me.txtGrade1) Me.Controls.Add(Me.Label5) Me.Controls.Add(Me.Label4) Me.Controls.Add(Me.Label3) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.Label1) Me.Font = New System.Drawing.Font("Comic Sans MS", 11.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Name = "Form1" Me.Text = "Letter Grade Calculator" Me.GroupBox1.ResumeLayout(False) Me.ResumeLayout(False) End Sub#End Region Edit: I got it, I needed to put "Exit Sub" before my "End If" statements. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now