Advertisement
Guest User

WPF

a guest
Nov 7th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using System.IO;
  16. using Microsoft.Win32;
  17.  
  18. namespace WPF2
  19. {
  20. /// <summary>
  21. /// Interaction logic for MainWindow.xaml
  22. /// CRUD
  23. /// FILES
  24. /// FILES WITH DATABASE
  25. /// CLOSE HIDE MULTIPLE WINDOWS
  26. /// LOGIN
  27. /// </summary>
  28. public partial class MainWindow : Window
  29. {
  30. public MainWindow()
  31. {
  32. InitializeComponent();
  33. }
  34.  
  35. WPFCRUD1Entities1 context = new WPFCRUD1Entities1();
  36. User usr = new User();
  37.  
  38. //Save
  39. private void btnSave_Click(object sender, RoutedEventArgs e)
  40. {
  41. usr.Username = txtUser.Text;
  42. usr.Pass = txtPass.Password;
  43. context.Users.Add(usr);
  44. context.SaveChanges();
  45. }
  46.  
  47. //Update
  48. private void btnUpdate_Click(object sender, RoutedEventArgs e)
  49. {
  50. int id = int.Parse(txtSearch.Text);
  51. bool isFound = context.Users.Where(x => x.ID == id).Any();
  52. if (isFound)
  53. {
  54. usr = context.Users.Where(x => x.ID == id).FirstOrDefault();
  55. usr.Pass = txtPass.Password;
  56. usr.Username = txtUser.Text;
  57. context.SaveChanges();
  58. MessageBox.Show("Data Updated");
  59. }
  60. else
  61. {
  62. MessageBox.Show("Update Failed");
  63. }
  64. }
  65.  
  66. private void btnDelete_Click(object sender, RoutedEventArgs e)
  67. {
  68. int id = int.Parse(txtSearch.Text);
  69. bool isFound = context.Users.Where(x => x.ID == id).Any();
  70. if (isFound)
  71. {
  72. usr = context.Users.Where(x => x.ID == id).FirstOrDefault();
  73. context.Users.Remove(usr);
  74. context.SaveChanges();
  75. MessageBox.Show("Record Deleted");
  76. }
  77. else
  78. {
  79. MessageBox.Show("No ID found");
  80. }
  81. }
  82.  
  83. private void btnSearch_Click(object sender, RoutedEventArgs e)
  84. {
  85. int id = int.Parse(txtSearch.Text);
  86. bool isFound = context.Users.Where(x => x.ID == id).Any();
  87. if (isFound)
  88. {
  89. usr = context.Users.Where(x => x.ID == id).FirstOrDefault();
  90. txtUser.Text = usr.Username;
  91. txtPass.Password = usr.Pass;
  92. }
  93. else
  94. MessageBox.Show("Record Not Found");
  95. }
  96.  
  97. private void ckShowGrid_Checked(object sender, RoutedEventArgs e)
  98. {
  99. var data = context.Users.ToList();
  100. dataGrid.ItemsSource = data;
  101. }
  102.  
  103. private void ckShowGrid_Unchecked(object sender, RoutedEventArgs e)
  104. {
  105. dataGrid.ItemsSource = null;
  106. }
  107.  
  108. private void btnOpenFile_Click(object sender, RoutedEventArgs e)
  109. {
  110. System.Windows.Forms.OpenFileDialog open = new System.Windows.Forms.OpenFileDialog();
  111.  
  112. open.Title = "Open File";
  113. open.Filter = "Text Files (*.txt)|*.txt| All Files (*.*)|*.*";
  114.  
  115. if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  116. {
  117. StreamReader read = new StreamReader(File.OpenRead(open.FileName));
  118. txtUpload.Text = read.ReadToEnd();
  119. read.Dispose();
  120. }
  121. }
  122.  
  123. private void btnSaveFile_Click(object sender, RoutedEventArgs e)
  124. {
  125. System.Windows.Forms.SaveFileDialog save = new System.Windows.Forms.SaveFileDialog();
  126.  
  127. save.Title = "Save File";
  128. save.Filter = "Text Files (*.txt)|*.txt| All Files (*.*)|*.*";
  129.  
  130. if (save.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  131. {
  132. StreamWriter write = new StreamWriter(File.Create(save.FileName));
  133.  
  134. write.Write(txtUpload.Text);
  135. write.Dispose();
  136. }
  137. }
  138.  
  139. private void btnDbFile_Click(object sender, RoutedEventArgs e)
  140. {
  141. DBFILE win2 = new DBFILE();
  142. win2.Show();
  143. }
  144.  
  145. //POP-UP OPEN
  146. private void Show_Click(object sender, RoutedEventArgs e)
  147. {
  148. MyPopup.IsOpen = true;
  149. }
  150.  
  151. //POP-UP CLOSE / CLICKING OUTSIDE ITS DONE IN XAML WITH STAYSOPEN
  152. private void Hide_Click(object sender, RoutedEventArgs e)
  153. {
  154. MyPopup.IsOpen = false;
  155. }
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement