How to manage Label in c# - 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

Related

How to shorten if-else-if statement in c#

Can someone shorten this code?
There are 14 Buttons and 8 text Boxes. The action will be perform if the textbox is not empty and if it is not empty, then when you click it the button corresponding with the letter in the textbox will be visible again making the textbox empty.
private void txt1_Click(object sender, EventArgs e)
{
if (txt1.Text == "J")
{
txt1.Text = "";
btn1.Visible = true;
}
else if (txt1.Text == "M")
{
txt1.Text = "";
btn2.Visible = true;
}
else if (txt1.Text == "Y")
{
txt1.Text = "";
btn3.Visible = true;
}
else if (txt1.Text == "E")
{
if (btn4.Visible == true)
{
txt1.Text = "";
btn5.Visible = true;
}
else
{
txt1.Text = "";
btn4.Visible = true;
}
}
else if (txt1.Text == "Q")
{
txt1.Text = "";
btn6.Visible = true;
}
else if (txt1.Text == "L")
{
if (btn7.Visible == true)
{
txt1.Text = "";
btn10.Visible = true;
}
else
{
txt1.Text = "";
btn7.Visible = true;
}
}
else if (txt1.Text == "B")
{
txt1.Text = "";
btn8.Visible = true;
}
else if (txt1.Text == "C")
{
txt1.Text = "";
btn9.Visible = true;
}
else if (txt1.Text == "P")
{
txt1.Text = "";
btn11.Visible = true;
}
else if (txt1.Text == "I")
{
txt1.Text = "";
btn12.Visible = true;
}
else if (txt1.Text == "K")
{
txt1.Text = "";
btn13.Visible = true;
}
else if (txt1.Text == "O")
{
txt1.Text = "";
btn14.Visible = true;
}
}
Assuming all the btn variables are part of the state of your class then you can declare a method like so:
public Button Click(String txt) {
switch(txt) {
case "J":
return btn1;
case "M":
return btn2;
case "Y":
return btn3;
case "E":
return (btn4.Visible ? btn5 : btn4);
case "Q":
return btn6;
case "L":
return (btn7.Visible ? btn10 : btn7);
case "B":
return btn8;
case "C":
return btn9;
case "P":
return btn11;
case "I":
return btn12;
case "K":
return btn13;
case "O":
return btn14;
}
return null;
}
and then you call it:
var button = Click(txt1.Text);
if(button != null) {
button.Visible = true;
txt1.Text = "";
}
If however the btn variables have local scope, than instead of a method you can just define an inline Func<String,Button> delegate as so:
Func<String, Button> Click = txt => {
switch(txt) {
...
}
};
You'd still have to handle the special cases ("E" and "L") but you could use a Dictionary<string, Button> which would allow you to do a lookup:
var buttonDictionary = new Dictionary<string, Button>();
buttonDictionary["J"] = btn1;
buttonDictionary["M"] = btn2;
//etc...
if (buttonDictionary.Keys.Contains(txt1.Text))
{
txt1.Text = "";
buttonDictionary[txt1.Text].Visible = false;
}
That would reduce most of the repetitive code.
Looking at this: I'd suggest you go back think of more logical way to do whatever you're doing.
Out of my experience, any enormous chains of if-else in code represent that there has been a logical issue somewhere.
Also, familiarize yourself with switch statements

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 find label controls in an asp.net page

