2011-05-26 101 views
0
if (textBox1.Text == "Alabama" || textBox1.Text == "alabama") 
     { 
      StateTax = 0.0570; 
     }if (textBox1.Text == "Alaska" || textBox1.Text == "alaska") 
     { 
      StateTax = 0.1167; 
     }if (textBox1.Text == "Arizona" || textBox1.Text == "arizona") 
     { 
      StateTax = 0.0592; 
     }if (textBox1.Text == "Arkansas" || textBox1.Text == "arkansas") 
     { 
      StateTax = 0.0835; 
     }if (textBox1.Text == "California" || textBox1.Text == "california") 
     { 
      StateTax = 0.0739; 
     }if (textBox1.Text == "Colorado" || textBox1.Text == "colorado") 
     { 
      StateTax = 0.0272; 
     }if (textBox1.Text == "Connecticut" || textBox1.Text == "connecticut") 
     { 
      StateTax = 0.0654; 
     }if (textBox1.Text == "Deleware" || textBox1.Text == "deleware") 
     { 
      StateTax = 0.0825; 
     }if (textBox1.Text == "Florida" || textBox1.Text == "flordia") 
     { 
      StateTax = 0.0503; 
     }if (textBox1.Text == "Georgia" || textBox1.Text == "georgia") 
     { 
      StateTax = 0.0562; 
     }if (textBox1.Text == "Hawaii" || textBox1.Text == "hawaii") 
     { 
      StateTax = 0.0985; 
     }if (textBox1.Text == "Idaho" || textBox1.Text == "idaho") 
     { 
      StateTax = 0.0736; 
     }if (textBox1.Text == "Illinois" || textBox1.Text == "illinois") 
     { 
      StateTax = 0.0562; 
     }if (textBox1.Text == "Indiana" || textBox1.Text == "indiana") 
     { 
      StateTax = 0.0648; 
     }if (textBox1.Text == "Iowa" || textBox1.Text == "iowa") 
     { 
      StateTax = 0.0591; 
     }if (textBox1.Text == "Kansas" || textBox1.Text == "kansas") 
     { 
      StateTax = 0.0654; 
     }if (textBox1.Text == "Kentucky" || textBox1.Text == "kentucky") 
     { 
      StateTax = 0.0734; 
     }if (textBox1.Text == "Louisiana" || textBox1.Text == "louisiana") 
     { 
      StateTax = 0.0685; 
     }if (textBox1.Text == "Maine" || textBox1.Text == "maine") 
     { 
      StateTax = 0.0770; 
     }if (textBox1.Text == "Maryland" || textBox1.Text == "maryland") 
     { 
      StateTax = 0.0559; 
     }if (textBox1.Text == "Massachusetts" || textBox1.Text == "massachusetts") 
     { 
      StateTax = 0.0630; 
     }if (textBox1.Text == "Michigan" || textBox1.Text == "Michigan") 
     { 
      StateTax = 0.0672; 
     }if (textBox1.Text == "Minnesota" || textBox1.Text == "minnesota") 
     { 
      StateTax = 0.0802; 
     }if (textBox1.Text == "Mississippi" || textBox1.Text == "mississippi") 
     { 
      StateTax = 0.0740; 
     }if (textBox1.Text == "Missouri" || textBox1.Text == "missouri") 
     { 
      StateTax = 0.0517; 
     }if (textBox1.Text == "Montana" || textBox1.Text == "montana") 
     { 
      StateTax = 0.0708; 
     }if (textBox1.Text == "Nebraska" || textBox1.Text == "nebraska") 
     { 
      StateTax = 0.0610; 
     }if (textBox1.Text == "Nevada" || textBox1.Text == "nevada") 
     { 
      StateTax = 0.0612; 
     }if (textBox1.Text == "New Hampshire" || textBox1.Text == "new hampshire") 
     { 
      StateTax = 0.0387; 
     }if (textBox1.Text == "New Jersey" || textBox1.Text == "new jersey") 
     { 
      StateTax = 0.0661; 
     }if (textBox1.Text == "New Mexico" || textBox1.Text == "new mexico") 
     { 
      StateTax = 0.0826; 
     }if (textBox1.Text == "New York" || textBox1.Text == "new york") 
     { 
      StateTax = 0.0676; 
     }if (textBox1.Text == "North Carolina" || textBox1.Text == "north carolina") 
     { 
      StateTax = 0.0726; 
     }if (textBox1.Text == "North Dakota" || textBox1.Text == "north dakota") 
     { 
      StateTax = 0.0711; 
     }if (textBox1.Text == "Ohio" || textBox1.Text == "Ohio") 
     { 
      StateTax = 0.0609; 
     }if (textBox1.Text == "Oklahoma" || textBox1.Text == "oklahoma") 
     { 
      StateTax = 0.0621; 
     }if (textBox1.Text == "Oregon" || textBox1.Text == "oregon") 
     { 
      StateTax = 0.0576; 
     }if (textBox1.Text == "Pennsylvania" || textBox1.Text == "pennsylvania") 
     { 
      StateTax = 0.0617; 
     }if (textBox1.Text == "Rhode Island" || textBox1.Text == "rhode island") 
     { 
      StateTax = 0.0640; 
     }if (textBox1.Text == "South Carolina" || textBox1.Text == "south carolina") 
     { 
      StateTax = 0.0619; 
     }if (textBox1.Text == "South Dakota" || textBox1.Text == "south dakota") 
     { 
      StateTax = 0.0423; 
     }if (textBox1.Text == "Tennessee" || textBox1.Text == "tennessee") 
     { 
      StateTax = 0.0539; 
     }if (textBox1.Text == "Texas" || textBox1.Text == "texas") 
     { 
      StateTax = 0.0438; 
     }if (textBox1.Text == "Utah" || textBox1.Text == "utah") 
     { 
      StateTax = 0.0728; 
     }if (textBox1.Text == "Vermont" || textBox1.Text == "vermont") 
     { 
      StateTax = 0.1060; 
     }if (textBox1.Text == "Virginia" || textBox1.Text == "virginia") 
     { 
      StateTax = 0.0563; 
     }if (textBox1.Text == "Washington" || textBox1.Text == "washington") 
     { 
      StateTax = 0.0648; 
     }if (textBox1.Text == "West Virginia" || textBox1.Text == "west virginia") 
     { 
      StateTax = 0.0834; 
     }if (textBox1.Text == "Wisconsin" || textBox1.Text == "wisconsin") 
     { 
      StateTax = 0.0693; 
     }if (textBox1.Text == "Wyoming" || textBox1.Text == "wyoming") 
     { 
      StateTax = 0.0778; 
     } if (textBox1.Text == "" || textBox1.Text == "") 
     { 
      string messageBoxText = "Their is No State Entered into the first pages State Textbox"; 
      MessageBox.Show(messageBoxText); 
      return; 

     } 

所以我真的只需要分配的值能够被输入的名称分配,我现在正在工作,我只是需要它,以便只有那些单词可以进入,只是没有更多。 如果你能帮助我,那会很棒,我之前发布过一次,但我意识到我已经把所有东西挤在一起。我需要一个文本框的帮助

+0

我想你需要澄清你的意思是“只有这些词可以输入” - 因为用户可以在文本框中输入任何他们想要的东西,并且没有什么可以阻止它们。在这种情况下,您可能想要重新说明您的真正含义。 – 2011-05-26 03:06:25

+0

我的第一步是简化代码。不要苛刻,但这是在tdwtf上找到的那种代码。我建议使用字典和字符串大小写标准化作为开始。现在,就“问题”而言,考虑限制性控制类型,如ListBox或ComboBox。 – 2011-05-26 03:08:38

+0

为什么不能有一个listbox/combobox允许用户选择状态?如果列表框/组合框绑定到包含名称和税款的州级类别,则可以根据所选项目确定税额。 – 2011-05-26 03:10:15

回答

0

您可以尝试的的InputBox:

Dim state as String 
state = InputBox("What is your name?","User Input") 
textBox1.Test = state 
2

我会建议一个列表框,然后你决定用户可以选择什么状态的名字和你没有检查,如果状态名称是有效的。将一个ListBox控件添加到您的窗体并填充它。

listBox1.Items.Add("Alabama"); 
listBox1.Items.Add("Arizona"); 
listBox1.Items.Add("Arkansas"); 

然后做这样的事情:

Double StateTax = 0; 

if (listBox1.SelectedItem != null) 
{ 
    switch (listBox1.SelectedItem.ToString().ToLower()) 
      { 
       case "alabama": StateTax = 0.0570; break; 
       case "arizona": StateTax = 0.0570; break; 
       case "arkansas": StateTax = 0.0570; break; 
       /* etc... */ 

       default: break; 
      } 
     } 
2

做这样的:

绑定

string allStateTaxes = "Alabama+0.0570|Alaska+0.1167| ... |Wisconsin+0.0778"; 

Dictionary<string,string> stateTaxes = new Dictionary<string,string>(); 
string[] stateTaxSplit = null; 

foreach(string stateTax in stateTaxes.Split('|')) 
{ 
    stateTaxSplit = stateTax.Split('+'); 
    stateTaxes.Add(stateTaxSplit[0], stateTaxSplit[1]); 
} 

listBox1.DataSource = new BindingSource(stateTaxes, null); 
listBox1.DisplayMember = "Key"; 
listBox1.ValueMember = "Value"; 

搜索

string strTax = null; 
float tax; 

if(stateTaxes.TryGetValue(state, out strTax)) { 
    // state is found; strTax is set 
    tax = float.Parse(strTax); 
} else { 
    // state not found; strTax is null 
} 
+0

我不喜欢字符串的东西 - 字符串打字很好 - 但是显示如何数据绑定的+1。 – 2011-05-26 06:51:05

+0

@pst:实际上我的目标是将字符串作为比其他本地进程存储方法缩短代码的主要优势:如果您查看所选格式a)可读b)短c)可维护d)常量(含义可以用作一种常量变化类型的东西)。我认为这可能是最好的方式去,如果你提出绑定一个ddl或列表框与这个特定的数据(所有州和税收它)编辑:这也是好的表现,不是它很重要,但对于记录 – selion 2011-05-26 06:58:16

+0

我的意思是“严格”打字。选择字符串的原因是,如果需要,它可以稍后进行类型化扩展。我可能会使用一个'Dictionary ',它可能以后扩展为'Dictionary '或类似的 - 我完全同意将它完全保存在一个地方! – 2011-05-26 07:04:45