question

ItsHard-4727 avatar image
0 Votes"
ItsHard-4727 asked ItsHard-4727 answered

Form Shrinking Once upon KeyDown

My Program resizes once smaller with smaller pictureboxes and buttons when I receive a keypress, I reproduced the behavior here, any ideas? Visual Studio 2019

Here is the bug problem https://drive.google.com/file/d/1e2h4UhgOosk6u7sl21T7EVSqDpCDXqaH/view?usp=sharing it on my PC it shrinks about 20% once on keypress

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Media;

namespace tesatbug
{
public partial class Form1 : Form
{
MediaPlayer audio1;

     protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, Keys keyData)
     {
       // audio1 = new System.Windows.Media.MediaPlayer(); //uncomment to reproduce form shrink
         MessageBox.Show("cheese");  return true;

     }
         public Form1()
     {
         InitializeComponent();
     }

     private void Form1_Load(object sender, EventArgs e)
     {

     }
 }

}

dotnet-csharp
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


The issue does not seems reproduced. If you have a simple project that demonstrates the problem, you can publish it on OneDrive or another place.

Since you need the MediaPlayer, maybe you can switch to WPF development instead of Forms.


0 Votes 0 ·

Have added a link to the bug Solution. Does it shrink the form 20% or so for you? Thank you for your time.

0 Votes 0 ·

The form does not shrink.

1 Vote 1 ·

I notice when I open the Design tab it says 'Scaling on your main display is set to 125%. Restart Visual Studio with 100% Scaling'

Could this be an issue? I have set the displays to 100% and still get this message.

0 Votes 0 ·
ItsHard-4727 avatar image
0 Votes"
ItsHard-4727 answered

Thanks for the help Viorel-1 the problem was fixed by rebooting after setting the display scaling to 100% for both monitors in Start>Display Settings>Scale and layout. I needed to reboot after then rebuild project.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered

If you need to change the size of the form, you can modify its width and height properties and use a loop to make it an animation.

             for (int i = 0; i < 20; i++)
             {
                 this.Height -= 7;
                 this.Width -= 10;
                 Thread.Sl*eep(10);
             }

You can also consider using WPF, which has rich animation effects.

 <Grid>
         <Button Content="Click" Width="100" Height="30">
             <Button.Triggers>
                 <EventTrigger  RoutedEvent="Button.Click">
                     <EventTrigger.Actions>
                         <BeginStoryboard >
                             <Storyboard  RepeatBehavior="Forever" AutoReverse="False">
                                 <DoubleAnimation  Storyboard.TargetName="myWindow"
                                       Storyboard.TargetProperty = "(Window.Height)"
                                         To="300" Duration="0:0:5"/>
                                 <Storyboard  RepeatBehavior="Forever" AutoReverse="False">
    
                                     <DoubleAnimation  Storyboard.TargetName="myWindow"
                                       Storyboard.TargetProperty = "(Window.Width)"
                                         To="300" Duration="0:0:5"/>
                                 </Storyboard>
                             </Storyboard>
                         </BeginStoryboard>
    
                     </EventTrigger.Actions>
                 </EventTrigger>
             </Button.Triggers>
         </Button>
     </Grid>

If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.