I'm trying to create a WPF application for students' registration with a simple form containing Name, matriculation number, credits and so on. I have a Mainwindow.xaml
and a Student.cs
.
In my MainWindow.xaml
I have an advance button that should advance the level of the students, based on the credits (if the student has more than 120 credits, level should be advanced to "2")
This is the Student.cs with Advance() method
class Student
{
private int matric;
private int level;
private int credits;
public Student() { }
public int Matric
{
get { return matric; }
set
{
//there should be a range check for the
matric = value;
}
}
public int Level
{
get { return level; }
set { level = value; }
}
public int Credits
{
get { return credits; }
set { credits = value; }
}
//this is my problem:
public int Advance()
{
if (Credits >= 0 && Credits < 120)
{
return Level;
}
else if (credits >= 120)
{
return Level++;
}
else if (credits >= 240)
{
return Level++;
}
else if (Credits >= 360)
{
return Level++;
}
else
{
return 0;
}
}
}
MainWindow.xaml, just the part with the button and the textboxes
<TextBox x:Name="txtLevel" HorizontalAlignment="Left" Height="23" Margin="184,266,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="txtCredit" HorizontalAlignment="Left" Height="23" Margin="184,328,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
<Button x:Name="btnAdvance" Content="Advance" HorizontalAlignment="Left" Margin="324,267,0,0" VerticalAlignment="Top" Width="75" Click="btnAdvance_Click"/>
And where I'm trying to call the method MainWindow.xaml.cs
public partial class MainWindow : Window
{
Student student;
public MainWindow()
{
InitializeComponent();
}
private void btnSet_Click(object sender, RoutedEventArgs e)
{
student = new Student();
student.Credits = int.Parse(txtCredit.Text);
student.Level = int.Parse(txtLevel.Text);
}
private void btnAdvance_Click(object sender, RoutedEventArgs e)
{
student.Advance(); //this should be the call of the method
}
}
Of course it's not working... Can anyone help me?
EDIT This is what I have now, still not working
public void Advance()
{
if (Credits >= 0 && Credits < 120)
{
Level = 1;
}
else if (credits >= 120 && Level == 1)
{
Level = 2;
}
else if (credits >= 240 && Level == 2)
{
Level = 3;
}
else if (Credits >= 360 && Level == 3)
{
Level = 4;
}
else if (Level == 4)
{
MessageBox.Show("You can't advance more!");
}
else
{
MessageBox.Show("Advance is not possible!");
}
}
Really you should be doing this with binding, where you bind the Level textbox to the Level property of the student, implementing iNotifyPropertyChanged on your model. I would suggest you research binding and redesign it this way.
However, if you wish to proceed in your current design, here are changes I would suggest to achieve the behavior you are expecting:
1) In btnSet_Click
, remove this line: student.Level = int.Parse(txtLevel.Text);
Your Level should not be set by the TextBox; it should be set by the Advance method.
2) Your Advance method should look like follows:
public int Advance()
{
if (Credits >= 0 && Credits < 120)
{
level = 1;
}
else if (credits >= 120 && credits < 240)
{
level = 2;
}
else if (credits >= 240 && credits < 360)
{
level = 3;
}
else if (Credits >= 360)
{
if (level != 4)
{
level = 4;
}
else
{
MessageBox.Show("You can't advance more!");
}
}
else
{
MessageBox.Show("Advance is not possible!");
}
return level;
}
3) Add the IsReadOnly="True"
attribute to the Level Textbox, as it should not be settable from the interface.
<TextBox x:Name="txtLevel" IsReadOnly="True" HorizontalAlignment="Left" Height="23" Margin="184,266,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
4) Since you are not using binding, in your Advance_click, you will need to post the returned value back to the interface:
private void btnAdvance_Click(object sender, RoutedEventArgs e)
{
txtLevel.Text = student.Advance(); //this should be the call of the method
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments