PictureBox不会在C#Winforms应用程序中以编程方式移动到新位置。 [英] PictureBox won't move to new location programatically in C# Winforms app.

查看:73
本文介绍了PictureBox不会在C#Winforms应用程序中以编程方式移动到新位置。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个C#WinForms应用程序,我必须在其中调整用户控件上的图片框的位置。以下是创建用户控件的代码:



        public void LoadIntroScreen()

        {

            glbintQuestionIndex = 0;

            ucIntroScreen IntroScreen = new ucIntroScreen(GameInfo,glbintTotalQuestions,glbblPlayingAgain,Players);

            IntroScreen.Parent = this;

            IntroScreen.Dock = DockStyle.Fill;

            IntroScreen.Show();

        }¥b $ b现在这里是将图片框移动到正确位置的代码块:



         public ucIntroScreen(clsGameInfo GameInfo,int intTotalQuestions,bool blPlayingAgain,clsPlayerInfo ThePlayers)

        {

            InitializeComponent();

            //现在让我们将图片框放在中心...
$
            if(this.Parent是frmGame)

            {

                frmGame form1 =(frmGame)this.Parent;

                pbTPImage.Location = new Point((form1.Width / 2) - (pbTPImage.Width / 2),pbTPImage.Location.Y);

             }
       


图片框的初始位置属性是X = 520,Y = 20.图片框是用户控件(ucIntroScreen)的控件。上面的代码试图将图片框的中心与父表单(frmGame)的宽度和高度相关联。图片框
将调整为Portrait(340,350)或Landscape(545,350),其Anchor属性为Top / Left。当图片框是纵向尺寸时,图片框正确定位,但是当它是横向尺寸时保持在其默认位置(520,20),因此
略微偏离中心。代码"pbTPImage.Location = new Point((form1.Width / 2) - (pbTPImage.Width / 2),pbTPImage.Location.Y);"应该适用于纵向和横向尺寸,但我仍然坚持如何解决这个问题。

I'm working on a C# WinForms app in which I must adjust the location of a picturebox on a user control. Here's the code for creating the user control:

        public void LoadIntroScreen()
        {
            glbintQuestionIndex = 0;
            ucIntroScreen IntroScreen = new ucIntroScreen(GameInfo, glbintTotalQuestions, glbblPlayingAgain, Players);
            IntroScreen.Parent = this;
            IntroScreen.Dock = DockStyle.Fill;
            IntroScreen.Show();
        }
Now here's the block of code to move the picturebox to the right place:

        public ucIntroScreen(clsGameInfo GameInfo, int intTotalQuestions, bool blPlayingAgain, clsPlayerInfo ThePlayers)
        {
            InitializeComponent();
            // Now let's center the picturebox...
            if (this.Parent is frmGame)
            {
                frmGame form1 = (frmGame)this.Parent;
                pbTPImage.Location = new Point((form1.Width / 2) - (pbTPImage.Width / 2), pbTPImage.Location.Y);
            }
        }

The picturebox's initial location property is X = 520 and Y = 20. The picturebox is a control on the user control(ucIntroScreen). The code above is an attempt to center the picturebox in relation to the width and height of the parent form (frmGame). The picturebox will be resized to Portrait(340, 350) or Landscape(545, 350) and its Anchor property is Top/Left. When the picturebox is Portrait size, the picturebox positions itself correctly, but remains at its default location(520, 20) when it is Landscape size, and thus slightly off-center. The code "pbTPImage.Location = new Point((form1.Width / 2) - (pbTPImage.Width / 2), pbTPImage.Location.Y);" should work for both Portrait and Landscape sizes, but I'm stuck on how to fix this.

推荐答案

密尔沃基广泛,

Hi Milwaukee Broad,

也许您可以尝试使用Form.SizeChanged事件来设置图片框的位置,示例:

Perhaps you could try to use Form.SizeChanged event to set the picturebox's location, sample:

if (this.Parent is frmGame) {

frmGame form1 =(frmGame)this.Parent;
form1.SizeChanged + =(obj,arg)=>
{
pbTPImage.Location = new Point((form1.Width / 2) - (pbTPImage.Width / 2),pbTPImage.Location.Y);
};
}

frmGame form1 = (frmGame)this.Parent; form1.SizeChanged += (obj, arg) => { pbTPImage.Location = new Point((form1.Width / 2) - (pbTPImage.Width / 2), pbTPImage.Location.Y); }; }

如果上面没有帮助,请您提供更多详细信息?

If above is not help, Could you please provide more detail information about this?

问候,

Moonlight

Moonlight


这篇关于PictureBox不会在C#Winforms应用程序中以编程方式移动到新位置。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