2011-11-19 74 views
0

我正在为电影票务程序编写应用程序的一部分。验证文本框中的数字[VB2010]

基本上,我必须编写一个If语句来验证放入文本框中的年龄,这取决于您选中的radiobox。

因此,如果选中radiobox“PG”,文本框中的年龄必须等于或大于12.如果选中“Restricted”,则文本框必须等于或大于17。

任何人都可以帮我解决这个问题吗?我会很感激。

谢谢!

回答

0

我正在假设这是一个asp.net应用程序?

如果是这样,最好的方法是通过将其Autopostback属性设置为true来制作单选按钮和文本回发。在服务器端,检查组合并相应地采取行动。

使用Switch语句根据所选等级检查年龄。

您可以将它们包装在UpdatePanel中以防止可见的回传。

伪代码:

rating_changed() { 
    checkAge(); 
} 

txtAge_changed() { 
    checkAge(); 
} 

void checkAge() { 
    bool ageOkay = false; 
    int age = Convert.ToInt32(txtAge.Text); 

    switch (rating.SelectedItem.Value) { 
    case "G": 
     ageOkay = true; 
     break; 
    case "PG": 
     if (age >= 8) ageOkay = true; 
     break; 
    case "PG-13": 
     if (age >= 13) ageOkay = true; 
     break; 
    } 

if (ageOkay) { 
    //do next task 
} else { 
    //you're not old enough 
} 
}