UseMnemonic is being affected by FlatStyle on a Button - c#

I'm trying to enable UseMnemonic on a button so that the amprasand (&) will show up for me. The works fine unless I set the buttons flatStyle to System (all other flatstyles are fine).
Labels work fine with the same flatStyle, this seems to be specific to buttons.
Does any one know why this happens or a way behind it?
At the moment the only thing I can think of is ignore the UseMnemonic and add and extra & if one is found.
Also something else weird is that sometimes the & gets replaced with an underscore (_) but I can't yet reproduce this at will...
Far from the most effective code but something I through together to test this:
public Form1()
{
InitializeComponent();
this.label1.Text = "hello & goodbye";
this.button1.Text = "1&2";
this.label3.Text = this.button1.UseMnemonic.ToString();
this.label4.Text = this.button1.FlatStyle.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
switch (count)
{
case 0:
case 1:
this.label1.UseMnemonic = this.button1.UseMnemonic = false;
this.label1.FlatStyle = this.button1.FlatStyle = FlatStyle.System;
count = 2;
break;
case 2:
this.label1.UseMnemonic = this.button1.UseMnemonic = false;
this.label1.FlatStyle = this.button1.FlatStyle = FlatStyle.Flat;
count = 3;
break;
case 3:
this.label1.UseMnemonic = this.button1.UseMnemonic = false;
this.label1.FlatStyle = this.button1.FlatStyle = FlatStyle.Popup;
count = 4;
break;
case 4:
this.label1.UseMnemonic = this.button1.UseMnemonic = false;
this.label1.FlatStyle = this.button1.FlatStyle = FlatStyle.Standard;
count = 5;
break;
case 5:
this.label1.UseMnemonic = this.button1.UseMnemonic = true;
this.label1.FlatStyle = this.button1.FlatStyle = FlatStyle.System;
count = 6;
break;
case 6:
this.label1.UseMnemonic = this.button1.UseMnemonic = true;
this.label1.FlatStyle = this.button1.FlatStyle = FlatStyle.Flat;
count = 7;
break;
case 7:
this.label1.UseMnemonic = this.button1.UseMnemonic = true;
this.label1.FlatStyle = this.button1.FlatStyle = FlatStyle.Popup;
count = 8;
break;
case 8:
this.label1.UseMnemonic = this.button1.UseMnemonic = true;
this.label1.FlatStyle = this.button1.FlatStyle = FlatStyle.Standard;
count = 1;
break;
}
this.label3.Text = this.button1.UseMnemonic.ToString();
this.label4.Text = this.button1.FlatStyle.ToString();

Not sure I follow. But to get the ampersand to show up, you have to double it. Fix:
this.button1.Text = "1&&2";

Related

C# - Need help generating a random number and using the same number multiple times in other functions

I'm trying to make a psudo-Shut The Box program that will roll dice and user has the option to choose between covering the number corresponding to both the dice or the sum of the dice.
It seems like everything is working perfectly so far except for one thing. When I try to click the button for either of the user input choices, it is clear that it is still generating random numbers due to the wrong check boxes being checked.
I suspect that this is a result of how I am calling the functions. Is there a way to pass the random value from dice one and dice two into something that will stay the same if called in other functions while being able to be randomly generated again after pressing the roll dice button?
private int firstDiceRandom()
{
Random diceOne = new Random();
int oneDie = diceOne.Next(1, 7);
System.Threading.Thread.Sleep(100);
return oneDie;
}
// Set value of Random number to a value
private int firstDiceValue()
{
int value = firstDiceRandom();
return value;
}
// Random number for Dice Two
private int secondDiceRandom()
{
Random diceTwo = new Random();
int twoDie = diceTwo.Next(1, 7);
return twoDie;
}
// Set value of Random number to a value
private int secondDiceValue()
{
int value = secondDiceRandom();
return value;
}
// Set value of Total random numbers
private int totalDice()
{
int totalDice = firstDiceValue() + secondDiceValue();
return totalDice;
}
// Check if boxes 7-12 are checked
private bool bothDiceBool()
{
if (checkBox7.Checked && checkBox8.Checked && checkBox9.Checked && checkBox10.Checked && checkBox11.Checked && checkBox12.Checked)
{
return false;
}
return true;
}
// Roll Dice Button and Generation of dice pictures for values.
private void rollDiceButton_Click(object sender, EventArgs e)
{
int oneDie = firstDiceValue();
int twoDie = secondDiceValue();
switch (oneDie)
{
case 1:
diceOneImage.Image = new Bitmap(#"location");
break;
case 2:
diceOneImage.Image = new Bitmap(#"location");
break;
case 3:
diceOneImage.Image = new Bitmap(#"location");
break;
case 4:
diceOneImage.Image = new Bitmap(#"location");
break;
case 5:
diceOneImage.Image = new Bitmap(#"location");
break;
case 6:
diceOneImage.Image = new Bitmap(#"location");
break;
}
//If 7-12 are all checked, not supposed to run, sends a blank picture where the die would be.
if (bothDiceBool() == true)
{
switch (twoDie)
{
case 1:
diceTwoImage.Image = new Bitmap(#"location");
break;
case 2:
diceTwoImage.Image = new Bitmap(#"location");
break;
case 3:
diceTwoImage.Image = new Bitmap(#"location");
break;
case 4:
diceTwoImage.Image = new Bitmap(#"location");
break;
case 5:
diceTwoImage.Image = new Bitmap(#"location");
break;
case 6:
diceTwoImage.Image = new Bitmap(#"location");
break;
}
}
// Blank die picture
else
{
diceTwoImage.Image = new Bitmap(#"location");
}
}
// User choice button
private void button2_Click(object sender, EventArgs e)
{
int buttonOneTotal = totalDice();
// If Choice One selected when button pressed
if (radioButton1.Checked)
{
switch (buttonOneTotal)
{
case 2:
checkBox2.Checked = true;
radioButton1.Checked = false;
break;
case 3:
checkBox3.Checked = true;
radioButton1.Checked = false;
break;
case 4:
checkBox4.Checked = true;
radioButton1.Checked = false;
break;
case 5:
checkBox5.Checked = true;
radioButton1.Checked = false;
break;
case 6:
checkBox6.Checked = true; radioButton1.Checked = false;
break;
case 7:
checkBox7.Checked = true;
radioButton1.Checked = false;
break;
case 8:
checkBox8.Checked = true;
radioButton1.Checked = false;
break;
case 9:
checkBox9.Checked = true;
radioButton1.Checked = false;
break;
case 10:
checkBox10.Checked = true;
radioButton1.Checked = false;
break;
case 11:
checkBox11.Checked = true;
radioButton1.Checked = false;
break;
case 12:
checkBox12.Checked = true;
radioButton1.Checked = false;
break;
}
}
// If Choice two selected when button pressed
else if (radioButton2.Checked)
{
int buttonTwoOne = firstDiceValue();
int buttonTwoTwo = secondDiceValue();
// First Die
switch (buttonTwoOne)
{
case 1:
checkBox1.Checked = true;
radioButton2.Checked = false;
break;
case 2:
checkBox2.Checked = true;
radioButton2.Checked = false;
break;
case 3:
checkBox3.Checked = true;
radioButton2.Checked = false;
break;
case 4:
checkBox4.Checked = true;
radioButton2.Checked = false;
break;
case 5:
checkBox5.Checked = true;
radioButton2.Checked = false;
break;
case 6:
checkBox6.Checked = true;
radioButton2.Checked = false;
break;
}
// Second Die
switch (buttonTwoTwo)
{
case 1:
checkBox1.Checked = true;
radioButton1.Checked = false;
break;
case 2:
checkBox2.Checked = true;
radioButton1.Checked = false;
break;
case 3:
checkBox3.Checked = true;
radioButton1.Checked = false;
break;
case 4:
checkBox4.Checked = true;
radioButton1.Checked = false;
break;
case 5:
checkBox5.Checked = true;
radioButton1.Checked = false;
break;
case 6:
checkBox6.Checked = true;
radioButton1.Checked = false;
break;
}
}
}
One solution that can help your case is declaring a variable outside the scope of this program. In other words a public variable.
public int RandomValueOne;
Now what you want to do is store your random value inside this public variable.
private int firstDiceValue()
{
RandomValueOne = firstDiceRandom();
return RandomValueOne;
}
Now you can use your variable directly without any fear of numbers re-generating. Call your function "FirstDiceValue" ONCE and keep using the global variable isntead

Values not retained after postback during a method call

I am currently working on migration of a vb.net desktop application into a web application, in the desktop application the previous developer had declared all the variables once on the
form_load
stdate = VB6.Format(Now, "mmddyyyyhh")
current_month = CInt(Mid(stdate, 1, 2))
current_day = CInt(Mid(stdate, 3, 2))
current_year = CInt(Mid(stdate, 5, 4))
current_hour = CInt(Mid(stdate, 9, 2))
base_year = current_year
this_year = current_year
base_month = current_month
this_month = current_month
txtBaseyear.Text = CStr(base_year)
txtBaseMonth.Text = CStr(base_month)
swyearerror = 0
They were then able to use these variables anywhere in the code and it would retain the values.
my application
protected void Page_Load(object sender, EventArgs e)
{
DateTime now = DateTime.Now;
string format = "MMdyyyyhh";
string stdate = now.ToString(format);
Session["stdate"] = stdate;
int current_month = Convert.ToInt32(now.Month);
Session["currentmonth"] = current_month;
int current_day = Convert.ToInt32(now.Day);
Session["currentday"] = current_day;
int current_year = Convert.ToInt32(now.Year);
Session["currentyear"] = current_year;
int current_hour = Convert.ToInt32(now.Hour);
Session["currenthour"] = current_hour;
int base_year = (int)(Session["currentyear"]);
int this_year = (int)(Session["currentyear"]);
int base_month = (int)(Session["currentmonth"]);
int this_month = (int)(Session["currentmonth"]);
TxtBase.Text = Convert.ToString(base_year);
TxtBase1.Text = Convert.ToString(base_month);
}
Now each time there is a post-back when the text is changed the values are lost and set to zero so I tried storing it in a session but that still doesn't work.
Let me give you a visual of my situation.
1) on the first load everything loads fine.
2) then the users enters an item and hits enter
protected void TxtItem_TextChanged(object sender, EventArgs e)
{
Calc_Rotation();
Calc_Best_Before();
}
now...
public void Calc_Rotation()
{
switch (current_month) becomes 0 suppose to be 4
{
case 1:
rotation_month = "A";
break;
case 2:
rotation_month = "B";
break;
case 3:
rotation_month = "G";
break;
case 4:
rotation_month = "J";
break;
case 5:
rotation_month = "K";
break;
case 6:
rotation_month = "L";
break;
case 7:
rotation_month = "N";
break;
case 8:
rotation_month = "P";
break;
case 9:
rotation_month = "S";
break;
case 10:
rotation_month = "W";
break;
case 11:
rotation_month = "Y";
break;
case 12:
rotation_month = "Z";
break;
}
switch (current_hour) becomes 0 suppose to be the current hour
{
case 0:
case 1:
rotation_batch = 2;
break;
case 2:
case 3:
rotation_batch = 4;
break;
case 4:
case 5:
rotation_batch = 6;
break;
case 6:
case 7:
rotation_batch = 8;
break;
case 8:
case 9:
rotation_batch = 10;
break;
case 10:
case 11:
rotation_batch = 12;
break;
case 12:
case 13:
rotation_batch = 14;
break;
case 14:
case 15:
rotation_batch = 16;
break;
case 16:
case 17:
rotation_batch = 18;
break;
case 18:
case 19:
rotation_batch = 20;
break;
case 20:
case 21:
rotation_batch = 22;
break;
case 22:
case 23:
rotation_batch = 24;
break;
}
}
same goes for the Calc_Best_Before
after I tried the postback solution I am still getting 0 values.
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
return;
DateTime now = DateTime.Now;
string format = "MMdyyyyhh";
string stdate = now.ToString(format);
Session["stdate"] = stdate;
int current_month = Convert.ToInt32(now.Month);
Session["currentmonth"] = current_month;
int current_day = Convert.ToInt32(now.Day);
Session["currentday"] = current_day;
int current_year = Convert.ToInt32(now.Year);
Session["currentyear"] = current_year;
int current_hour = Convert.ToInt32(now.Hour);
Session["currenthour"] = current_hour;
int base_year = (int)(Session["currentyear"]);
int this_year = (int)(Session["currentyear"]);
int base_month = (int)(Session["currentmonth"]);
int this_month = (int)(Session["currentmonth"]);
TxtBase.Text = Convert.ToString(base_year);
TxtBase1.Text = Convert.ToString(base_month);
}
Put your code inside the below code. If the web page loads for the first time IsPostBack property is 'false' and code inside the if condition will be executed.
if the page loads after a post back IsPostBack turns to 'true' which cause program execution to escape the if condition.
if (!IsPostBack)
{
}
In case you are using Session to store the values, you need to ensure that you are checking the IsPostBack Property. Otherwise, everytime the page postback occurs, you are initializing the values again and thus you loose the state.
Page.IsPostBack property basically signifies whether the Page is accessing the server for the first time or not.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DateTime now = DateTime.Now;
string format = "MMdyyyyhh";
string stdate = now.ToString(format);
Session["stdate"] = stdate;
int current_month = Convert.ToInt32(now.Month);
Session["currentmonth"] = current_month;
int current_day = Convert.ToInt32(now.Day);
Session["currentday"] = current_day;
int current_year = Convert.ToInt32(now.Year);
Session["currentyear"] = current_year;
int current_hour = Convert.ToInt32(now.Hour);
Session["currenthour"] = current_hour;
int base_year = (int)(Session["currentyear"]);
int this_year = (int)(Session["currentyear"]);
int base_month = (int)(Session["currentmonth"]);
int this_month = (int)(Session["currentmonth"]);
TxtBase.Text = Convert.ToString(base_year);
TxtBase1.Text = Convert.ToString(base_month);
}
}
UPDATE 1:
#CodeMan: You need to use the Session to read the value for the current month in the function Calc_Rotation. You are trying to read the value from the variable current_month which is an int and would default to zero.
I would suggest you to use a Property to wrap up the Session Variable and then use the property in the code. Below, i have created a wrapper property CurrentMonth which encapsulates the Read and write operations on Session in the webpage, declared at class scope. Simmilary you can create wrapper properties for other variables as well. After creating the properties, you need to read the property value in the Calc_Rotation function.
private int CurrentMonth {
get {
var monthfromSession = HttpContext.Current.Session["ClassNameSpace.CurrentMonth"];
return monthfromSession!= null ? (int)monthfromSession : 0;
}
set {
HttpContext.Current.Session["ClassNameSpace.CurrentMonth"] = value;
}
}
public void Calc_Rotation()
{
switch (this.CurrentMonth) // it should have correct month value now.
{
case 1:
rotation_month = "A";
break;
case 2:
rotation_month = "B";
break;
case 3:
....
}
}

c# calculator minus not working properly

I've got to make a calculator for school, everything works except the minus button: 5-5 = -5 for example.
I know that the problem exists somewhere in my case "minus" but I just
can't get it figured out..
Here's the code.
Most of the variables are in dutch, I hope that won't be a problem!
Note: this problem exists when i do this: 5 - 5, so not using the equals button.
private int totaal = 0;
private Boolean lastClicked, equalsPressed, tussen = false;
public MainWindow()
{
InitializeComponent();
}
private void button_Click(object sender, EventArgs e)
{
switch(((Button)sender).Name){
case "one":
if (tussen)
{
uitkomstBox.Text = "";
tussen = false;
}
if (equalsPressed)
{
uitkomstBox.Text = uitkomstBox.Text + "1";
equalsPressed = false;
}
else
{
uitkomstBox.Text = uitkomstBox.Text + "1";
}
break;
case "two":
if (tussen)
{
uitkomstBox.Text = "";
tussen = false;
}
if (equalsPressed)
{
uitkomstBox.Text = uitkomstBox.Text + "2";
equalsPressed = false;
}
else
{
uitkomstBox.Text = uitkomstBox.Text + "2";
}
break;
case "three":
if (tussen)
{
uitkomstBox.Text = "";
tussen = false;
}
if (equalsPressed)
{
uitkomstBox.Text = uitkomstBox.Text + "3";
equalsPressed = false;
}
else
{
uitkomstBox.Text = uitkomstBox.Text + "3";
}
break;
case "four":
if (tussen)
{
uitkomstBox.Text = "";
tussen = false;
}
if (equalsPressed)
{
uitkomstBox.Text = uitkomstBox.Text + "4";
equalsPressed = false;
}
else
{
uitkomstBox.Text = uitkomstBox.Text + "4";
}
break;
case "five":
if (tussen)
{
uitkomstBox.Text = "";
tussen = false;
}
if (equalsPressed)
{
uitkomstBox.Text = uitkomstBox.Text + "5";
equalsPressed = false;
}
else
{
uitkomstBox.Text = uitkomstBox.Text + "5";
}
break;
case "six":
if (tussen)
{
uitkomstBox.Text = "";
tussen = false;
}
if (equalsPressed)
{
uitkomstBox.Text = uitkomstBox.Text + "6";
equalsPressed = false;
}
else
{
uitkomstBox.Text = uitkomstBox.Text + "6";
}
break;
case "seven":
if (tussen)
{
uitkomstBox.Text = "";
tussen = false;
}
if (equalsPressed)
{
uitkomstBox.Text = uitkomstBox.Text + "7";
equalsPressed = false;
}
else
{
uitkomstBox.Text = uitkomstBox.Text + "7";
}
break;
case "eight":
if (tussen)
{
uitkomstBox.Text = "";
tussen = false;
}
if (equalsPressed)
{
uitkomstBox.Text = uitkomstBox.Text + "8";
equalsPressed = false;
}
else
{
uitkomstBox.Text = uitkomstBox.Text + "8";
}
break;
case "nine":
if (tussen)
{
uitkomstBox.Text = "";
tussen = false;
}
if (equalsPressed)
{
uitkomstBox.Text = uitkomstBox.Text + "9";
equalsPressed = false;
}
else
{
uitkomstBox.Text = uitkomstBox.Text + "9";
}
break;
case "zero":
if (tussen)
{
uitkomstBox.Text = "";
tussen = false;
}
if (equalsPressed)
{
uitkomstBox.Text = uitkomstBox.Text + "0";
equalsPressed = false;
}
else
{
uitkomstBox.Text = uitkomstBox.Text + "0";
}
break;
case "plus":
totaal = totaal + Convert.ToInt32(uitkomstBox.Text);
uitkomstBox.Text = totaal.ToString();
tussen = true;
lastClicked = true;
break;
case "minus":
totaal = totaal - Convert.ToInt32(uitkomstBox.Text);
uitkomstBox.Text = totaal.ToString();
tussen = true;
lastClicked = false;
break;
case "clear":
uitkomstBox.Text = "";
totaal = 0;
break;
case "equals":
if (lastClicked)
{
uitkomstBox.Text = (totaal + Convert.ToInt32(uitkomstBox.Text)).ToString();
}
else
{
uitkomstBox.Text = (Convert.ToInt32(uitkomstBox.Text) - totaal).ToString();
}
equalsPressed = true;
totaal = 0;
break;
}
}
}
}
The problem is that you are subtracting the number twice. Once in the switch statement and then again here:
if (lastClicked)
{
uitkomstBox.Text = (totaal + Convert.ToInt32(uitkomstBox.Text)).ToString();
}
else
{
// You have already subtracted the number but then you are subtracting it again!
uitkomstBox.Text = (Convert.ToInt32(uitkomstBox.Text) - totaal).ToString();
}
It's the logic of the plus and minus buttons you need to check.
Take yourself through the code step by step.
In the beginning, totaal = 0 and uitkomnstBox.Text is empty. Then you click "5". So uitkomstBox.Text is "5". Then you click the "minus" button. totaal now changes to -5. i.e. before you have even entered a number to subtract from it. Then you click "5" again (to complete 5 - 5) but totaal doesn't change in this case until you click the equals button, at which point it hits that lastClicked logic which I don't really get... (I think it should end up as zero after that but you said the problem exists if you don't click "equals").
So it's not just that you're subtracting twice but you're subtracting before you even have a number to subtract.
As a possible solution, what I'd suggest is something like this: execute the logic of the operation ("plus" or "minus") after the next number is complete.
i.e. When someone clicks "minus" save this in memory somewhere until they have finished entering the next number, then subtract it from the total, or if they clicked "plus" add it. For the first number, process "plus" (e.g. 5 - 5 is processed as if it was entered as + 5 - 5)
private char operation = '+'; // Default first operation to "plus"
// Your other code...
// ...
// ...
case "plus" :
if (operation == '+') {
totaal += Convert.ToInt32(uitkomnstBox.Text);
}
if (operation == '-') {
totaal -= Convert.ToInt32(uitkomnstBox.Text);
}
operation = '+';
// anything else you want
break;
case "minus" :
if (operation == '+') {
totaal += Convert.ToInt32(uitkomnstBox.Text);
}
if (operation == '-') {
totaal -= Convert.ToInt32(uitkomnstBox.Text);
}
operation = '-';
// anything else you want
break;
case "equals" :
if (operation == '+') {
totaal += Convert.ToInt32(uitkomnstBox.Text);
}
if (operation == '-') {
totaal -= Convert.ToInt32(uitkomnstBox.Text);
}
operation = '+'; // Set back to default for next calculation
// anything else you want
break;
It looks like you are subtracting from totaal twice. Are you sure that adding works correctly? Check the code for your "=" button.
Also, maybe consider storing numbers for calculation in some counter int (or any other number type, if you are planning to allow fractions), rather than in textbox, since then you have to convert it back and forth just for the calculation.

How to manage Label in c#

I am trying to organize Label and TextBoxes in c# i have two function :
private void BtnKaydet_Click(object sender, EventArgs e)
{
_service = new Client.BioAuthenticationService.BioAuthenticationService();
int warningcase = 0;
if ((string.IsNullOrEmpty(TbTcNo.Text) || string.IsNullOrWhiteSpace(TbTcNo.Text)))
{
warningcase = 1;
TextLabelManagement(warningcase);
}
else if ((string.IsNullOrEmpty(TbId.Text) || string.IsNullOrWhiteSpace(TbId.Text)))
{
warningcase = 2;
TextLabelManagement(warningcase);
}
else if ((string.IsNullOrEmpty(TbName.Text) || string.IsNullOrWhiteSpace(TbName.Text)))
{
warningcase = 3;
TextLabelManagement(warningcase);
}
else if ((string.IsNullOrEmpty(TbSurname.Text) || string.IsNullOrWhiteSpace(TbSurname.Text)))
{
warningcase = 4;
TextLabelManagement(warningcase);
}
else if ((string.IsNullOrEmpty(TbDepartment.Text) || string.IsNullOrWhiteSpace(TbDepartment.Text)))
{
warningcase = 5;
TextLabelManagement(warningcase);
}
else
{
if (_imageIndex == 3)
{
bool enrollResult = _service.CheckAndEnrollUser(image1, image2, image3, 300, 6);
}
else
{
warningcase = 6;
TextLabelManagement(warningcase);
}
}
}
Here i write cases if TextBox is null i will give error message to fill them. Here is my cases :
private void TextLabelManagement(int cases)
{
switch (cases)
{
case 1:
LblWarning.Visible = true;
LblWarning.Text = "* Lütfen Bu Alanları Doldurunuz..";
LblTcNo.Text = "* TC No";
LblTcNo.ForeColor = System.Drawing.Color.Red;
LblWarning.ForeColor = System.Drawing.Color.Red;
break;
case 2:
LblWarning.Visible = true;
LblWarning.Text = "* Lütfen Bu Alanları Doldurunuz..";
LblId.Text = "* ID";
LblId.ForeColor = System.Drawing.Color.Red;
LblWarning.ForeColor = System.Drawing.Color.Red;
break;
case 3:
LblWarning.Visible = true;
LblWarning.Text = "* Lütfen Bu Alanları Doldurunuz..";
LblName.Text = "* Ad";
LblName.ForeColor = System.Drawing.Color.Red;
LblWarning.ForeColor = System.Drawing.Color.Red;
break;
case 4:
LblWarning.Visible = true;
LblWarning.Text = "* Lütfen Bu Alanları Doldurunuz..";
LblSurname.Text = "* Soyad";
LblSurname.ForeColor = System.Drawing.Color.Red;
LblWarning.ForeColor = System.Drawing.Color.Red;
break;
case 5:
LblWarning.Visible = true;
LblWarning.Text = "* Lütfen Bu Alanları Doldurunuz..";
LblDepartment.Text = "* Soyad";
LblDepartment.ForeColor = System.Drawing.Color.Red;
LblWarning.ForeColor = System.Drawing.Color.Red;
break;
case 6:
LblFingerWarning.Visible = true;
LblFingerWarning.Text = "Lütfen Parmak İzinizi Üç Kez Veriniz.";
LblFingerWarning.ForeColor = System.Drawing.Color.Red;
break;
default:
break;
}
}
However when user click Save button it will enter first IF condition. Then Else If .. But here is my problem. How i can organize these items. For example if user did't fill all boxes i want to give him all warning message not step by step.
You can use a validator for text box. The validator controls that you would find in toolbox.
Try the link
Use if instead of else if because when first if is true it will not enter into else if, so only one message is displayed. Use it as :
if ((string.IsNullOrEmpty(TbTcNo.Text) || string.IsNullOrWhiteSpace(TbTcNo.Text)))
{
warningcase = 1;
TextLabelManagement(warningcase);
}
if ((string.IsNullOrEmpty(TbId.Text) || string.IsNullOrWhiteSpace(TbId.Text)))
{
warningcase = 2;
TextLabelManagement(warningcase);
}
if ((string.IsNullOrEmpty(TbName.Text) || string.IsNullOrWhiteSpace(TbName.Text)))
{
warningcase = 3;
TextLabelManagement(warningcase);
}
Winforms user control validation = http://msdn.microsoft.com/en-us/library/ms229603(v=vs.110).aspx
ASP.NET user control validation = http://msdn.microsoft.com/en-us/library/7kh55542.ASPX

NullReferenceException even when control is present in the previous page

Whenever I try to retrieve the previous page dropdownlist control in bedCount() mwethod it gives this exception while I rest assure you that the control which I am searching is very much present in the previous page. What is the reason for this? My code is given below :
public partial class Room2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DropDownList[] adult=new DropDownList[6];
DropDownList[] child = new DropDownList[6];
TableRow[] trp = new TableRow[5];
TableRow[] trc ={ trc1, trc2, trc3, trc4, trc5 };
DropDownList[] rtype ={ DropDownList1,DropDownList2, DropDownList3, DropDownList4, DropDownList5, DropDownList6 };
Label[] bed ={Label1,Label2,Label3,Label4,Label5,Label6};
int i,x,c=2;
for (i = 0; i < 5; i++)
{
trp[i] = (TableRow)PreviousPage.Master.FindControl("ContentPlaceHolder1").FindControl("tr" + (i + 1));
}
for (i = 0; i < 6; i++,c+=4)
{
adult[i] = (DropDownList)PreviousPage.Master.FindControl("ContentPlaceHolder1").FindControl("DropDownList" + c++);
child[i] = (DropDownList)PreviousPage.Master.FindControl("ContentPlaceHolder1").FindControl("DropDownList" + c);
}
for (i = 0; i < 6; i++)
{
x = adult[i].SelectedIndex + child[i].SelectedIndex;
switch (x)
{
case 0:
case 1:
rtype[i].Items.Add("Executive Class");
rtype[i].Items.Add("Business Class");
rtype[i].Items.Add("Gold Class (Type-I)");
rtype[i].Items.Add("Gold Class (Type-II)");
rtype[i].SelectedIndex = 0;
bed[i].Text = "No";
break;
case 2:
rtype[i].Items.Add("Business Class");
rtype[i].Items.Add("Gold Class (Type-I)");
rtype[i].Items.Add("Gold Class (Type-II)");
rtype[i].SelectedIndex = 0;
bed[i].Text = "1";
break;
case 3:
bed[i].Text = "1";
goto case 4;
case 4:
rtype[i].Items.Add("Gold Class (Type-I)");
rtype[i].Items.Add("Gold Class (Type-II)");
rtype[i].SelectedIndex = 0;
bed[i].Text = "2";
break;
case 5:
bed[i].Text = "2";
goto case 6;
case 6:
rtype[i].Items.Add("Gold Class (Type-II)");
rtype[i].SelectedIndex = 0;
bed[i].Text = "3";
break;
}
if (i<5 && trp[i].Visible)
trc[i].Visible = true;
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if(DropDownList1.Items.Count<=3)
bedCount(DropDownList1,Label1,0);
}
protected void bedCount(DropDownList d,Label l,int a)
{
protected void bedCount(DropDownList d,Label l,int x)
{
DropDownList a=new DropDownList();
DropDownList c = new DropDownList();
int s;
a = (DropDownList)PreviousPage.Master.FindControl("ContentPlaceHolder1").FindControl("DropDownList"+x++);//gives exception here
c = (DropDownList)PreviousPage.Master.FindControl("ContentPlaceHolder1").FindControl("DropDownList"+x);
s = c.SelectedIndex + c.SelectedIndex;
if (d.SelectedItem.Equals("Business Class"))
if(s==2)
l.Text = "1";
else
l.Text = "No";
else if(d.SelectedItem.Equals("Gold Class (Type-I)"))
if(s==3)
l.Text = "1";
else if(s==4)
l.Text = "2";
else
l.Text = "No";
else if(d.SelectedItem.Equals("Gold Class (Type-II)"))
if(s==4)
l.Text = "1";
else if(s==5)
l.Text = "2";
else if(s==6)
l.Text = "3";
else
l.Text = "No";
}
When you change selected item in DropDownList1, then each item of your arrays
DropDownList[] adult=new DropDownList[6];
DropDownList[] child = new DropDownList[6];
will become a null because the page is recreated after each postback (even after changing dropdown list selected item)
On the first load of page you get your arrays filled because you fill them manually in Page_Load

Categories

Resources