I have 3 pairs of textboxes in a windows form :-
TxtboxSourceFolder1 -> TxtboxDestinationFolder1
TxtboxSourceFolder2 -> TxtboxDestinationFolder2
TxtboxSourceFolder3 -> TxtboxDestinationFolder3
I am creating a List<string> SrcFolders and a List<string> DestFolders.
Now I have to validate the user input:-
1) Is there a value for TxtboxSourceFolder1, If yes, Is there a corresponding value in TxtboxDestinationFolder1 ? If all answers are yes, and the values are legitimate then add them to corresponding lists. And then repeat.
To me it seems like to check if a textbox is empty:-
private int ConstructSourceDestinationFolderList()
{
if (Directory.Exists(txtboxSrcFolder1.Text) && Directory.Exists(txtboxDestFolder1.Text))
{
trans.szSourceFolderList.Add(txtboxSrcFolder1.Text.ToString());
trans.szDestinationFolderList.Add(txtboxDestFolder1.Text);
}
else
{
return 1;
}
if (!String.IsNullOrWhiteSpace(txtboxSrcFolder2.Text))
{
if (Directory.Exists(txtboxSrcFolder2.Text) && Directory.Exists(txtboxDestFolder2.Text))
{
trans.szSourceFolderList.Add(txtboxSrcFolder2.Text);
trans.szDestinationFolderList.Add(txtboxDestFolder2.Text);
}
else
{
return 1;
}
}
if (!String.IsNullOrWhiteSpace(txtboxSrcFolder3.Text))
{
if (Directory.Exists(txtboxSrcFolder3.Text) && Directory.Exists(txtboxDestFolder3.Text))
{
trans.szSourceFolderList.Add(txtboxSrcFolder3.Text);
trans.szDestinationFolderList.Add(txtboxDestFolder3.Text);
}
else
{
return 1;
}
}
return 0;
}
Now I have to do the same thing for all of the textboxes. This seems cumbersome. Is there a more compact way to design this validation?
Related
I am designing a user control that has inside this user control (label, text box and check box list), I defined a property for this user control that when we enter a value inside it, a value from the database for We returned
Apparently, there is no problem with the code I wrote, but I don't actually receive an answer.
I wanted to get your help to solve this problem.
I will send you the code I wrote in the properties RegionID.
Thank you for your cooperation
private int _regionID = 0;
public int RegionID
{
get
{
return _regionID;
}
set
{
_regionID = value;
if (value < 1 && value> 4)
{
_regionID = 0;
}
else
{
Models.DataBaseContext dataBaseContext = null;
try
{
dataBaseContext =
new Models.DataBaseContext();
Models.Region region
= dataBaseContext.Regions
.Where(current => current.Region_Id == value)
.FirstOrDefault();
captionLabelControl.Text = region.Region_Name;
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
}
}
I am trying to find a code that helps to create a pattern of:
but the number of rows and columns are custom.
Currently this is the code I have but it doesnt apply properly for all values.
(labelseat.backcolour=color.red , refers to the red boxes)
count=0;
if (Row%2==0)
{
count+=1;
if (count==3)
{
labelSeat.BackColor=Color.Red;
}
else if (count==4)
{
labelSeat.BackColor=Color.Red;
count=0;
}
}
if (Row%2==1)
{
count+=1;
if (count==1)
{
labelSeat.BackColor=Color.Red;
}
else if (count==2)
{
labelSeat.BackColor=Color.Red;
}
else if (count==4)
{
count=0;
}
}
Maybe something like this:
private void ColorBox(int elementNumber)
{
boolean colorRed = ((int)elementNumber/2) % 2 == 0;
Box[elementNumber].backgroundColor = colorWhite ? "#FF0000" : "#FFFFFF";
}
Good day To All, i was wondering is there any possibility
Winform Items
A.) 1~5 ComboBox
B.) 1~5 Textbox for Time (i'll identify them for example as txtTime1 ~ txtTime5)
C.) 1~5 Textbox for Amount (i'll identify them for example as txtAmount1 ~ txtAmount5)
Items 1 ~ 5 (ComboBox 1-5 , txtTime 1-5, txtAmount 1-5) will do the same Functions.
A.)
if (combobox1.SelectedValue.ToString() == "Regular")
{
x = 1.25;
}
else if (combobox1.SelectedValue.ToString() == "Double")
{
x = 2;
}
// Same Codes for ComboBox 2~5
B.)Textbox "txtTime(s)" will hold a TextChange Event fetching values of our said comboBoxes
if (txtTime.Text.Lenght > 0)
{
// Item Letter "C"
// value of "x" is equal to the above item
txtAmount.Text = (double.Parse(txtTime.Text) * x).ToString();
}
i just need a quick idea on how will i make this work
Thank You in Advance
Edit*
All i can think of is calling them 1 by 1 just a quick code
private Method1()
{ double x,base;
if (combobox1 = "Regular")
{ x = base * 1.25; }
if (combobox2 = "Regular")
{ x = base * 1.25; }
// so on
return x;
}
private txtTime1_TextChange(Event ****)
{
if (txtTime1.Text.Lenght > 0)
{ txtAmount1.Text = (Method1() * double.Parse(txtTime1.Text)).ToString();}
private txtTime2_TextChange(Event ****)
{
if (txtTime2.Text.Lenght > 0)
{ txtAmount2.Text = (Method1() * double.Parse(txtTime2.Text)).ToString();}
// and so on
You can have a method as your controls event handler .Each event handler has a sender argument that represent the control that the event has fired for.
You can have a event handler like this :
public ComboBoxEventHandler(object sender,EventArgs args)
{
var comboBox=sender as ComboBox;
if(comboBox==null) return;
if (comboBox.SelectedValue.ToString() == "Regular")
{
x = 1.25;
}
else if (comboBox.SelectedValue.ToString() == "Double")
{
x = 2;
}
}}
You can do the same thing for other controls.
UPDATE
To make things easier to maintain you can have a class that contains corresponding controls and their behaviors then you can add your controls dynamically to your form without having to repeat yourself or you can add more rows to your form very easily .Take something like this for example:
public class RowController
{
public ComboBox Rate{get;private set;}
public TextBox Hours{get;private set;}
public TextBox Amount{get;private set;}
public RowController(ComboBox rate,TextBox hours,TextBox amount)
{
Rate=rate;
Hours=hours;
Hours.TextChange+=OnHoursChanged;
Amount=amount;
}
private void OnHoursChanged(object sender,EventArgs args)
{
if (Hours.Text.Length > 0)
{ Amount.Text = (GetRate() * double.Parse(Hours.Text)).ToString();}
}
private double GetRate()
{
if (Rate.SelectedValue.ToString() == "Regular")
{
return 1.25;
}
else if (Rate.SelectedValue.ToString() == "Double")
{
return 2;
}
}
}
Then you can define RowControllers in your form like this :
var row1=new RowController(comboBox1,txtTime1,txtAmount1);
and each controller will do its job by its own.
When the following function is called, it is not showing the corresponding photoBoxes. I've done a debugging walkthrough, it even reaches the parts necessary to Show() and Hide(). I don't know what I can do. It's not showing anything
public void SmokerTakeIngredientFromTable(agents agent, List<smokers> smoker)
{
int index = 0;
bool smoker_takes_ingredients = false;
while (!smoker_takes_ingredients)
{
if ((smoker[index].item != agent.item_1) && (smoker[index].item != agent.item_2))
{
if (index == 0)
{
leftarrow_img.Show();
rightarrow_img.Hide();
downarrow_img.Hide();
}
else if (index == 1)
{
leftarrow_img.Hide();
rightarrow_img.Show();
downarrow_img.Hide();
}
else if (index == 2)
{
leftarrow_img.Hide();
rightarrow_img.Hide();
downarrow_img.Show();
}
agent.item_1 = 3;
agent.item_2 = 3;
break;
}
index++;
}
}
This is what the designer for these photoBoxes look like:
This is the properties page for one of the photoBoxes (they are all identical apart from the actual image file, they all have Visible = false too)
When making visibility changes I need to refresh the form using this.Refresh()
I'm using Asp.Net 2.0. I have a scenario where i need to check a user input against any of two ranges. For e.g. I need to check a textbox value against ranges 100-200 or 500-600. I know that i can hook up 2 Asp.Net RangeValidators to the TextBox, but that will try to validate the input against both the ranges, an AND condition,if you will. CustomValidator is an option, but how would I pass the 2 ranges values from the server-side. Is it possible to extend the RangeValidator to solve this particular problem?
[Update]
Sorry I didn't mention this, the problem for me is that range can vary. And also the different controls in the page will have different ranges based on some condition. I know i can hold these values in some js variable or hidden input element, but it won't look very elegant.
A CustomValidator should work. I'm not sure what you mean by "pass the 2 ranges values from the server-side". You could validate it on the server-side using a validation method like this:
void ValidateRange(object sender, ServerValidateEventArgs e)
{
int input;
bool parseOk = int.TryParse(e.Value, out input);
e.IsValid = parseOk &&
((input >= 100 || input <= 200) ||
(input >= 500 || input <= 600));
}
You will then need to set the OnServerValidate property of your CustomValidator to "ValidateRange", or whatever you happen to call it.
Is this the sort of thing you're after?
I do not believe this is possible using the standard RangeValidator control.
I did some searching and I believe your best solution is going to be to create your own CustomValidator control which you can include in your project to handle this scenario.
http://www.dotnetjunkies.ddj.com/Article/592CE980-FB7E-4DF7-9AC1-FDD572776680.dcik
You shouldn't have to compile it just to use it in your project, as long as you reference it properly.
You can use the RegularExpressionValidator with the ValidationExpression property set to
Edit: (whoops, 650 and 201 etc. were valid with the old pattern)
^(1\d{2}|200|5\d{2}|600)$
This will test the entered text for 100-200 and 500-600.
I extended the BaseValidator to achieve this. Its fairly simple once you understand how Validators work. I've included a crude version of code to demonstrate how it can be done. Mind you it's tailored to my problem(like int's should always be > 0) but you can easily extend it.
public class RangeValidatorEx : BaseValidator
{
protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
{
base.AddAttributesToRender(writer);
if (base.RenderUplevel)
{
string clientId = this.ClientID;
// The attribute evaluation funciton holds the name of client-side js function.
Page.ClientScript.RegisterExpandoAttribute(clientId, "evaluationfunction", "RangeValidatorEx");
Page.ClientScript.RegisterExpandoAttribute(clientId, "Range1High", this.Range1High.ToString());
Page.ClientScript.RegisterExpandoAttribute(clientId, "Range2High", this.Range2High.ToString());
Page.ClientScript.RegisterExpandoAttribute(clientId, "Range1Low", this.Range1Low.ToString());
Page.ClientScript.RegisterExpandoAttribute(clientId, "Range2Low", this.Range2Low.ToString());
}
}
// Will be invoked to validate the parameters
protected override bool ControlPropertiesValid()
{
if ((Range1High <= 0) || (this.Range1Low <= 0) || (this.Range2High <= 0) || (this.Range2Low <= 0))
throw new HttpException("The range values cannot be less than zero");
return base.ControlPropertiesValid();
}
// used to validation on server-side
protected override bool EvaluateIsValid()
{
int code;
if (!Int32.TryParse(base.GetControlValidationValue(ControlToValidate), out code))
return false;
if ((code < this.Range1High && code > this.Range1Low) || (code < this.Range2High && code > this.Range2Low))
return true;
else
return false;
}
// inject the client-side script to page
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (base.RenderUplevel)
{
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "RangeValidatorEx", RangeValidatorExJs(),true);
}
}
string RangeValidatorExJs()
{
string js;
// the validator will be rendered as a SPAN tag on the client-side and it will passed to the validation function.
js = "function RangeValidatorEx(val){ "
+ " var code=document.getElementById(val.controltovalidate).value; "
+ " if ((code < rangeValidatorCtrl.Range1High && code > rangeValidatorCtrl.Range1Low ) || (code < rangeValidatorCtrl.Range2High && code > rangeValidatorCtrl.Range2Low)) return true; else return false;}";
return js;
}
public int Range1Low
{
get {
object obj2 = this.ViewState["Range1Low"];
if (obj2 != null)
return System.Convert.ToInt32(obj2);
return 0;
}
set { this.ViewState["Range1Low"] = value; }
}
public int Range1High
{
get
{
object obj2 = this.ViewState["Range1High"];
if (obj2 != null)
return System.Convert.ToInt32(obj2);
return 0;
}
set { this.ViewState["Range1High"] = value; }
}
public int Range2Low
{
get
{
object obj2 = this.ViewState["Range2Low"];
if (obj2 != null)
return System.Convert.ToInt32(obj2);
return 0;
}
set { this.ViewState["Range2Low"] = value; }
}
public int Range2High
{
get
{
object obj2 = this.ViewState["Range2High"];
if (obj2 != null)
return System.Convert.ToInt32(obj2);
return 0;
}
set { this.ViewState["Range2High"] = value; }
}
}