Advertisement
Guest User

Untitled

a guest
May 26th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. private void button3_Click(object sender, EventArgs e)
  2. {
  3. if (TabelkaGUI.pobierzLiczbeStanow(dataGridView1) == 0)
  4. {
  5. MessageBox.Show("Najpierw dodaj stany");
  6. return;
  7. }
  8.  
  9. string input = String.Empty;
  10. while (String.IsNullOrEmpty(input))
  11. {
  12. try
  13. {
  14. input = Interaction.InputBox("Dozwolone symbole a-z0-9 oraz $ (znak pusty)", "Podaj znak", String.Empty, (Int32.Parse(screenWidth) / 2) - 150, (Int32.Parse(screenHeight) / 2) - 100);
  15. if (input == String.Empty) break;
  16. char c = Char.Parse(input);
  17. if ((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '$')
  18. {
  19. if (!TabelkaGUI.czyIstniejeRzad(dataGridView1, input)) TabelkaGUI.dodajRzad(dataGridView1, input);
  20. else MessageBox.Show("Taki znak już istnieje!");
  21. }
  22. else
  23. {
  24. MessageBox.Show("Niedozwolony symbol!");
  25. }
  26. }
  27. catch (FormatException)
  28. {
  29. MessageBox.Show("Możesz podać pojedyńczy symbol!");
  30. }
  31. }
  32. }
  33.  
  34. private void button4_Click(object sender, EventArgs e)
  35. {
  36. TabelkaGUI.dodajKolumne(dataGridView1, "q" + (TabelkaGUI.pobierzLiczbeStanow(dataGridView1)).ToString());
  37. }
  38.  
  39. private void button5_Click(object sender, EventArgs e)
  40. {
  41. try
  42. {
  43. TabelkaGUI.usunOstatniRzad(dataGridView1);
  44. }
  45. catch (Exception ex)
  46. {
  47. MessageBox.Show(ex.Message);
  48. }
  49. }
  50.  
  51. private void button6_Click(object sender, EventArgs e)
  52. {
  53. try
  54. {
  55. TabelkaGUI.usunOstatniaKolumne(dataGridView1);
  56. }
  57. catch (Exception ex)
  58. {
  59. MessageBox.Show(ex.Message);
  60. }
  61. }
  62.  
  63.  
  64. =============================================================================
  65.  
  66. using System;
  67. using System.Collections.Generic;
  68. using System.Linq;
  69. using System.Text;
  70. using System.Threading.Tasks;
  71. using System.Windows.Forms;
  72. using System.Diagnostics;
  73.  
  74. namespace AutomatZeStosem
  75. {
  76. static class TabelkaGUI
  77. {
  78. public static void dodajRzad(DataGridView tabelka, string nazwaRzedu)
  79. {
  80. if (tabelka.ColumnCount == 0) throw new Exception("Najpierw dodaj kolumny!");
  81. DataGridViewRow rzad = new DataGridViewRow();
  82. rzad.HeaderCell.Value = nazwaRzedu;
  83. tabelka.Rows.Add(rzad);
  84. }
  85.  
  86. public static void dodajKolumne(DataGridView tabelka, string nazwaKolumny)
  87. {
  88. tabelka.Columns.Add(nazwaKolumny, nazwaKolumny);
  89. }
  90.  
  91. public static void usunOstatniRzad(DataGridView tabelka)
  92. {
  93. int liczbaRzedow = tabelka.Rows.Count;
  94. liczbaRzedow -= 2;
  95. if (liczbaRzedow < 0) throw new Exception("Brak rzędów!");
  96. tabelka.Rows.RemoveAt(tabelka.Rows[liczbaRzedow].Index);
  97. }
  98.  
  99. public static void usunOstatniaKolumne(DataGridView tabelka)
  100. {
  101. int liczbaKolumn = tabelka.Columns.Count;
  102. liczbaKolumn -= 1;
  103. if (liczbaKolumn < 0) throw new Exception("Brak kolumn!");
  104. tabelka.Columns.RemoveAt(tabelka.Columns[liczbaKolumn].Index);
  105. }
  106.  
  107. public static void zaznaczKomorke(DataGridView tabelka, int x, int y)
  108. {
  109. try
  110. {
  111. tabelka.CurrentCell = tabelka[x, y];
  112. }
  113. catch (ArgumentOutOfRangeException e)
  114. {
  115. throw new ArgumentOutOfRangeException("x = " + x.ToString() + ", y = " + y.ToString(), e);
  116. }
  117. }
  118.  
  119. public static void wpiszTekst(DataGridView tabelka, int x, int y, string tekst)
  120. {
  121. try
  122. {
  123. tabelka[x, y].Value = tekst;
  124. }
  125. catch (ArgumentOutOfRangeException e)
  126. {
  127. throw new ArgumentOutOfRangeException("x = " + x.ToString() + ", y = " + y.ToString(), e);
  128. }
  129. }
  130.  
  131. public static bool czyIstniejeRzad(DataGridView tabelka, string nazwaRzedu)
  132. {
  133. foreach (DataGridViewRow rzad in tabelka.Rows)
  134. {
  135. if (rzad.HeaderCell.Value == null) continue;
  136. if (rzad.HeaderCell.Value.ToString() == nazwaRzedu) return true;
  137. }
  138. return false;
  139. }
  140.  
  141. public static bool czyIstniejeKolumna(DataGridView tabelka, string nazwaKolumny)
  142. {
  143. foreach (DataGridViewRow rzad in tabelka.Rows)
  144. {
  145. if (rzad.HeaderCell.Value.ToString() == nazwaKolumny) return true;
  146. }
  147. return false;
  148. }
  149.  
  150. public static int pobierzLiczbeStanow(DataGridView tabelka)
  151. {
  152. return tabelka.Columns.Count;
  153. }
  154. }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement