Witam
Zaczynam tworzyć aplikacje coraz bardziej rozbudowane graficznie używając dużej ilości kontrolek (przycisków, pół tekstowych, zakładek, etykiet etc.). Zastanawiam się w jaki sposób "ogarnąć" dość dużą ilość kontrolek, niestety nie odnalazłem nic sensownego stąd moje pytanie.
Stworzyłem aplikację z dość prostym interfejsem: 3 pola tekstowe, 2 przyciski i jedna etykieta.
Pola tekstowe służą do wprowadzania liczb przez użytkownika, jeden przycisk służy do wykonywania obliczeń na tych liczbach, drugi do wyczyszczenia pół tekstowych a w etykiecie pojawia się wynik.
Kod mojego programu przedstawiam poniżej:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
InterfaceHandling.Buttons.btnCalculateClick(this.lblResult, this.txtNumber1,this.txtNumber2,this.txtNumber3);
}
private void btnClear_Click(object sender, EventArgs e)
{
InterfaceHandling.Buttons.btnClearClick(this.txtNumber1, this.txtNumber2, this.txtNumber3);
}
}
public static class InterfaceHandling //klasa obslugujaca interfejs aplikacji
{
public static class Buttons
{
public static void btnCalculateClick(Label plbl, TextBox pt1,TextBox pt2, TextBox pt3)
{
double a, b, c;
double result;
a = InterfaceHandling.TextBoxs.GetText(pt1);
b = InterfaceHandling.TextBoxs.GetText(pt2);
c = InterfaceHandling.TextBoxs.GetText(pt3);
result = Calculations.Calculate(a, b, c);
InterfaceHandling.Labels.SetText(plbl,result.ToString());
}
public static void btnClearClick(TextBox t1, TextBox t2, TextBox t3)
{
t1.Clear();
t2.Clear();
t3.Clear();
}
}
public static class TextBoxs
{
public static double GetText(TextBox txtBox)
{
double res;
res = Double.NaN;
try
{
res = Convert.ToDouble(txtBox.Text);
}
catch
{
MessageBox.Show("Wprowadź liczbę");
}
return res;
}
}
public static class Labels
{
public static void SetText(Label lbl, string text)
{
if (text != "NaN")
{
lbl.Text = "Wynik: "+text;
}
}
}
}
public static class Calculations
{
public static double Calculate(double pA, double pB, double pC)
{
double res = pA + pB + pC;
return res;
}
}
}
Klasa InterfaceHandling odpowiada za "obsługę" wszystkich kontrolek. Kontrolki posegregowałem wg typu. Klasa Calculations odpowiada za wykonanie obliczeń.
Moje wątpliwości:
- Czy moje podejście do obsługi interfejsu jest poprawne?
- Nie podoba mi się przekazywanie w parametrach metody obiektów określających kontrolki
InterfaceHandling.Buttons.btnCalculateClick(this.lblResult, this.txtNumber1,this.txtNumber2,this.txtNumber3);
Czy można to jakoś zrobić lepiej?
Dziękuje za ewentualne poprawki i sugestie.