简体   繁体   English

使用2个NumericUpDown字段来维护Dimensions纵横比

[英]Using 2 NumericUpDown fields to maintain Dimensions aspect ratio

I have two NumericUpDown text fields. 我有两个NumericUpDown文本字段。 They are used for Image Dimensions. 它们用于图像尺寸。 I've done it before but I don't remember how I did it. 我以前做过,但是我不记得我是怎么做的。 I've looked at over 20 related questions on here but they're not really the same question. 我看就在这里了20个相关的问题,但他们并不是真正的同样的问题。 The logic and math is totally different than what I need. 逻辑和数学与我所需要的完全不同。

Basically, numericUpDown1 is for Width and numericUpDown2 is for Height. 基本上,numericalUpDown1表示宽度,numericalUpDown2表示高度。 When the numericUpDown1 value is changed (either by using arrows or typing), numericUpDown2 must auto change as well, while maintaining the correct aspect ratio. 更改numericUpDown1值(通过使用箭头或键入)时,numericUpDown2也必须自动更改,同时保持正确的宽高比。

How can I do this? 我怎样才能做到这一点? I'm really lost. 我真的迷路了。

If the aspect ratio is simply defined like this: Width/Height, you should implement both the ValueChanged events of the two NumericUpDown controls: in the event for the numericUpDown1 (for width) you should set the new height value by dividing the numericUpDown1.Value by the aspect ratio; 如果像这样简单地定义宽高比:宽度/高度,则应同时实现两个NumericUpDown控件的ValueChanged事件:如果遇到numericUpDown1(用于宽度),则应通过除以numericUpDown1.Value来设置新的高度值。通过长宽比 in the other event you should multiply numericUpDown2.Value by the aspect ratio in order to get the new width. 在另一种情况下,您应该将numericUpDown2.Value乘以纵横比以获得新的宽度。

  • Keep the original values, as the base for the calculation. 保留原始值作为计算的基础。 This will prevent rounding errors. 这样可以防止舍入错误。
  • Set a flag to indicate that you are adjusting the other control(s) from your code, and they should not react to their events. 设置一个标志以指示您正在调整代码中的其他控件,并且其他控件不应对其事件做出反应。 So you don't end up in an endless loop. 因此,您不会陷入无休止的循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在这种情况下如何保持网格的纵横比? - How to maintain the aspect ratio of Grid in this case? 如何以编程方式保持 wpf 中对象的纵横比 - How to programmatically maintain aspect ratio of object in wpf 如何调整图像大小并保持宽高比? - How to resize my image and maintain the aspect ratio? 我应该如何在 Unity c# 中保持纵横比并将 3D 对象设置为在所选纵横比的纵横比内缩放? - How should I Maintain aspect ratio in Unity c# and set the 3D object to scale within the aspect ratio of the selected aspect ratio? 调整Window Forms应用程序大小时如何保持长宽比? - How to maintain the aspect ratio when resizing a Window Forms application? Unity)我怎样才能保持精灵的纵横比? - Unity) How can I maintain sprites aspect ratio? Win Form调整大小时如何保持Datagridview列宽的宽高比 - How to maintain aspect ratio of datagridview column width when win form resize 如何在Textblock中缩小字体大小以适合内容的宽度并保持字体长宽比 - How to shrink font size in Textblock to fit width of content AND maintain font aspect ratio 使用MediaCapture时,长宽比/缩放级别更改是否存在问题? - Issue with aspect ratio / zoom level changing when using MediaCapture? 如何使用 C# 更改 Windows 媒体播放器的纵横比 - How to change the aspect ratio of an Windows media player by using C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM