How do you save selected ComboBox items into a .txt file

Hi I'm am new to programming and I am trying to create a C# grade calculator using WPF for users to enter their grades and for it to remove 2 of their lowest grades and then tell them their overall grade, I have got that part working but I would like it to save the selected grade from the ComboBoxes into a txt file and maybe be able to load the txt file into the grade calculator again.

Here is the code I have so far

<Window x:Class="GradeCalculator.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Grade Calculator" Height="779.736" Width="952"> <Grid> <Grid.Background> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="White" Offset="1"/> <GradientStop Color="#FFAEF7F7"/> <GradientStop Color="#FFD0FAFA" Offset="0.49"/> </LinearGradientBrush> </Grid.Background> <Menu VerticalAlignment="Top" IsMainMenu="True" Margin="0,0,868,0"/> <Label Name="Title" Content="Grade Calculator" Margin="366,181,366,0" VerticalAlignment="Top" FontSize="25" HorizontalAlignment="Center" FontWeight="Bold" Width="212"/> <TextBlock Name="Information" HorizontalAlignment="Center" Margin="324,223,324,0" TextWrapping="Wrap" Text="Please enter your grades or predicted grades into the drop down menus below" VerticalAlignment="Top" Width="296" FontSize="16" TextAlignment="Center"/> <Label Name="Mandatory" Content="Mandatory Units" HorizontalAlignment="Left" Margin="156,273,0,0" VerticalAlignment="Top" Height="28" Width="122" FontSize="14" FontWeight="Bold"/> <TextBlock Name="CES" HorizontalAlignment="Left" Margin="12,306,0,0" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="16"><Run Text="Communication and Employability"/><LineBreak/><Run Text="Skills for IT"/></TextBlock> <TextBlock Name="CS" HorizontalAlignment="Left" Margin="12,354,0,0" TextWrapping="Wrap" Text="Computer Systems" VerticalAlignment="Top" FontSize="16"/> <TextBlock Name="IS" HorizontalAlignment="Left" Margin="12,380,0,0" TextWrapping="Wrap" Text="Information Systems" VerticalAlignment="Top" FontSize="16"/> <TextBlock Name="SDD" HorizontalAlignment="Left" Margin="12,406,0,0" TextWrapping="Wrap" Text="Software Design and Development" VerticalAlignment="Top" FontSize="16"/> <TextBlock Name="SAD" HorizontalAlignment="Left" Margin="12,432,0,0" TextWrapping="Wrap" Text="Systems Analysis and Design" VerticalAlignment="Top" FontSize="16"/> <TextBlock Name="EDP" HorizontalAlignment="Left" Margin="10,462,0,0" TextWrapping="Wrap" Text="Event Driven Programming" VerticalAlignment="Top" FontSize="16"/> <ComboBox Name="CESBox" HorizontalAlignment="Left" Margin="288,318,0,0" VerticalAlignment="Top" Width="120"> <ComboBoxItem Content="Pass" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Merit" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Distinction" HorizontalAlignment="Left" Width="118"/> </ComboBox> <ComboBox Name="CSBox" HorizontalAlignment="Left" Margin="288,354,0,0" VerticalAlignment="Top" Width="120"> <ComboBoxItem Content="Pass" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Merit" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Distinction" HorizontalAlignment="Left" Width="118"/> </ComboBox> <ComboBox Name="ISBox" HorizontalAlignment="Left" Margin="288,462,0,0" VerticalAlignment="Top" Width="120"> <ComboBoxItem Content="Pass" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Merit" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Distinction" HorizontalAlignment="Left" Width="118"/> </ComboBox> <ComboBox Name="SDDBox" HorizontalAlignment="Left" Margin="288,381,0,0" VerticalAlignment="Top" Width="120"> <ComboBoxItem Content="Pass" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Merit" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Distinction" HorizontalAlignment="Left" Width="118"/> </ComboBox> <ComboBox Name="SADBox" HorizontalAlignment="Left" Margin="288,408,0,0" VerticalAlignment="Top" Width="120"> <ComboBoxItem Content="Pass" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Merit" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Distinction" HorizontalAlignment="Left" Width="118"/> </ComboBox> <ComboBox Name="EDPBox" HorizontalAlignment="Left" Margin="288,435,0,0" VerticalAlignment="Top" Width="120"> <ComboBoxItem Content="Pass" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Merit" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Distinction" HorizontalAlignment="Left" Width="118"/> </ComboBox> <Label Name="Optional" Content="Optional Units" HorizontalAlignment="Left" Margin="644,273,0,0" VerticalAlignment="Top" Height="28" Width="118" FontSize="14" FontWeight="Bold"/> <TextBlock Name="MN" HorizontalAlignment="Left" Margin="480,301,0,0" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="16"><Run Text="Managing Network"/><Run Text="s"/></TextBlock> <TextBlock Name="CN" HorizontalAlignment="Left" Margin="480,327,0,0" TextWrapping="Wrap" Text="Computer Networks" VerticalAlignment="Top" FontSize="16" Height="22"/> <TextBlock Name="CT" HorizontalAlignment="Left" Margin="480,354,0,0" TextWrapping="Wrap" Text="Communication Technologies" VerticalAlignment="Top" FontSize="16" Height="22"/> <TextBlock Name="ITTS" HorizontalAlignment="Left" Margin="480,381,0,0" TextWrapping="Wrap" Text="IT Technical Support" VerticalAlignment="Top" FontSize="16" Height="22"/> <TextBlock Name="ITTSTR" HorizontalAlignment="Left" Margin="480,408,0,0" TextWrapping="Wrap" Text="IT Systems and Troubleshooting and Repair" VerticalAlignment="Top" FontSize="16" Height="22"/> <TextBlock Name="PPIT" HorizontalAlignment="Left" Margin="480,435,0,0" TextWrapping="Wrap" Text="Project Planning with IT" VerticalAlignment="Top" FontSize="16" Height="22"/> <TextBlock Name="DB" HorizontalAlignment="Left" Margin="480,462,0,0" TextWrapping="Wrap" Text="Database Design" VerticalAlignment="Top" FontSize="16" Height="22"/> <TextBlock Name="CSCWP" HorizontalAlignment="Left" Margin="480,489,0,0" TextWrapping="Wrap" Text="Client Side Customisation of Web Pages" VerticalAlignment="Top" FontSize="16" Height="22"/> <TextBlock Name="DCG" HorizontalAlignment="Left" Margin="480,516,0,0" TextWrapping="Wrap" Text="Developing Computer Games" VerticalAlignment="Top" FontSize="16" Height="22"/> <TextBlock Name="WSS" HorizontalAlignment="Left" Margin="480,543,0,0" TextWrapping="Wrap" Text="Web Server Scripting" VerticalAlignment="Top" FontSize="16" Height="22"/> <TextBlock Name="WP" HorizontalAlignment="Left" Margin="480,570,0,0" TextWrapping="Wrap" Text="Website Production" VerticalAlignment="Top" FontSize="16" Height="22"/> <TextBlock Name="DG" HorizontalAlignment="Left" Margin="480,597,0,0" TextWrapping="Wrap" Text="Digital Graphics" VerticalAlignment="Top" FontSize="16" Height="22"/> <TextBlock Name="NSS" HorizontalAlignment="Left" Margin="480,624,0,0" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="16" Height="22"><Run Text="Network Systems Secur"/><Run Text="i"/><Run Text="ty"/></TextBlock> <TextBlock Name="WAIM" HorizontalAlignment="Left" Margin="480,651,0,0" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="16" Height="22"><Run Text="Web Animation for Interactive Med"/><Run Text="i"/><Run Text="a"/></TextBlock> <ComboBox Name="MNBox" HorizontalAlignment="Left" Margin="798,300,0,0" VerticalAlignment="Top" Width="120"> <ComboBoxItem Content="Fail" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Pass" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Merit" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Distinction" HorizontalAlignment="Left" Width="118"/> </ComboBox> <ComboBox Name="CNBox" HorizontalAlignment="Left" Margin="798,327,0,0" VerticalAlignment="Top" Width="120"> <ComboBoxItem Content="Fail" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Pass" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Merit" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Distinction" HorizontalAlignment="Left" Width="118"/> </ComboBox> <ComboBox Name="CTBox" HorizontalAlignment="Left" Margin="798,354,0,0" VerticalAlignment="Top" Width="120"> <ComboBoxItem Content="Fail" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Pass" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Merit" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Distinction" HorizontalAlignment="Left" Width="118"/> </ComboBox> <ComboBox Name="ITTSBox" HorizontalAlignment="Left" Margin="798,381,0,0" VerticalAlignment="Top" Width="120"> <ComboBoxItem Content="Fail" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Pass" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Merit" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Distinction" HorizontalAlignment="Left" Width="118"/> </ComboBox> <ComboBox Name="ITTSTRBox" HorizontalAlignment="Left" Margin="798,408,0,0" VerticalAlignment="Top" Width="120"> <ComboBoxItem Content="Fail" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Pass" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Merit" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Distinction" HorizontalAlignment="Left" Width="118"/> </ComboBox> <ComboBox Name="PPITBox" HorizontalAlignment="Left" Margin="798,435,0,0" VerticalAlignment="Top" Width="120"> <ComboBoxItem Content="Fail" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Pass" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Merit" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Distinction" HorizontalAlignment="Left" Width="118"/> </ComboBox> <ComboBox Name="DBBox" HorizontalAlignment="Left" Margin="798,462,0,0" VerticalAlignment="Top" Width="120"> <ComboBoxItem Content="Fail" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Pass" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Merit" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Distinction" HorizontalAlignment="Left" Width="118"/> </ComboBox> <ComboBox Name="CSCWPBox" HorizontalAlignment="Left" Margin="798,489,0,0" VerticalAlignment="Top" Width="120"> <ComboBoxItem Content="Fail" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Pass" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Merit" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Distinction" HorizontalAlignment="Left" Width="118"/> </ComboBox> <ComboBox Name="DCGBox" HorizontalAlignment="Left" Margin="798,516,0,0" VerticalAlignment="Top" Width="120"> <ComboBoxItem Content="Fail" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Pass" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Merit" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Distinction" HorizontalAlignment="Left" Width="118"/> </ComboBox> <ComboBox Name="WSSBox" HorizontalAlignment="Left" Margin="798,543,0,0" VerticalAlignment="Top" Width="120"> <ComboBoxItem Content="Fail" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Pass" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Merit" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Distinction" HorizontalAlignment="Left" Width="118"/> </ComboBox> <ComboBox Name="WPBox" HorizontalAlignment="Left" Margin="798,570,0,0" VerticalAlignment="Top" Width="120"> <ComboBoxItem Content="Fail" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Pass" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Merit" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Distinction" HorizontalAlignment="Left" Width="118"/> </ComboBox> <ComboBox Name="DGBox" HorizontalAlignment="Left" Margin="798,597,0,0" VerticalAlignment="Top" Width="120"> <ComboBoxItem Content="Fail" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Pass" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Merit" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Distinction" HorizontalAlignment="Left" Width="118"/> </ComboBox> <ComboBox Name="NSSBox" HorizontalAlignment="Left" Margin="798,624,0,0" VerticalAlignment="Top" Width="120"> <ComboBoxItem Content="Fail" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Pass" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Merit" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Distinction" HorizontalAlignment="Left" Width="118"/> </ComboBox> <ComboBox Name="WAIMBox" HorizontalAlignment="Left" Margin="798,651,0,0" VerticalAlignment="Top" Width="120"> <ComboBoxItem Content="Fail" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Pass" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Merit" HorizontalAlignment="Left" Width="118"/> <ComboBoxItem Content="Distinction" HorizontalAlignment="Left" Width="118"/> </ComboBox> <Button Name="SubButton" Content="Submit" HorizontalAlignment="Left" Margin="396,686,0,0" VerticalAlignment="Top" Click="Button_Click" Width="130" Height="38" FontSize="18"/> <Image Margin="324,0,298,541" Source="BSDC.png" Stretch="Fill"/> <Label Name="TotalGrade" Content="" HorizontalAlignment="Left" Margin="104,597,0,0" VerticalAlignment="Top" Height="49" Width="225" FontSize="30"/> <Label Name="TotalScore" Content="" HorizontalAlignment="Left" Margin="104,534,0,0" VerticalAlignment="Top" Height="49" Width="225" FontSize="30"/> <Button Name="SaveButton" Content="Save Grades" HorizontalAlignment="Left" Margin="12,10,0,0" VerticalAlignment="Top" Click="SaveButt_Click" Width="75"/> </Grid>

public MainWindow() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { int total = 0; total += GetGrade(CESBox); total += GetGrade(CSBox); total += GetGrade(ISBox); total += GetGrade(SDDBox); total += GetGrade(SADBox); total += GetGrade(EDPBox); total += GetGrade(MNBox); total += GetGrade(CNBox); total += GetGrade(CTBox); total += GetGrade(ITTSBox); total += GetGrade(ITTSTRBox); total += GetGrade(PPITBox); total += GetGrade(DBBox); total += GetGrade(CSCWPBox); total += GetGrade(DCGBox); total += GetGrade(WSSBox); total += GetGrade(WPBox); total += GetGrade(DGBox); total += GetGrade(NSSBox); total += GetGrade(WAIMBox); total -= lowGrade1; total -= lowGrade2; if (total >= 1300 && total <= 1339) { grade = "MPP"; } if (total >= 1340 && total <= 1379) { grade = "MMP"; } if (total >= 1380 && total <= 1419) { grade = "MMM"; } if (total >= 1420 && total <= 1459) { grade = "DMM"; } if (total >= 1460 && total <= 1499) { grade = "DDM"; } if (total >= 1500 && total <= 1529) { grade = "DDD"; } if (total >= 1530 && total <= 1559) { grade = "D*DD"; } if (total >= 1560 && total <= 1589) { grade = "D*D*D"; } if (total >= 1590) { grade = "D*D*D*"; } TotalScore.Content = "Score = " + total; TotalGrade.Content = "Grade = " + grade; } public int GetGrade(ComboBox cbContent) { int score = 0; ComboBoxItem cbContentSelectedItem = cbContent.SelectedItem as ComboBoxItem; if (cbContentSelectedItem != null) { if ((string)cbContentSelectedItem.Content == "Fail") { if (lowGrade1 > 0) { lowGrade1 = 0; } else if (lowGrade2 > 0) { lowGrade2 = 0; } score = 0; } if ((string)cbContentSelectedItem.Content == "Pass") { if (lowGrade1 > 70) { lowGrade1 = 70; } else if (lowGrade2 > 70) { lowGrade2 = 70; } score = 70; } if ((string)cbContentSelectedItem.Content == "Merit") { if (lowGrade1 > 80) { lowGrade1 = 80; } else if (lowGrade2 > 80) { lowGrade2 = 80; } score = 80; } if ((string)cbContentSelectedItem.Content == "Distinction") { if (lowGrade1 > 90) { lowGrade1 = 90; } else if (lowGrade2 > 90) { lowGrade2 = 90; } score = 90; } } return score; } private void SaveButt_Click(object sender, RoutedEventArgs e) { } }

}

-------------Problems Reply------------

If you have problem accessing C:\ you app have problem of permission.

Try creating a new folder "C:\Test" and try to write there. Or start visual studio as administrator. Also include the filename

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void SaveButt_Click(object sender, RoutedEventArgs e)
{
File.WriteAllText("C:\\file.txt", "Hello World!");
}
}

Also check this link for more info

Category:c# Views:2 Time:2018-12-02
Tags: wpf combobox

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.079 (s). 11 q(s)