Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 输入框中的图片框_C#_Picturebox_Inputbox - Fatal编程技术网

C# 输入框中的图片框

C# 输入框中的图片框,c#,picturebox,inputbox,C#,Picturebox,Inputbox,我想把一个PictureBox放在输入框中,我试着用这种方式,但不起作用(图片不显示): 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, re

我想把一个PictureBox放在输入框中,我试着用这种方式,但不起作用(图片不显示):

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

尝试使用此代码来构建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(.....);

这段代码将把您的图像放入
输入框
确保表单的大小足以显示您的图像,您还需要进行定位。我所做的是向InputBox的新方法添加另一个参数,以将图像传递给控件。看看这对你有什么好处

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。我对上面的示例进行了编辑,请查看。

System.Drawing.dll中发生了类型为“System.ArgumentException”的未处理异常。其他信息:参数无效。@user1534830它正在我的计算机上工作。我在发布之前运行过它。您需要验证输入的参数是否为正确的类型。尝试在一个示例项目(而不是主项目)中运行此功能。我现在正在玩它,因为较小的图像显示在表单的边界之外。我的图像被添加到资源中,我添加了新的位图(“alarma.png”),我的图像的尺寸为128 x 128!我创建了一个新项目,并添加了代码,将图像添加到参考资料和新位图(“alarma.png”),如果您有新位图(“此处的图像”)。我也有同样的错误。我认为程序找不到图像。@user1534830我也在使用参考资料中的一个图像,它正在工作,我的示例是自包含的,请在单独的项目中使用它。