From 3b0de4ccb06b6ba10a6c4be61a3c3f9ad7af73b1 Mon Sep 17 00:00:00 2001 From: "Simon Zhao (BEYONDSOFT CONSULTING INC)" Date: Thu, 2 Jul 2026 12:17:52 +0800 Subject: [PATCH 01/10] Fix issue 14693: TrackBar UI control doesn't reflect properties --- .../PublicAPI.Shipped.txt | 1 + .../Forms/Controls/TrackBar/TrackBar.cs | 20 ++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/System.Windows.Forms/PublicAPI.Shipped.txt b/src/System.Windows.Forms/PublicAPI.Shipped.txt index c8734dec7e8..555b2b253e6 100644 --- a/src/System.Windows.Forms/PublicAPI.Shipped.txt +++ b/src/System.Windows.Forms/PublicAPI.Shipped.txt @@ -2402,6 +2402,7 @@ override System.Windows.Forms.TrackBar.IsInputKey(System.Windows.Forms.Keys keyD override System.Windows.Forms.TrackBar.OnBackColorChanged(System.EventArgs! e) -> void override System.Windows.Forms.TrackBar.OnHandleCreated(System.EventArgs! e) -> void override System.Windows.Forms.TrackBar.OnMouseWheel(System.Windows.Forms.MouseEventArgs! e) -> void +override System.Windows.Forms.TrackBar.OnSizeChanged(System.EventArgs! e) -> void override System.Windows.Forms.TrackBar.OnSystemColorsChanged(System.EventArgs! e) -> void override System.Windows.Forms.TrackBar.SetBoundsCore(int x, int y, int width, int height, System.Windows.Forms.BoundsSpecified specified) -> void override System.Windows.Forms.TrackBar.Text.get -> string! diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs index eea2d4b47d2..2583802dd92 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs @@ -793,13 +793,31 @@ private void DrawTicksManually() } PInvokeCore.SendMessage(this, PInvoke.TBM_CLEARTICS, (WPARAM)1, (LPARAM)0); - for (int i = _minimum + drawnTickFrequency; i < _maximum - drawnTickFrequency; i += drawnTickFrequency) + for (int i = _minimum + drawnTickFrequency; i <= _maximum - drawnTickFrequency; i += drawnTickFrequency) { LRESULT lresult = PInvokeCore.SendMessage(this, PInvoke.TBM_SETTIC, lParam: (IntPtr)i); Debug.Assert((bool)(BOOL)lresult); } } + protected override void OnSizeChanged(EventArgs e) + { + base.OnSizeChanged(e); + if (!IsHandleCreated) + { + return; + } + + if (ShouldRecreateHandle()) + { + RecreateHandle(); + return; + } + + DrawTicksManually(); + Invalidate(); + } + /// /// Called when initialization of the control is complete. /// From 551a9eef83060f6e7ad8d85a33fcd23c77b501cc Mon Sep 17 00:00:00 2001 From: "Simon Zhao (BEYONDSOFT CONSULTING INC)" Date: Thu, 2 Jul 2026 17:27:03 +0800 Subject: [PATCH 02/10] Handling logic errors --- .../PublicAPI.Shipped.txt | 1 - .../Forms/Controls/TrackBar/TrackBar.cs | 35 ++++--------------- 2 files changed, 6 insertions(+), 30 deletions(-) diff --git a/src/System.Windows.Forms/PublicAPI.Shipped.txt b/src/System.Windows.Forms/PublicAPI.Shipped.txt index 555b2b253e6..c8734dec7e8 100644 --- a/src/System.Windows.Forms/PublicAPI.Shipped.txt +++ b/src/System.Windows.Forms/PublicAPI.Shipped.txt @@ -2402,7 +2402,6 @@ override System.Windows.Forms.TrackBar.IsInputKey(System.Windows.Forms.Keys keyD override System.Windows.Forms.TrackBar.OnBackColorChanged(System.EventArgs! e) -> void override System.Windows.Forms.TrackBar.OnHandleCreated(System.EventArgs! e) -> void override System.Windows.Forms.TrackBar.OnMouseWheel(System.Windows.Forms.MouseEventArgs! e) -> void -override System.Windows.Forms.TrackBar.OnSizeChanged(System.EventArgs! e) -> void override System.Windows.Forms.TrackBar.OnSystemColorsChanged(System.EventArgs! e) -> void override System.Windows.Forms.TrackBar.SetBoundsCore(int x, int y, int width, int height, System.Windows.Forms.BoundsSpecified specified) -> void override System.Windows.Forms.TrackBar.Text.get -> string! diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs index 2583802dd92..8bda6f2b5d7 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs @@ -781,41 +781,18 @@ private void DrawTicksManually() int drawnTickFrequency = _tickFrequency; // Divide by 2 because otherwise the ticks appear as a solid line. - int maxTickCount = (Orientation == Orientation.Horizontal ? Size.Width : Size.Height) / 2; uint range = (uint)(_maximum - _minimum); - if (range > maxTickCount && maxTickCount != 0) - { - int calculatedTickFrequency = (int)(range / maxTickCount); - if (calculatedTickFrequency > drawnTickFrequency) - { - drawnTickFrequency = calculatedTickFrequency; - } - } + int maxTickCount = range == 0 + ? 1 + : (int)range / drawnTickFrequency + 1; PInvokeCore.SendMessage(this, PInvoke.TBM_CLEARTICS, (WPARAM)1, (LPARAM)0); - for (int i = _minimum + drawnTickFrequency; i <= _maximum - drawnTickFrequency; i += drawnTickFrequency) - { - LRESULT lresult = PInvokeCore.SendMessage(this, PInvoke.TBM_SETTIC, lParam: (IntPtr)i); - Debug.Assert((bool)(BOOL)lresult); - } - } - protected override void OnSizeChanged(EventArgs e) - { - base.OnSizeChanged(e); - if (!IsHandleCreated) + for (int i = 1; i <= maxTickCount; i++) { - return; - } - - if (ShouldRecreateHandle()) - { - RecreateHandle(); - return; + int tickValue = Minimum + i * drawnTickFrequency; + PInvokeCore.SendMessage(this, PInvoke.TBM_SETTIC, (WPARAM)0, (LPARAM)tickValue); } - - DrawTicksManually(); - Invalidate(); } /// From 75126efecb101852b015ab743ae1ca6e12c743e2 Mon Sep 17 00:00:00 2001 From: "Simon Zhao (BEYONDSOFT CONSULTING INC)" Date: Fri, 3 Jul 2026 13:57:27 +0800 Subject: [PATCH 03/10] Handle feedback --- .../System/Windows/Forms/Controls/TrackBar/TrackBar.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs index 8bda6f2b5d7..430e6cecd3d 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs @@ -779,7 +779,7 @@ private void DrawTicksManually() return; } - int drawnTickFrequency = _tickFrequency; + int drawnTickFrequency = _tickFrequency <= 0 ? 1 : _tickFrequency; // Divide by 2 because otherwise the ticks appear as a solid line. uint range = (uint)(_maximum - _minimum); int maxTickCount = range == 0 @@ -788,10 +788,11 @@ private void DrawTicksManually() PInvokeCore.SendMessage(this, PInvoke.TBM_CLEARTICS, (WPARAM)1, (LPARAM)0); - for (int i = 1; i <= maxTickCount; i++) + for (int i = 1; i < maxTickCount; i++) { int tickValue = Minimum + i * drawnTickFrequency; - PInvokeCore.SendMessage(this, PInvoke.TBM_SETTIC, (WPARAM)0, (LPARAM)tickValue); + LRESULT lresult = PInvokeCore.SendMessage(this, PInvoke.TBM_SETTIC, (WPARAM)0, (LPARAM)tickValue); + Debug.Assert((bool)(BOOL)lresult); } } From 7a813e4dc5ef30492e201aa7c64827f950be5d14 Mon Sep 17 00:00:00 2001 From: "Simon Zhao (BEYONDSOFT CONSULTING INC)" Date: Fri, 3 Jul 2026 13:59:28 +0800 Subject: [PATCH 04/10] Handle feedback --- .../System/Windows/Forms/Controls/TrackBar/TrackBar.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs index 430e6cecd3d..21450d96d84 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs @@ -780,7 +780,6 @@ private void DrawTicksManually() } int drawnTickFrequency = _tickFrequency <= 0 ? 1 : _tickFrequency; - // Divide by 2 because otherwise the ticks appear as a solid line. uint range = (uint)(_maximum - _minimum); int maxTickCount = range == 0 ? 1 From 2a144e57794f548f2785af3d868bb74adbc0688f Mon Sep 17 00:00:00 2001 From: "Simon Zhao (BEYONDSOFT CONSULTING INC)" Date: Mon, 6 Jul 2026 11:03:03 +0800 Subject: [PATCH 05/10] Optimize the code --- src/System.Windows.Forms/Resources/SR.resx | 5 +++- .../Resources/xlf/SR.cs.xlf | 5 ++++ .../Resources/xlf/SR.de.xlf | 5 ++++ .../Resources/xlf/SR.es.xlf | 5 ++++ .../Resources/xlf/SR.fr.xlf | 5 ++++ .../Resources/xlf/SR.it.xlf | 5 ++++ .../Resources/xlf/SR.ja.xlf | 5 ++++ .../Resources/xlf/SR.ko.xlf | 5 ++++ .../Resources/xlf/SR.pl.xlf | 5 ++++ .../Resources/xlf/SR.pt-BR.xlf | 5 ++++ .../Resources/xlf/SR.ru.xlf | 5 ++++ .../Resources/xlf/SR.tr.xlf | 5 ++++ .../Resources/xlf/SR.zh-Hans.xlf | 5 ++++ .../Resources/xlf/SR.zh-Hant.xlf | 5 ++++ .../Forms/Controls/TrackBar/TrackBar.cs | 25 +++++++++++++------ 15 files changed, 87 insertions(+), 8 deletions(-) diff --git a/src/System.Windows.Forms/Resources/SR.resx b/src/System.Windows.Forms/Resources/SR.resx index 53d5073b748..33cd6a3d879 100644 --- a/src/System.Windows.Forms/Resources/SR.resx +++ b/src/System.Windows.Forms/Resources/SR.resx @@ -6184,6 +6184,9 @@ Stack trace where the illegal operation occurred was: SmallChange: Value '{0}' must be greater than or equal to 0. + + TickFrequency: Value '{0}' must be greater than or equal to 0. + The number of positions between tick marks. @@ -7045,4 +7048,4 @@ Stack trace where the illegal operation occurred was: The FormScreenCaptureMode property can only be changed on top-level Forms with their TopLevel property set to true. - \ No newline at end of file + diff --git a/src/System.Windows.Forms/Resources/xlf/SR.cs.xlf b/src/System.Windows.Forms/Resources/xlf/SR.cs.xlf index ce2bc9260f9..9dab981c88b 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.cs.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.cs.xlf @@ -10789,6 +10789,11 @@ Trasování zásobníku, kde došlo k neplatné operaci: Počet pozic mezi značkami. + + TickFrequency: Value '{0}' must be greater than or equal to 0. + TickFrequency: Value '{0}' must be greater than or equal to 0. + + Indicates where the ticks appear on the TrackBar. Označuje, kde budou na panelu TrackBar zobrazeny značky. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.de.xlf b/src/System.Windows.Forms/Resources/xlf/SR.de.xlf index b5cf47b1170..ddd306599fa 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.de.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.de.xlf @@ -10789,6 +10789,11 @@ Stapelüberwachung, in der der unzulässige Vorgang auftrat: Die Anzahl der Positionen zwischen den Teilstrichen. + + TickFrequency: Value '{0}' must be greater than or equal to 0. + TickFrequency: Value '{0}' must be greater than or equal to 0. + + Indicates where the ticks appear on the TrackBar. Gibt an, ob die Teilstriche in der TrackBar angezeigt werden. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.es.xlf b/src/System.Windows.Forms/Resources/xlf/SR.es.xlf index 82eb0cd40bb..832d4e394d9 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.es.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.es.xlf @@ -10789,6 +10789,11 @@ El seguimiento de la pila donde tuvo lugar la operación no válida fue: Número de posiciones entre marcas de paso. + + TickFrequency: Value '{0}' must be greater than or equal to 0. + TickFrequency: Value '{0}' must be greater than or equal to 0. + + Indicates where the ticks appear on the TrackBar. Indica dónde aparecerán los pasos en la barra de seguimiento. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.fr.xlf b/src/System.Windows.Forms/Resources/xlf/SR.fr.xlf index f02c36b8e65..936d2ea1579 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.fr.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.fr.xlf @@ -10789,6 +10789,11 @@ Cette opération non conforme s'est produite sur la trace de la pile : Le nombre de positions entre les graduations. + + TickFrequency: Value '{0}' must be greater than or equal to 0. + TickFrequency: Value '{0}' must be greater than or equal to 0. + + Indicates where the ticks appear on the TrackBar. Indique l'emplacement où les graduations s'affichent sur le TrackBar. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.it.xlf b/src/System.Windows.Forms/Resources/xlf/SR.it.xlf index fc9f709a0be..5e4203491b5 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.it.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.it.xlf @@ -10789,6 +10789,11 @@ Analisi dello stack dove si è verificata l'operazione non valida: Il numero di posizioni tra i segni di graduazione. + + TickFrequency: Value '{0}' must be greater than or equal to 0. + TickFrequency: Value '{0}' must be greater than or equal to 0. + + Indicates where the ticks appear on the TrackBar. Indica dove vengono visualizzati i segni di graduazione nel controllo TrackBar. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.ja.xlf b/src/System.Windows.Forms/Resources/xlf/SR.ja.xlf index 4511ba6cf76..b5babfea6d1 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.ja.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.ja.xlf @@ -10789,6 +10789,11 @@ Stack trace where the illegal operation occurred was: 目盛りマーク間の位置の数です。 + + TickFrequency: Value '{0}' must be greater than or equal to 0. + TickFrequency: Value '{0}' must be greater than or equal to 0. + + Indicates where the ticks appear on the TrackBar. 目盛りをトラックバーのどこに表示するか示します。 diff --git a/src/System.Windows.Forms/Resources/xlf/SR.ko.xlf b/src/System.Windows.Forms/Resources/xlf/SR.ko.xlf index 2924f72aab7..c46bde328d9 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.ko.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.ko.xlf @@ -10789,6 +10789,11 @@ Stack trace where the illegal operation occurred was: 눈금 사이의 위치 개수입니다. + + TickFrequency: Value '{0}' must be greater than or equal to 0. + TickFrequency: Value '{0}' must be greater than or equal to 0. + + Indicates where the ticks appear on the TrackBar. TrackBar에서 눈금이 표시되는 위치를 나타냅니다. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.pl.xlf b/src/System.Windows.Forms/Resources/xlf/SR.pl.xlf index f86297cb7f6..1d726b2b730 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.pl.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.pl.xlf @@ -10789,6 +10789,11 @@ Stos śledzenia, w którym wystąpiła zabroniona operacja: Liczba pozycji między znacznikami osi. + + TickFrequency: Value '{0}' must be greater than or equal to 0. + TickFrequency: Value '{0}' must be greater than or equal to 0. + + Indicates where the ticks appear on the TrackBar. Wskazuje, gdzie w obiekcie TrackBar mają się pojawić znaczniki osi. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.pt-BR.xlf b/src/System.Windows.Forms/Resources/xlf/SR.pt-BR.xlf index 5dcda8ac793..83af5834c43 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.pt-BR.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.pt-BR.xlf @@ -10789,6 +10789,11 @@ O rastreamento de pilha em que a operação ilegal ocorreu foi: O número de posições entre marcas de tiques. + + TickFrequency: Value '{0}' must be greater than or equal to 0. + TickFrequency: Value '{0}' must be greater than or equal to 0. + + Indicates where the ticks appear on the TrackBar. Indica onde os tiques são exibidos em TrackBar. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.ru.xlf b/src/System.Windows.Forms/Resources/xlf/SR.ru.xlf index 89af4e75583..f17f569e64e 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.ru.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.ru.xlf @@ -10789,6 +10789,11 @@ Stack trace where the illegal operation occurred was: Число позиций между отметками на оси. + + TickFrequency: Value '{0}' must be greater than or equal to 0. + TickFrequency: Value '{0}' must be greater than or equal to 0. + + Indicates where the ticks appear on the TrackBar. Указывает, отображаются ли отметки для TrackBar. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.tr.xlf b/src/System.Windows.Forms/Resources/xlf/SR.tr.xlf index 9e72305a890..0bb5068a3fc 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.tr.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.tr.xlf @@ -10789,6 +10789,11 @@ Geçersiz işlemin gerçekleştiği yığın izi: Değer çizgileri arasındaki konum sayısı. + + TickFrequency: Value '{0}' must be greater than or equal to 0. + TickFrequency: Value '{0}' must be greater than or equal to 0. + + Indicates where the ticks appear on the TrackBar. Ölçü çizgilerin TrackBar üzerinde nerede görüneceğini gösterir. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.zh-Hans.xlf b/src/System.Windows.Forms/Resources/xlf/SR.zh-Hans.xlf index 6827f20269f..398498856e0 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.zh-Hans.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.zh-Hans.xlf @@ -10789,6 +10789,11 @@ Stack trace where the illegal operation occurred was: 刻度线间的位置数。 + + TickFrequency: Value '{0}' must be greater than or equal to 0. + TickFrequency: Value '{0}' must be greater than or equal to 0. + + Indicates where the ticks appear on the TrackBar. 指示在 TrackBar 上的哪些位置显示刻度。 diff --git a/src/System.Windows.Forms/Resources/xlf/SR.zh-Hant.xlf b/src/System.Windows.Forms/Resources/xlf/SR.zh-Hant.xlf index 15f5f06c126..cb9fe45b98e 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.zh-Hant.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.zh-Hant.xlf @@ -10789,6 +10789,11 @@ Stack trace where the illegal operation occurred was: 刻度間相距的位置數值大小。 + + TickFrequency: Value '{0}' must be greater than or equal to 0. + TickFrequency: Value '{0}' must be greater than or equal to 0. + + Indicates where the ticks appear on the TrackBar. 指定 TrackBar 上刻度的顯示位置。 diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs index 21450d96d84..d6895307718 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs @@ -562,6 +562,11 @@ public int TickFrequency get => _tickFrequency; set { + if (value < 0) + { + throw new ArgumentOutOfRangeException(nameof(value), value, string.Format(SR.TrackBarTickFrequencyError, value)); + } + if (_tickFrequency == value) { return; @@ -780,14 +785,20 @@ private void DrawTicksManually() } int drawnTickFrequency = _tickFrequency <= 0 ? 1 : _tickFrequency; + // Estimate the maximum number of ticks that can be accommodated using the control's pixel dimensions. + int maxTickCount = (Orientation == Orientation.Horizontal ? Size.Width : Size.Height); uint range = (uint)(_maximum - _minimum); - int maxTickCount = range == 0 + // Calculate the number of ticks to draw based on the frequency. + int exceptionTickCount = range == 0 ? 1 - : (int)range / drawnTickFrequency + 1; + : (int)range / drawnTickFrequency; + if (maxTickCount > exceptionTickCount) + { + maxTickCount = exceptionTickCount; + } PInvokeCore.SendMessage(this, PInvoke.TBM_CLEARTICS, (WPARAM)1, (LPARAM)0); - - for (int i = 1; i < maxTickCount; i++) + for (int i = 1; i <= maxTickCount; i++) { int tickValue = Minimum + i * drawnTickFrequency; LRESULT lresult = PInvokeCore.SendMessage(this, PInvoke.TBM_SETTIC, (WPARAM)0, (LPARAM)tickValue); @@ -908,9 +919,9 @@ protected override void OnMouseWheel(MouseEventArgs e) if (e is HandledMouseEventArgs hme) { if (hme.Handled) - { - return; - } + { + return; + } hme.Handled = true; } From 6b80188578b403518083201bd66ab4b217a8c325 Mon Sep 17 00:00:00 2001 From: "Simon Zhao (BEYONDSOFT CONSULTING INC)" Date: Mon, 6 Jul 2026 11:05:01 +0800 Subject: [PATCH 06/10] Fixed formatting issues. --- .../System/Windows/Forms/Controls/TrackBar/TrackBar.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs index d6895307718..2edc61a2ab3 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs @@ -919,9 +919,9 @@ protected override void OnMouseWheel(MouseEventArgs e) if (e is HandledMouseEventArgs hme) { if (hme.Handled) - { - return; - } + { + return; + } hme.Handled = true; } From 233bb419b2ba76e75b157bc34c979db04d12bb97 Mon Sep 17 00:00:00 2001 From: "Simon Zhao (BEYONDSOFT CONSULTING INC)" Date: Mon, 6 Jul 2026 12:17:08 +0800 Subject: [PATCH 07/10] Handle failure test cases --- src/System.Windows.Forms/Resources/SR.resx | 3 --- src/System.Windows.Forms/Resources/xlf/SR.cs.xlf | 5 ----- src/System.Windows.Forms/Resources/xlf/SR.de.xlf | 5 ----- src/System.Windows.Forms/Resources/xlf/SR.es.xlf | 5 ----- src/System.Windows.Forms/Resources/xlf/SR.fr.xlf | 5 ----- src/System.Windows.Forms/Resources/xlf/SR.it.xlf | 5 ----- src/System.Windows.Forms/Resources/xlf/SR.ja.xlf | 5 ----- src/System.Windows.Forms/Resources/xlf/SR.ko.xlf | 5 ----- src/System.Windows.Forms/Resources/xlf/SR.pl.xlf | 5 ----- src/System.Windows.Forms/Resources/xlf/SR.pt-BR.xlf | 5 ----- src/System.Windows.Forms/Resources/xlf/SR.ru.xlf | 5 ----- src/System.Windows.Forms/Resources/xlf/SR.tr.xlf | 5 ----- src/System.Windows.Forms/Resources/xlf/SR.zh-Hans.xlf | 5 ----- src/System.Windows.Forms/Resources/xlf/SR.zh-Hant.xlf | 5 ----- .../System/Windows/Forms/Controls/TrackBar/TrackBar.cs | 5 ----- 15 files changed, 73 deletions(-) diff --git a/src/System.Windows.Forms/Resources/SR.resx b/src/System.Windows.Forms/Resources/SR.resx index 33cd6a3d879..c79934103fd 100644 --- a/src/System.Windows.Forms/Resources/SR.resx +++ b/src/System.Windows.Forms/Resources/SR.resx @@ -6184,9 +6184,6 @@ Stack trace where the illegal operation occurred was: SmallChange: Value '{0}' must be greater than or equal to 0. - - TickFrequency: Value '{0}' must be greater than or equal to 0. - The number of positions between tick marks. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.cs.xlf b/src/System.Windows.Forms/Resources/xlf/SR.cs.xlf index 9dab981c88b..ce2bc9260f9 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.cs.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.cs.xlf @@ -10789,11 +10789,6 @@ Trasování zásobníku, kde došlo k neplatné operaci: Počet pozic mezi značkami. - - TickFrequency: Value '{0}' must be greater than or equal to 0. - TickFrequency: Value '{0}' must be greater than or equal to 0. - - Indicates where the ticks appear on the TrackBar. Označuje, kde budou na panelu TrackBar zobrazeny značky. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.de.xlf b/src/System.Windows.Forms/Resources/xlf/SR.de.xlf index ddd306599fa..b5cf47b1170 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.de.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.de.xlf @@ -10789,11 +10789,6 @@ Stapelüberwachung, in der der unzulässige Vorgang auftrat: Die Anzahl der Positionen zwischen den Teilstrichen. - - TickFrequency: Value '{0}' must be greater than or equal to 0. - TickFrequency: Value '{0}' must be greater than or equal to 0. - - Indicates where the ticks appear on the TrackBar. Gibt an, ob die Teilstriche in der TrackBar angezeigt werden. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.es.xlf b/src/System.Windows.Forms/Resources/xlf/SR.es.xlf index 832d4e394d9..82eb0cd40bb 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.es.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.es.xlf @@ -10789,11 +10789,6 @@ El seguimiento de la pila donde tuvo lugar la operación no válida fue: Número de posiciones entre marcas de paso. - - TickFrequency: Value '{0}' must be greater than or equal to 0. - TickFrequency: Value '{0}' must be greater than or equal to 0. - - Indicates where the ticks appear on the TrackBar. Indica dónde aparecerán los pasos en la barra de seguimiento. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.fr.xlf b/src/System.Windows.Forms/Resources/xlf/SR.fr.xlf index 936d2ea1579..f02c36b8e65 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.fr.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.fr.xlf @@ -10789,11 +10789,6 @@ Cette opération non conforme s'est produite sur la trace de la pile : Le nombre de positions entre les graduations. - - TickFrequency: Value '{0}' must be greater than or equal to 0. - TickFrequency: Value '{0}' must be greater than or equal to 0. - - Indicates where the ticks appear on the TrackBar. Indique l'emplacement où les graduations s'affichent sur le TrackBar. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.it.xlf b/src/System.Windows.Forms/Resources/xlf/SR.it.xlf index 5e4203491b5..fc9f709a0be 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.it.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.it.xlf @@ -10789,11 +10789,6 @@ Analisi dello stack dove si è verificata l'operazione non valida: Il numero di posizioni tra i segni di graduazione. - - TickFrequency: Value '{0}' must be greater than or equal to 0. - TickFrequency: Value '{0}' must be greater than or equal to 0. - - Indicates where the ticks appear on the TrackBar. Indica dove vengono visualizzati i segni di graduazione nel controllo TrackBar. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.ja.xlf b/src/System.Windows.Forms/Resources/xlf/SR.ja.xlf index b5babfea6d1..4511ba6cf76 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.ja.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.ja.xlf @@ -10789,11 +10789,6 @@ Stack trace where the illegal operation occurred was: 目盛りマーク間の位置の数です。 - - TickFrequency: Value '{0}' must be greater than or equal to 0. - TickFrequency: Value '{0}' must be greater than or equal to 0. - - Indicates where the ticks appear on the TrackBar. 目盛りをトラックバーのどこに表示するか示します。 diff --git a/src/System.Windows.Forms/Resources/xlf/SR.ko.xlf b/src/System.Windows.Forms/Resources/xlf/SR.ko.xlf index c46bde328d9..2924f72aab7 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.ko.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.ko.xlf @@ -10789,11 +10789,6 @@ Stack trace where the illegal operation occurred was: 눈금 사이의 위치 개수입니다. - - TickFrequency: Value '{0}' must be greater than or equal to 0. - TickFrequency: Value '{0}' must be greater than or equal to 0. - - Indicates where the ticks appear on the TrackBar. TrackBar에서 눈금이 표시되는 위치를 나타냅니다. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.pl.xlf b/src/System.Windows.Forms/Resources/xlf/SR.pl.xlf index 1d726b2b730..f86297cb7f6 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.pl.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.pl.xlf @@ -10789,11 +10789,6 @@ Stos śledzenia, w którym wystąpiła zabroniona operacja: Liczba pozycji między znacznikami osi. - - TickFrequency: Value '{0}' must be greater than or equal to 0. - TickFrequency: Value '{0}' must be greater than or equal to 0. - - Indicates where the ticks appear on the TrackBar. Wskazuje, gdzie w obiekcie TrackBar mają się pojawić znaczniki osi. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.pt-BR.xlf b/src/System.Windows.Forms/Resources/xlf/SR.pt-BR.xlf index 83af5834c43..5dcda8ac793 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.pt-BR.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.pt-BR.xlf @@ -10789,11 +10789,6 @@ O rastreamento de pilha em que a operação ilegal ocorreu foi: O número de posições entre marcas de tiques. - - TickFrequency: Value '{0}' must be greater than or equal to 0. - TickFrequency: Value '{0}' must be greater than or equal to 0. - - Indicates where the ticks appear on the TrackBar. Indica onde os tiques são exibidos em TrackBar. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.ru.xlf b/src/System.Windows.Forms/Resources/xlf/SR.ru.xlf index f17f569e64e..89af4e75583 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.ru.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.ru.xlf @@ -10789,11 +10789,6 @@ Stack trace where the illegal operation occurred was: Число позиций между отметками на оси. - - TickFrequency: Value '{0}' must be greater than or equal to 0. - TickFrequency: Value '{0}' must be greater than or equal to 0. - - Indicates where the ticks appear on the TrackBar. Указывает, отображаются ли отметки для TrackBar. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.tr.xlf b/src/System.Windows.Forms/Resources/xlf/SR.tr.xlf index 0bb5068a3fc..9e72305a890 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.tr.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.tr.xlf @@ -10789,11 +10789,6 @@ Geçersiz işlemin gerçekleştiği yığın izi: Değer çizgileri arasındaki konum sayısı. - - TickFrequency: Value '{0}' must be greater than or equal to 0. - TickFrequency: Value '{0}' must be greater than or equal to 0. - - Indicates where the ticks appear on the TrackBar. Ölçü çizgilerin TrackBar üzerinde nerede görüneceğini gösterir. diff --git a/src/System.Windows.Forms/Resources/xlf/SR.zh-Hans.xlf b/src/System.Windows.Forms/Resources/xlf/SR.zh-Hans.xlf index 398498856e0..6827f20269f 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.zh-Hans.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.zh-Hans.xlf @@ -10789,11 +10789,6 @@ Stack trace where the illegal operation occurred was: 刻度线间的位置数。 - - TickFrequency: Value '{0}' must be greater than or equal to 0. - TickFrequency: Value '{0}' must be greater than or equal to 0. - - Indicates where the ticks appear on the TrackBar. 指示在 TrackBar 上的哪些位置显示刻度。 diff --git a/src/System.Windows.Forms/Resources/xlf/SR.zh-Hant.xlf b/src/System.Windows.Forms/Resources/xlf/SR.zh-Hant.xlf index cb9fe45b98e..15f5f06c126 100644 --- a/src/System.Windows.Forms/Resources/xlf/SR.zh-Hant.xlf +++ b/src/System.Windows.Forms/Resources/xlf/SR.zh-Hant.xlf @@ -10789,11 +10789,6 @@ Stack trace where the illegal operation occurred was: 刻度間相距的位置數值大小。 - - TickFrequency: Value '{0}' must be greater than or equal to 0. - TickFrequency: Value '{0}' must be greater than or equal to 0. - - Indicates where the ticks appear on the TrackBar. 指定 TrackBar 上刻度的顯示位置。 diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs index 2edc61a2ab3..2d9180e05fa 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs @@ -562,11 +562,6 @@ public int TickFrequency get => _tickFrequency; set { - if (value < 0) - { - throw new ArgumentOutOfRangeException(nameof(value), value, string.Format(SR.TrackBarTickFrequencyError, value)); - } - if (_tickFrequency == value) { return; From 0039431441dc1206fb86af35de4a2abe43a225ef Mon Sep 17 00:00:00 2001 From: "Simon Zhao (BEYONDSOFT CONSULTING INC)" Date: Mon, 6 Jul 2026 13:52:38 +0800 Subject: [PATCH 08/10] Revert changes to unmodified files --- src/System.Windows.Forms/Resources/SR.resx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Windows.Forms/Resources/SR.resx b/src/System.Windows.Forms/Resources/SR.resx index c79934103fd..53d5073b748 100644 --- a/src/System.Windows.Forms/Resources/SR.resx +++ b/src/System.Windows.Forms/Resources/SR.resx @@ -7045,4 +7045,4 @@ Stack trace where the illegal operation occurred was: The FormScreenCaptureMode property can only be changed on top-level Forms with their TopLevel property set to true. - + \ No newline at end of file From 18278a195214d4a146044f7b4db8e41d1c2e69f7 Mon Sep 17 00:00:00 2001 From: "Simon Zhao (BEYONDSOFT CONSULTING INC)" Date: Mon, 6 Jul 2026 15:47:37 +0800 Subject: [PATCH 09/10] Handle high-risk issues --- .../Forms/Controls/TrackBar/TrackBar.cs | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs index 2d9180e05fa..8c211cd31ec 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs @@ -780,23 +780,30 @@ private void DrawTicksManually() } int drawnTickFrequency = _tickFrequency <= 0 ? 1 : _tickFrequency; - // Estimate the maximum number of ticks that can be accommodated using the control's pixel dimensions. - int maxTickCount = (Orientation == Orientation.Horizontal ? Size.Width : Size.Height); + int maxTickCount = Orientation == Orientation.Horizontal ? Size.Width : Size.Height; uint range = (uint)(_maximum - _minimum); - // Calculate the number of ticks to draw based on the frequency. - int exceptionTickCount = range == 0 - ? 1 - : (int)range / drawnTickFrequency; - if (maxTickCount > exceptionTickCount) + + // Remove auto-generated interior ticks first; endpoints remain managed by the native control. + PInvokeCore.SendMessage(this, PInvoke.TBM_CLEARTICS, (WPARAM)1, (LPARAM)0); + + // No interior ticks are possible for empty/1-step ranges, or when there is no drawable size. + if (maxTickCount <= 0 || range <= 1) + { + return; + } + + // Ensure the visual tick count does not exceed available pixels. + // This keeps ticks distributed across the full value range without over-drawing. + int minimumStepForSize = ((int)range + maxTickCount - 1) / maxTickCount; + if (minimumStepForSize > drawnTickFrequency) { - maxTickCount = exceptionTickCount; + drawnTickFrequency = minimumStepForSize; } - PInvokeCore.SendMessage(this, PInvoke.TBM_CLEARTICS, (WPARAM)1, (LPARAM)0); - for (int i = 1; i <= maxTickCount; i++) + // Advance by value across the full track range, adding only interior ticks. + for (long tickValue = _minimum + drawnTickFrequency; tickValue < _maximum; tickValue += drawnTickFrequency) { - int tickValue = Minimum + i * drawnTickFrequency; - LRESULT lresult = PInvokeCore.SendMessage(this, PInvoke.TBM_SETTIC, (WPARAM)0, (LPARAM)tickValue); + LRESULT lresult = PInvokeCore.SendMessage(this, PInvoke.TBM_SETTIC, (WPARAM)0, (LPARAM)(int)tickValue); Debug.Assert((bool)(BOOL)lresult); } } From 07e4a77d5b6d3ac75063ab89602888099e64e952 Mon Sep 17 00:00:00 2001 From: "Simon Zhao (BEYONDSOFT CONSULTING INC)" Date: Mon, 6 Jul 2026 17:11:16 +0800 Subject: [PATCH 10/10] The final decision was to fix it based on the existing code. --- .../PublicAPI.Unshipped.txt | 1 + .../Forms/Controls/TrackBar/TrackBar.cs | 53 ++++++++++++------- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/System.Windows.Forms/PublicAPI.Unshipped.txt b/src/System.Windows.Forms/PublicAPI.Unshipped.txt index e69de29bb2d..d9a512fedca 100644 --- a/src/System.Windows.Forms/PublicAPI.Unshipped.txt +++ b/src/System.Windows.Forms/PublicAPI.Unshipped.txt @@ -0,0 +1 @@ +override System.Windows.Forms.TrackBar.OnSizeChanged(System.EventArgs! e) -> void diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs index 8c211cd31ec..554be258dc1 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/TrackBar/TrackBar.cs @@ -779,31 +779,23 @@ private void DrawTicksManually() return; } - int drawnTickFrequency = _tickFrequency <= 0 ? 1 : _tickFrequency; - int maxTickCount = Orientation == Orientation.Horizontal ? Size.Width : Size.Height; + int drawnTickFrequency = _tickFrequency; + // Divide by 2 because otherwise the ticks appear as a solid line. + int maxTickCount = (Orientation == Orientation.Horizontal ? Size.Width : Size.Height) / 2; uint range = (uint)(_maximum - _minimum); - - // Remove auto-generated interior ticks first; endpoints remain managed by the native control. - PInvokeCore.SendMessage(this, PInvoke.TBM_CLEARTICS, (WPARAM)1, (LPARAM)0); - - // No interior ticks are possible for empty/1-step ranges, or when there is no drawable size. - if (maxTickCount <= 0 || range <= 1) - { - return; - } - - // Ensure the visual tick count does not exceed available pixels. - // This keeps ticks distributed across the full value range without over-drawing. - int minimumStepForSize = ((int)range + maxTickCount - 1) / maxTickCount; - if (minimumStepForSize > drawnTickFrequency) + if (range > maxTickCount && maxTickCount != 0) { - drawnTickFrequency = minimumStepForSize; + int calculatedTickFrequency = (int)(range / maxTickCount); + if (calculatedTickFrequency > drawnTickFrequency) + { + drawnTickFrequency = calculatedTickFrequency; + } } - // Advance by value across the full track range, adding only interior ticks. - for (long tickValue = _minimum + drawnTickFrequency; tickValue < _maximum; tickValue += drawnTickFrequency) + PInvokeCore.SendMessage(this, PInvoke.TBM_CLEARTICS, (WPARAM)1, (LPARAM)0); + for (int i = _minimum + drawnTickFrequency; i <= _maximum - drawnTickFrequency; i += drawnTickFrequency) { - LRESULT lresult = PInvokeCore.SendMessage(this, PInvoke.TBM_SETTIC, (WPARAM)0, (LPARAM)(int)tickValue); + LRESULT lresult = PInvokeCore.SendMessage(this, PInvoke.TBM_SETTIC, lParam: (IntPtr)i); Debug.Assert((bool)(BOOL)lresult); } } @@ -1007,6 +999,27 @@ protected override void OnSystemColorsChanged(EventArgs e) RedrawControl(); } + protected override void OnSizeChanged(EventArgs e) + { + base.OnSizeChanged(e); + + if (!IsHandleCreated) + { + return; + } + + bool recreateHandle = ShouldRecreateHandle(); + if (recreateHandle) + { + RecreateHandle(); + } + else if (!_autoDrawTicks) + { + DrawTicksManually(); + Invalidate(); + } + } + /// /// Overrides Control.SetBoundsCore to enforce auto sizing. ///