Hello,
my goal is to write a Validate class, and call it over to Form1.cs.
I've seen ways to validate user input through Form1.cs, but never on the normal class.
Will it be possible to validate textBox or comboBox input?
Thank you.
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Dec 4, 2014, 1:30 AM EST
Selva GanapathyRank #560 · 4.3K reputationPosted Dec 2, 2014, 7:15 PM EST
Hi Song,
Did you try to pass the String return form textbox of ComboBox my textbox.Text or coboBox.Text?
Or why cant you use TextValidation event of the TextBox control to make use of your own validation class here?
Regards,
Selva Ganapathy K
VulpesRank #55 · 29.2K reputationPosted Dec 3, 2014, 1:11 AM EST
This may give you some pointers as to how you could do it:
/* Validation.cs */
/* Form1.cs */
Song Lee49 reputationPosted Dec 3, 2014, 4:22 PM EST
////////////////////////////////////////////////////////////////////////////////////////////////////
Form cs.
private bool ValidateContact(ContactFiles.Contact contact)
{
if (!InputUtility.ValidateString(contact.FirstName))
{
MessageBox.Show("You must enter a first name with atleast one character (not a blank)", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
firstText.Focus();
firstText.Text = " ";
firstText.SelectAll();
return false;
}
else if (!InputUtility.ValidateString(contact.LastName))
{
MessageBox.Show("You must enter a last name with atleast one character (not a blank)", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
secondText.Focus();
secondText.Text = " ";
secondText.SelectAll();
return false;
}
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
non-form class:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; //Added
namespace Assignment_6_attempt_1
{
static class InputUtility
{
public static bool ValidateString(Control c, out int i)
{
return int.TryParse(c.Text, out i);
}
}
//I'm not too sure what to put in this method D:
//Getting: error: No overload for method "validateString" takes 1 arguments
Song Lee49 reputationPosted Dec 5, 2014, 12:23 PM EST