Frage

Ich möchte eine Bildbox in eine Eingabebox einfügen, ich versuche es auf diese Weise, aber es funktioniert nicht (das Bild wird nicht angezeigt):

Der ursprüngliche Code aus der Eingabebox lautet wie folgt: http://www.csharp-examples.net/inputbox/ Ich habe mich nur ein bisschen verändert.

Der Code, der die Bildbox aus der Eingabebox enthält:

public static DialogResult Show(string title, string luna_text, ref string luna_continut, string zi_text, ref string zi_continut, string ora_text, ref string ora_continut, string minut_text, ref string minut_continut, string mesaj, ref string imagine)

PictureBox picture = new PictureBox();

picture.ImageLocation = imagine;

picture.SetBounds(14, 60, 128, 128);

picture.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

form.Controls.AddRange(new Control[] { label1, textBox1, label2, textBox2, label3, textBox3, label4, textBox4, label5, picture, buttonOk });

imagine = picture.ImageLocation;

Der Code, der PictureBox aus Form1 enthält: (Der Code ist in einem private Leere Funktion)

Das Bild wird hinzugefügt Ressourcen !!

string inputbox = "";
string imagine = "alarma.png";

inputbox = CeasAlarma.InputBoxAnuntareAlarma.Show("CEAS ALARMA", "Luna:", ref luna, "Zi:", ref zi, "Ora:", ref ora, "Minut:", ref minut, "------ Ai o alarma care sunt in acest moment ! ------", ref imagine).ToString();

if (inputbox == "Cancel" || inputbox == "OK")
   //will do something
War es hilfreich?

Lösung

Dieser Code bringt Ihr Bild in Ihre InputBox stellen Sie sicher, dass die Größe des Formulars groß genug ist, um Ihr Bild anzuzeigen.Sie müssen auch mit der Positionierung spielen.Ich habe der neuen Methode der Eingabebox einen weiteren Parameter hinzugefügt, um das Bild an Ihr Steuerelement zu übergeben.Sehen Sie, wie das für Sie funktioniert.

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            string value = "Document 1";

            if (Tmp.InputBox("New document", "New document name:", ref value, new Bitmap("Your Image Here") == DialogResult.OK)
            {
                this.Text = value;
            }
        }
    }

    public static class Tmp  //Note new field called bitmap for passing your picture to the InputBox
    {
        public static DialogResult InputBox(string title, string promptText, ref string value, Bitmap image)
        {
            Form form = new Form();
            Label label = new Label();
            TextBox textBox = new TextBox();
            Button buttonOk = new Button();
            Button buttonCancel = new Button();
            PictureBox picture = new PictureBox();


            form.Text = title;
            label.Text = promptText;
            textBox.Text = value;
            picture.Image = image;

            buttonOk.Text = "OK";
            buttonCancel.Text = "Cancel";
            buttonOk.DialogResult = DialogResult.OK;
            buttonCancel.DialogResult = DialogResult.Cancel;

            label.SetBounds(9, 20, 372, 13);
            textBox.SetBounds(12, 36, 372, 20);
            buttonOk.SetBounds(228, 72, 75, 23);
            buttonCancel.SetBounds(309, 72, 75, 23);
            picture.SetBounds(14, 60, 128, 128);

            label.AutoSize = true;
            textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
            buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            picture.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

            form.ClientSize = new Size(396, 400); //Changed size to see the image
            form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height); //Changed position so you are not shrinking the available size after the controls are added
            form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel, picture});
            form.FormBorderStyle = FormBorderStyle.FixedDialog;
            form.StartPosition = FormStartPosition.CenterScreen;
            form.MinimizeBox = false;
            form.MaximizeBox = false;
            form.AcceptButton = buttonOk; 
            form.CancelButton = buttonCancel;

            DialogResult dialogResult = form.ShowDialog();
            value = textBox.Text;
            return dialogResult;
        }

    }
}

Beachten Sie in Ihrem Code einfach, dass Sie Ihre ClientSize nachdem Sie Ihre Steuerelemente hinzugefügt haben, daher wird das Bild mit einem negativen X-Positionswert angezeigt.Sie können entweder sicherstellen, dass die ClientSize nicht kleiner als die Formulargröße ist, oder die ClientSize festlegen, bevor Sie Ihre Steuerelemente hinzufügen.Ich habe Änderungen am obigen Beispiel vorgenommen, bitte schauen Sie sich das an.

Andere Tipps

Versuchen Sie es mit diesem Code, um Ihre PictureBox aufzubauen

generasacodicetagpre.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top