I have some label controls in a panel with id ResultsPanel. To find the label controls on the page, I did the following:
for (int ctr = 0; ctr < lblString.Length - 1; ctr++)
{
//string labelID = string.Format("lblResult{0}", ctr);
int mylbl = ctr + 1;
string lblResult = ((Label)ResultsPanel.FindControl("lblResult" + mylbl.ToString())).Text;
lblResult = lblString[mylbl].ToString();
}
lblResult1.Text = lblString.ToString();
lblString is a stringBuilder Object with 24 numbers. The Idea is to map each of the numbers in the stringbuilder object to labels in this manner:
lblResult1.Text = lblString[mylbl].ToString(); to the 24th label. But I can't seem to generate the labels and map the values to the label control.
Change the line to
Label lblResult = ((Label)ResultsPanel.FindControl("lblResult" + mylbl.ToString()));
lblResult.Text = lblString[mylbl].ToString();
Your question and code are a bit misleading, but I will try my best anyway.
Your code:
for (int ctr = 0; ctr < lblString.Length - 1; ctr++)
{
//string labelID = string.Format("lblResult{0}", ctr);
int mylbl = ctr + 1;
string lblResult = ((Label)ResultsPanel.FindControl("lblResult" + mylbl.ToString())).Text;
lblResult = lblString[mylbl].ToString();
}
lblResult1.Text = lblString.ToString();
If you have labels in the order of label1, label2, label3 ... then I would write something like this:
private void MapLabels()
{
var labelsResult = string.Empty;
var labelString = "123456789";
for (int i = 0; i < labelString.Length; i++)
{
var control = this.FindControl("label" + labelString[i]);
if(control != null)
{
labelsResult += ((Label)control).Text;
}
}
labelResult1.Text = labelsResult;
}
Let me know if you get stuck.
Hope this will help you. Like this you can get the value of label too.
public void GetControlsValuePopulated(Control control, Type controlType, Dictionary<string, string> dictControlPageValParam)
{
try
{
if (control.HasControls())
{
GetControlsValuePopulated(control, controlType, dictControlPageValParam);
}
else
{
if (controlType == null || controlType.IsAssignableFrom(control.GetType()))
{
if (control.ID != null)
{
bool FoundControl = dictControlPageValParam.ContainsKey(control.ID.Substring(3));
if (FoundControl)
{
switch (control.GetType().ToString())
{
case "System.Web.UI.WebControls.TextBox":
TextBox txt = (TextBox)control;
txt.Text = dictControlPageValParam[txt.ID.Substring(3)];
break;
case "System.Web.UI.WebControls.CheckBox":
CheckBox chk = (CheckBox)control;
if (dictControlPageValParam[chk.ID.Substring(3)].ToUpper() == "TRUE" || dictControlPageValParam[chk.ID.Substring(3)].ToUpper() == "T")
{
chk.Checked = true;
}
else
{
chk.Checked = false;
}
break;
case "System.Web.UI.WebControls.DropDownList":
DropDownList ddl = (DropDownList)control;
//ddl.SelectedValue = dictControlPageValParam[ddl.ID.Substring(3)];
break;
case "System.Web.UI.WebControls.RadioButtonList":
RadioButtonList rbl = (RadioButtonList)control;
rbl.SelectedValue = dictControlPageValParam[rbl.ID.Substring(3)];
break;
case "System.Web.UI.WebControls.HiddenField":
HiddenField hdn = (HiddenField)control;
hdn.Value = dictControlPageValParam[hdn.ID.Substring(3)];
break;
}
}
}
}
}
}
catch (Exception)
{
throw;
}
}
public void GetChildControlsId(Control control, Type controlType)
{
try
{
if (control.HasControls())
{
GetChildControlsId(control, controlType);
}
else
{
if (controlType == null || controlType.IsAssignableFrom(control.GetType()))
{
///checking if control already existing in the collection
if (control.ID != null)
{
bool FoundControl = controlHt.ContainsKey(control.ID);
if (!FoundControl)
{
switch (control.GetType().ToString())
{
case "System.Web.UI.WebControls.TextBox":
TextBox txt = (TextBox)control;
controlTypeName = txt.ID;
controlTypeName = controlTypeName.Substring(3);
controlTypeValue = txt.Text;
if (dictControlValParam.ContainsKey(controlTypeName) == false)
{
dictControlValParam.Add(controlTypeName, controlTypeValue);
}
else
{
dictControlValParam.Remove(controlTypeName);
dictControlValParam.Add(controlTypeName, controlTypeValue);
}
break;
case "System.Web.UI.WebControls.CheckBox":
CheckBox chk = (CheckBox)control;
controlTypeName = chk.ID;
controlTypeName = controlTypeName.Substring(3);
controlTypeValue = chk.Checked.ToString();
if (dictControlValParam.ContainsKey(controlTypeName) == false)
{
dictControlValParam.Add(controlTypeName, controlTypeValue);
}
else
{
dictControlValParam.Remove(controlTypeName);
dictControlValParam.Add(controlTypeName, controlTypeValue);
}
break;
case "System.Web.UI.WebControls.DropDownList":
DropDownList ddl = (DropDownList)control;
controlTypeName = ddl.ID;
controlTypeName = controlTypeName.Substring(3);
controlTypeValue = ddl.SelectedValue.ToString();
if (dictControlValParam.ContainsKey(controlTypeName) == false)
{
dictControlValParam.Add(controlTypeName, controlTypeValue);
}
else
{
dictControlValParam.Remove(controlTypeName);
dictControlValParam.Add(controlTypeName, controlTypeValue);
}
break;
case "System.Web.UI.WebControls.RadioButtonList":
RadioButtonList rbl = (RadioButtonList)control;
controlTypeName = rbl.ID;
controlTypeName = controlTypeName.Substring(3);
controlTypeValue = rbl.SelectedValue.ToString();
if (dictControlValParam.ContainsKey(controlTypeName) == false)
{
dictControlValParam.Add(controlTypeName, controlTypeValue);
}
else
{
dictControlValParam.Remove(controlTypeName);
dictControlValParam.Add(controlTypeName, controlTypeValue);
}
break;
case "System.Web.UI.WebControls.HiddenField":
HiddenField hdn = (HiddenField)control;
controlTypeName = hdn.ID;
controlTypeName = controlTypeName.Substring(3);
controlTypeValue = hdn.Value;
if (dictControlValParam.ContainsKey(controlTypeName) == false)
{
dictControlValParam.Add(controlTypeName, controlTypeValue);
}
else
{
dictControlValParam.Remove(controlTypeName);
dictControlValParam.Add(controlTypeName, controlTypeValue);
}
break;
}
}
}
}
}
}
catch (Exception)
{
throw;
}
}

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

UseMnemonic is being affected by FlatStyle on a Button

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";

Categories

Resources