Feedback

Steuerelemente zur Laufzeit hinzufügen

Sprache: C#

Ein recht simples Snippet. Im Beispiel wird eine Datei geladen, aus der dann Buttons hinzugefügt werden. Jede Zeile steht für einen Button. Eine Zeile ist so aufgebaut: [code]%text%|%breite%|%höhe%|%left(x)%|%top(y)%|%rot%;%grün%;%blau%[/code] Die Farbe in RGB kann auch durch "[default]" ersetzt werden. Somit entsteht ein Button mit aktivierten visuellen Effekten (und damit der System-Farbe 'Control'. Beispiel-Datei unter: https://www.dropbox.com/s/lhbodrae4djn5xu/Test-Form.txt [Edit:] Man hinterfrage bitte nicht den tieferen Sinn Buttons aus Dateien zu laden: Ist halt einfach ein Beispiel 😉
private void Form1_Load(object sender, EventArgs e)
{
    OpenFileDialog selectFile = new OpenFileDialog();
    selectFile.Title = "Select File";
    selectFile.Filter = "Text Files|*.txt|All Files|*.*";
    selectFile.FileName = "*.txt";
    if (selectFile.ShowDialog() == DialogResult.OK)
        LoadContent(selectFile.FileName);
    else
        Close();
}

private void LoadContent(string FileName)
{
    try
    {
        Text = Path.GetFileNameWithoutExtension(FileName);
        foreach (string line in File.ReadAllLines(FileName))
        {
            string[] info = line.Split('|');
            Button b = new Button();
            b.Text = info[0];
            b.Width = Convert.ToInt32(info[1]);
            b.Height = Convert.ToInt32(info[2]);
            b.Left = Convert.ToInt32(info[3]);
            b.Top = Convert.ToInt32(info[4]);
            if (info[5] != "[default]")
            {
                b.UseVisualStyleBackColor = false;
                string[] rgb = info[5].Split(';');
                b.BackColor = Color.FromArgb(Convert.ToInt32(rgb[0]), Convert.ToInt32(rgb[1]), Convert.ToInt32(rgb[2]));
            }
            b.Click += new System.EventHandler(this.ShowTextAsMsg);
            Controls.Add(b);
        }
    }
    catch
    {
        MessageBox.Show("Information could not be read.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        Close();
    }
}

private void ShowTextAsMsg(object sender, EventArgs e)
{
    MessageBox.Show("You pressed '" + ((Button)sender).Text + "'!");
}
private void Form1_Load(object sender, EventArgs e)
{
    OpenFileDialog selectFile = new OpenFileDialog();
    selectFile.Title = "Select File";
    selectFile.Filter = "Text Files|*.txt|All Files|*.*";
    selectFile.FileName = "*.txt";
    if (selectFile.ShowDialog() == DialogResult.OK)
        LoadContent(selectFile.FileName);
    else
        Close();
}

private void LoadContent(string FileName)
{
    try
    {
        Text = Path.GetFileNameWithoutExtension(FileName);
        foreach (string line in File.ReadAllLines(FileName))
        {
            string[] info = line.Split('|');
            Button b = new Button();
            b.Text = info[0];
            b.Width = Convert.ToInt32(info[1]);
            b.Height = Convert.ToInt32(info[2]);
            b.Left = Convert.ToInt32(info[3]);
            b.Top = Convert.ToInt32(info[4]);
            if (info[5] != "[default]")
            {
                b.UseVisualStyleBackColor = false;
                string[] rgb = info[5].Split(';');
                b.BackColor = Color.FromArgb(Convert.ToInt32(rgb[0]), Convert.ToInt32(rgb[1]), Convert.ToInt32(rgb[2]));
            }
            b.Click += new System.EventHandler(this.ShowTextAsMsg);
            Controls.Add(b);
        }
    }
    catch
    {
        MessageBox.Show("Information could not be read.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        Close();
    }
}

private void ShowTextAsMsg(object sender, EventArgs e)
{
    MessageBox.Show("You pressed '" + ((Button)sender).Text + "'!");
}