Advertisement
VladTheBush

C# ListBox

Apr 4th, 2020
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.14 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace Programiranje_domaći_1 {
  12.     public partial class Form1 : Form {
  13.         public Form1() {
  14.             InitializeComponent();
  15.         }
  16.  
  17.         private void dodaj() {
  18.             if(textBox1.Text.Length > 0) {
  19.                 listBox1.Items.Add(textBox1.Text);
  20.                 textBox1.Text = "";
  21.             }
  22.         }
  23.  
  24.         private int suma(int[] brojevi, int n) {
  25.             int s = 0;
  26.  
  27.             for(int i = 0; i < n; i++)
  28.                 s += brojevi[i];
  29.  
  30.             return s;
  31.         }
  32.  
  33.         private float prosek(int[] brojevi, int n) {
  34.             return (float)suma(brojevi, n) / n;
  35.         }
  36.  
  37.         private int max(int[] brojevi, int n) {
  38.             int max = brojevi[0];
  39.  
  40.             for(int i = 0; i < n; i++)
  41.                 max = (brojevi[i] > max) ? brojevi[i] : max;
  42.  
  43.             return max;
  44.         }
  45.         private int min(int[] brojevi, int n) {
  46.             int min = brojevi[0];
  47.  
  48.             for(int i = 0; i < n; i++)
  49.                 min = (brojevi[i] < min) ? brojevi[i] : min;
  50.  
  51.             return min;
  52.         }
  53.  
  54.         //Unos dugme
  55.         private void button1_Click(object sender, EventArgs e) {
  56.             dodaj();
  57.         }
  58.  
  59.         //Klik entera unosi
  60.         private void textBox1_KeyDown(object sender, KeyEventArgs e) {
  61.             if(e.KeyCode == Keys.Enter)
  62.                 dodaj();
  63.         }
  64.  
  65.         //Obrisi izabrano dugme
  66.         private void button2_Click(object sender, EventArgs e) {
  67.             if(listBox1.SelectedIndex != -1)
  68.                 listBox1.Items.RemoveAt(listBox1.SelectedIndex);
  69.         }
  70.  
  71.         //Obrisi sve izabrane dugme
  72.         private void button3_Click(object sender, EventArgs e) {
  73.             int x = listBox1.SelectedItems.Count;
  74.  
  75.             for(int i = 0; i < x; i++)
  76.                 listBox1.Items.Remove(listBox1.SelectedItems[0]);
  77.         }
  78.  
  79.         //Ocisti dugme
  80.         private void button4_Click(object sender, EventArgs e) {
  81.             listBox1.Items.Clear();
  82.         }
  83.  
  84.         //Izracunaj dugme
  85.         private void button5_Click(object sender, EventArgs e) {
  86.             int[] izabrani = new int[listBox1.Items.Count];
  87.             int i = 0;
  88.  
  89.             //Parni radio button
  90.             if(radioButton6.Checked) {
  91.                 foreach(string sx in listBox1.Items) {
  92.                     int x = int.Parse(sx);
  93.                     if(x % 2 == 0)
  94.                         izabrani[i++] = x;
  95.                 }
  96.             }
  97.  
  98.             //Neparni radio button
  99.             if(radioButton7.Checked) {
  100.                 foreach(string sx in listBox1.Items) {
  101.                     int x = int.Parse(sx);
  102.                     if(x % 2 == 1)
  103.                         izabrani[i++] = x;
  104.                 }
  105.             }
  106.  
  107.             //Svi radio button
  108.             if(radioButton8.Checked) {
  109.                 foreach(string sx in listBox1.Items)
  110.                     izabrani[i++] = int.Parse(sx);
  111.             }
  112.  
  113.             //Izabrani radio button
  114.             if(radioButton9.Checked) {
  115.                 foreach(string sx in listBox1.SelectedItems)
  116.                     izabrani[i++] = int.Parse(sx);
  117.             }
  118.  
  119.             //Suma radio button
  120.             if(radioButton1.Checked) {
  121.                 label2.Text = "Rezultat je: " + suma(izabrani, i);
  122.             }
  123.  
  124.             //Prosek radio button
  125.             if(radioButton2.Checked) {
  126.                 label2.Text = "Rezultat je: " + prosek(izabrani, i);
  127.             }
  128.  
  129.             //Broj radio button
  130.             if(radioButton3.Checked) {
  131.                 label2.Text = "Rezultat je: " + i;
  132.             }
  133.  
  134.             //Maksimalni radio button
  135.             if(radioButton4.Checked) {
  136.                 label2.Text = "Rezultat je: " + max(izabrani, i);
  137.             }
  138.  
  139.             //Minimalni radio button
  140.             if(radioButton5.Checked) {
  141.                 label2.Text = "Rezultat je: " + min(izabrani, i);
  142.             }
  143.         }
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement