[c# WINFORM] problema con posizionamento dinamico dei controlli
Salve a tutti, ringrazio anticipatamente tutti coloro che perderanno del tempo cercando di aiutarmi.
Ho creato una classe per dare un impaginazione lineare ai form della mia applicazione e funziona fin ad ora per quel che mi serve ad eccezion fatta per i controlli di tipo Button che non vanno a capo.
la classe presuppone che vi sia un taborder del form lineare e senza buchi numerici
e viene richiamata dai vari form in evento load e resize cosi:
{
Funzioni_Form ff = new Funzioni_Form();
ff.PosizionaControlliForm(this);
}
vi posto il codice della classe Funzioni_Form:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Gestione_Reti_Gas
{
class Funzioni_Form
{
public void Abilta_Modifiche(System.Windows.Forms.Form F)
{
foreach (System.Windows.Forms.Control c in F.Controls)
{
if (c is TextBox)
{ c.Enabled = true; }
else if (c is ComboBox)
{ c.Enabled = true; }
else if (c is CheckBox)
{ c.Enabled = true; }
}
}
public void Disabilita_Modifiche(System.Windows.Forms.Form F)
{
foreach (Control c in F.Controls)
{
if (c is TextBox)
{ c.Enabled = false; }
else if (c is ComboBox)
{ c.Enabled = false; }
else if (c is CheckBox)
{ c.Enabled = false; }
}
}
public void Svuota_Dati(System.Windows.Forms.Form F)
{
foreach (Control c in F.Controls)
{
if (c is TextBox)
{ c.Text = ""; }
else if (c is ComboBox)
{ c.Text = ""; }
else if (c is CheckBox)
{ }
}
}
public void PosizionaControlliForm(System.Windows.Forms.Form F)
{
int i ;//Indice
int Interlinea=20;
//int CurrInt = 1;
int SpazioOrizStd =10;
int SpazioOrizCtoC=5;
int SpazioOrizLtoC=5;
int SpazioOrizLtoL=5;
int SpazioOrizCtoL=40;
int SpazioDalMargSx = 20;
int SpazioDalMargDx = 40;//Conteggiare la scrool
int SpazioDalMargUp = 20;
int MisuraOccupazione=0;
int LarghezzaForm = F.Size.Width;
int XStart=0;
int YStart=0;
System.Windows.Forms.Control CC; // Controllo Corrente
System.Windows.Forms.Control CP; // Controllo Precedente
System.Windows.Forms.Control CS; // Controllo Successivo
for (i = 0; i < F.Controls.Count; i++)
{
CC = CercaControllo(F, i);
if (i == 0)
{
PosizionaControllo(CC, SpazioDalMargSx, SpazioDalMargUp);
}
else
{
CP = CercaControllo(F, i - 1);
if (CP.Visible == true)
{
// Stabilisco il punto di partenza sull'asse X
if ((CP.GetType().Name == "Label") && ((CC.GetType().Name == "TextBox") || (CC.GetType().Name == "ComboBox")))
{
XStart = CP.Location.X + CP.Size.Width + SpazioOrizLtoC;
}
else if (((CC.GetType().Name == "TextBox") || (CC.GetType().Name == "ComboBox")) && CC.GetType().Name == "Label")
{
XStart = CP.Location.X + CP.Size.Width + SpazioOrizCtoL;
}
else if (CP.GetType().Name == "Label" && CC.GetType().Name == "Label")
{
XStart = CP.Location.X + CP.Size.Width + SpazioOrizLtoL;
}
else if (((CC.GetType().Name == "TextBox") || (CC.GetType().Name == "ComboBox")) && ((CC.GetType().Name == "TextBox") || (CC.GetType().Name == "ComboBox")))
{
XStart = CP.Location.X + CP.Size.Width + SpazioOrizCtoC;
}
else
{
XStart = CP.Location.X + CP.Size.Width + SpazioOrizStd;
}
// Voglio far andare a capo le etichette col relativo controllo
// label ComboBox
MisuraOccupazione = 0;
if (i + 1 < F.Controls.Count)
{
CS = CercaControllo(F, i + 1);
if (((CS.GetType().Name == "TextBox") || (CS.GetType().Name == "ComboBox")) && ((CC.GetType().Name == "Label")))
{
MisuraOccupazione = XStart + CC.Size.Width+ SpazioOrizLtoC + CS.Size.Width;
}
}
else
{
// Stabilisco il punto di partenza sull'asse Y
MisuraOccupazione = XStart + CC.Size.Width;
}
if (MisuraOccupazione < (LarghezzaForm - (SpazioDalMargDx + SpazioDalMargSx)))
{
YStart = CP.Location.Y;
}
else
{
YStart = CP.Location.Y + CP.Size.Height + Interlinea;
XStart = SpazioDalMargSx;
}
// Eccezione il menu che va sempre a capo e occupano tutta la riga
if (CC.GetType().Name == "MenuStrip")
{
YStart = CP.Location.Y + CP.Size.Height + Interlinea;
XStart = SpazioDalMargSx;
}
if (CP.GetType().Name == "MenuStrip")
{
YStart = CP.Location.Y + CP.Size.Height + Interlinea;
XStart = SpazioDalMargSx;
}
// Eccezione per le DataGridView che va sempre a capo e occupano tutta la riga
// e si ridisegna al variare delle dimensioni del form
if (CC.GetType().Name == "DataGridView")
{
YStart = CP.Location.Y + CP.Size.Height + Interlinea;
XStart = SpazioDalMargSx;
System.Drawing.Size m = new System.Drawing.Size();
m.Height = CC.Size.Height;
m.Width = F.Size.Width - (SpazioDalMargDx + SpazioOrizStd);
CC.Size = m;
CC.Refresh();
}
if (CP.GetType().Name == "DataGridView")
{
YStart = CP.Location.Y + CP.Size.Height + Interlinea;
XStart = SpazioDalMargSx;
}
// Eccezione per i GroupBox che occupano tutta la riga e vanno a capo
if (CC.GetType().Name == "GroupBox")
{
YStart = CP.Location.Y + CP.Size.Height + Interlinea;
XStart = SpazioDalMargSx;
}
if (CP.GetType().Name == "GroupBox")
{
YStart = CP.Location.Y + CP.Size.Height + Interlinea;
XStart = SpazioDalMargSx;
}
// Eccezione per le etichette che contengono la parola
// TITOLO che vanno a capo e OCCUPANO tutta la riga
if ((CC.GetType().Name == "Label") && (CC.Name.ToString().ToUpper().Contains("TITOLO")))
{
YStart = CP.Location.Y + CP.Size.Height + Interlinea;
XStart = SpazioDalMargSx;
}
if ((CP.GetType().Name == "Label") && (CP.Name.ToString().ToUpper().Contains("TITOLO")))
{
YStart = CP.Location.Y + CP.Size.Height + Interlinea;
XStart = SpazioDalMargSx;
}
// Eccezione per le etichette che contengono la parola
// TITOLETTO che vanno a capo e NON OCCUPANO tutta la riga
if ((CC.GetType().Name == "Label") && (CC.Name.ToString().ToUpper().Contains("TITOLETTO" )))
{
YStart = CP.Location.Y + CP.Size.Height + Interlinea;
XStart = SpazioDalMargSx;
}
}
else
{
XStart = CP.Location.X;
YStart = CP.Location.Y;
}
// Posiziono il controllo
PosizionaControllo(CC, XStart, YStart);
}
}
}
public Control CercaControllo(Form f, int indicetab)
{
foreach (Control C in f.Controls)
{
if (C.TabIndex == indicetab)
{
return C;
}
}
return null;
}
public void PosizionaControllo(System.Windows.Forms.Control C, int x, int y)
{
System.Drawing.Point np= new System.Drawing.Point(x,y);
C.Location = np;
}
}
}