星期日, 5月 29, 2022

[C#] AutoSize

公司內自訂 TextBox 控件存在一個問題,字型大小變化後控件不會自動縮放,所以字型只能維持在 12,一旦字型大小變化後,Text 文字就會破版,花時間研究發現,原來是 AutoSize 被關閉造成

AutoSize 官方文件說明
指出當指定給控制項的字型變更時,控制項的高度是否要自動調整。
建立一個 UCTextBox 來模擬 AutoSize 被關閉情況
using System.Windows.Forms;

namespace UCAutoSizeSample
{
    public class UCTextBox : TextBox
    {
        public UCTextBox()
        {
            AutoSize = false; // 關閉 AutoSize
        }
    }
}
在設計階段的控件示意圖:
  • 左側:原生 TextBox,TextBox 高度會隨著字型大小變化,寬度則是不會
  • 右側:把 AutoSize 關閉的自訂 TextBox 控件,隨著文字變大,Text 文字就破版啦
[C#] AutoSize

發現該問題點時有點傻眼,因為 TextBox.AutoSize 有兩個 Attribute 設定值
  • BrowsableAtttribute(false):在屬性視窗內不可見
  • EditorBrowsable(EditorBrowsableState.Never):編輯器內也沒有 Intellisence 支援,key 的時候發現的
這種情況下還能把它關閉,真的是非關不可,Orz

星期五, 5月 27, 2022

[C#] UseCompatibleTextRendering

在自家某自訂控件內發現有開啟 UseCompatibleTextRendering 屬性,在官方文件 - Application.SetCompatibleTextRenderingDefault(Boolean) Method 備註說明查到該說明
The UseCompatibleTextRendering property is intended to provide visual compatibility between Windows Forms controls that render text using the TextRenderer class and .NET Framework 1.0 and .NET Framework 1.1 applications that perform custom text rendering using the Graphics class. In most cases, if your application is not being upgraded from .NET Framework 1.0 or .NET Framework 1.1, it is recommended that you leave UseCompatibleTextRendering set to the default value of false.
沒有向前相容需求就順道把它改回預設值就好,另外發現原來有全域設定方式,以往開啟 WinForm 程式都沒有注意過這細節
static class Program  
{  
    [STAThread]  
    static void Main()  
    {  
        Application.EnableVisualStyles();  
        Application.SetCompatibleTextRenderingDefault(false); // 全域設定
        Application.Run(new Form1());  
    }  
}  

星期五, 5月 20, 2022

[LINQ] Aggregate

每次看 Aggregate 文件時都會感到眼花撩亂,不知道在寫甚麼,透過官方文件內容來練習並對應文字說明

Aggregate() 多載 1

Applies an accumulator function over a sequence.
public static TSource Aggregate<TSource> 
(
	this System.Collections.Generic.IEnumerable<TSource> source, 
	Func<TSource,TSource,TSource> func
);
string sentence = "the quick brown fox jumps over the lazy dog";
sentence.Split(' ').Aggregate(
	(workingSentence, next) => next + " " + workingSentence)
	.Dump("官方範例:反轉單字");
[LINQ] Aggregate-1

Aggregate() 多載 2

Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value.
public static TAccumulate Aggregate<TSource,TAccumulate> 
(
	this System.Collections.Generic.IEnumerable<TSource> source, 
	TAccumulate seed, 
	Func<TAccumulate,TSource,TAccumulate> func
);
int[] ints = { 4, 8, 8, 3, 9, 0, 7, 8, 2 };
ints.Aggregate(
	0,
	(total, next) => next % 2 == 0 ? total + 1 : total)
	.Dump("官方範例:計算偶數");
[LINQ] Aggregate-2

Aggregate() 多載 3

Applies an accumulator function over a sequence. The specified seed value is used as the initial accumulator value, and the specified function is used to select the result value.
public static TResult Aggregate<TSource,TAccumulate,TResult> 
(
	this System.Collections.Generic.IEnumerable<TSource> source, 
	TAccumulate seed, 
	Func<TAccumulate,TSource,TAccumulate> func, 
	Func<TAccumulate,TResult> resultSelector
);
string[] fruits = { "apple", "mango", "orange", "passionfruit", "grape" };
fruits.Aggregate(
	"banana",
	(longest, next) => next.Length > longest.Length ? next : longest,
	fruit => fruit.ToUpper())
	.Dump("官方範例:找出最常字串並轉為全大寫");
[LINQ] Aggregate-3

觀察練習

把上述 [官方範例:計算偶數] 範例拿來修改,觀察不同資料型態使用
string[] demo = { "4", "8", "8", "3", "9", "0", "7", "8", "2" }; // 故意弄成字串陣列
demo.Aggregate(
	Convert.ToInt64(0), // seed 會影響 total 資料型別
	(total, next) => Convert.ToInt64(next) % 2 == 0 ? total + 1 : total,
	finalResult => DateTime.Today.AddDays(finalResult)
	.Dump("觀察練習"));
[LINQ] Aggregate-5 

上述範例執行結果

[LINQ] Aggregate-4