InputBox中的PictureBox

时间:2012-07-29 16:07:35

标签: c# picturebox inputbox

我想将一个PictureBox放在一个InputBox中,我尝试这种方式,但它不起作用(图片没有出现):

InputBox的原始代码是:http://www.csharp-examples.net/inputbox/ 我刚改变了一下。

包含来自InputBox的PictureBox的代码:

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;

包含Form1中PictureBox的代码: (代码位于私有空白功能中)

图片将添加到资源 !!

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

2 个答案:

答案 0 :(得分:1)

尝试使用此代码来构建您的pictureBox

picture.Width = 100;

picture.Height = 100;//just an example

Bitmap image = new Bitmap("alarma.png");

picture.Image = (Image)image;

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

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


form.Controls.AddRange(.....);

答案 1 :(得分:1)

此代码会将您的图片放入InputBox,确保表单的大小足以显示您的图片,您还必须使用定位。我所做的是在InputBox的New方法中添加另一个参数,将图像传递给控件。看看这对你有什么用。

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;
        }

    }
}

在添加控件后,只需在代码中注意到您正在设置ClientSize,因此图像显示的是负X位置值。您可以确保ClientSize不小于表单大小,也可以在添加控件之前设置ClientSize。我对上面的例子进行了编辑,请看一下。