ラベル c# の投稿を表示しています。 すべての投稿を表示
ラベル c# の投稿を表示しています。 すべての投稿を表示

2019年9月7日土曜日

C# グリッド状に配置したTextBoxをカーソルキーで移動したい!

必要に駆られて書いたので覚書
もっとかんたんなほうほうあるんやろか


public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        List textBoxList = new List();

        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (var item in this.Controls)
            {
                if (item is TextBox) textBoxList.Add(item as TextBox);
            }

            textBoxList.ForEach(x => x.KeyDown += X_KeyDown);
            textBoxList.ForEach(x => x.Text = "hogehoge");
        }

        private void X_KeyDown(object sender, KeyEventArgs e)
        {
            var t = sender as TextBox;
            TextBox nextControl;
            switch (e.KeyCode)
            {
                case Keys.Left:
                    if (t.SelectionStart != 0 && t.SelectionLength != t.Text.Length) return;//文字列編集途中なら抜ける

                    nextControl = textBoxList.Where(x => t.Location.X - 5 > x.Location.X)
                        .OrderBy(x => getDistance(t.Location.X, t.Location.Y, x.Location.X, x.Location.Y)).FirstOrDefault();

                    if (nextControl != null)
                    {
                        nextControl.Focus();
                        nextControl.SelectAll();
                    }

                    break;
                case Keys.Up:
                    nextControl = textBoxList.Where(x => t.Location.Y - 5 > x.Location.Y)
                        .OrderBy(x => getDistance(t.Location.X, t.Location.Y, x.Location.X, x.Location.Y)).FirstOrDefault();

                    if (nextControl != null)
                    {
                        nextControl.Focus();
                        nextControl.SelectAll();
                    }
                    break;
                case Keys.Right:
                    if (t.SelectionStart != t.TextLength  && t.SelectionLength != t.Text.Length) return;//文字列編集途中なら抜ける
                    
                    nextControl = textBoxList.Where(x => t.Location.X + 5 < x.Location.X)
                        .OrderBy(x => getDistance(t.Location.X, t.Location.Y, x.Location.X, x.Location.Y)).FirstOrDefault();

                    if (nextControl != null)
                    {
                        nextControl.Focus();
                        nextControl.SelectAll();
                    }
                    break;
                case Keys.Down:
                    nextControl = textBoxList.Where(x => t.Location.Y + 5 < x.Location.Y)
                        .OrderBy(x => getDistance(t.Location.X, t.Location.Y, x.Location.X, x.Location.Y)).FirstOrDefault();

                    if (nextControl != null)
                    {
                        nextControl.Focus();
                        nextControl.SelectAll();
                    }
                    break;
            }

        }

        private double getDistance(double x1, double y1, double x2, double y2)
        {
            return Math.Sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
        }
    }







最初からDataGrid系のコントロールを使えばよかった・・・


2014年3月12日水曜日

C#で9DSRの計算

/// 
/// 9DSR 計算
/// NW-7
/// 
/// /// 
public static string Get9DSR(string Value)
{
    bool result = Regex.IsMatch(Value, @"^[0-9]+$");
    if (!result)
    {
        return null;
    }
    return (ulong.Parse(Value) % 9 == 0 ? 0 : 9 - (int.Parse(Value) % 9)).ToString();
}

C#でルーンズ(モジュラス10/ウェイト2)の計算

/// 
/// ルーンズ(モジュラス10/ウェイト2 計算
/// NW-7
/// 
/// /// 
public static string GetModulus10Weight2Runes(string Value)
{
    bool result = Regex.IsMatch(Value, @"^[0-9]+$");
    if (!result)
    {
        return null;
    }

    long checkDigit = 0;

    for (int i = 0; i < Value.Length; i++)
    {
        if (i % 2 == 0)
        {
            int x = int.Parse(Value.Substring(Value.Length - 1 - i, 1)) * 2;
            //10以上の場合、桁ごとに加算
            if (10 <= x) x = 1 + (x % 10);

            checkDigit += x;
        }
        else
        {
            checkDigit += int.Parse(Value.Substring(Value.Length - 1 - i, 1));
        }
    }

    checkDigit = 10 - (checkDigit % 10);

    checkDigit = checkDigit == 10 ? 0 : checkDigit;

    return checkDigit.ToString();
}

C#で加重モジュラス11の計算

/// 
/// 加重モジュラス11 計算
/// NW-7
/// 
/// /// 
public static string GetWeightedModulus11(string Value)
{
    bool result = Regex.IsMatch(Value, @"^[0-9]+$");
    if (!result || Value.Length > 12)
    {
        return null;
    }

    int[] weight1 = { 2, 6, 3, 5, 4, 8, 7, 10, 9, 5, 3, 6, };
    int[] weight2 = { 9, 5, 8, 6, 7, 3, 4, 10, 2, 6, 8, 5, };

    try
    {
        int checkDigit = 0;
        for (int i = 0; i < 2; i++)
        {
            checkDigit = 0;
            for (int j = 0; j < Value.Length; j++)
            {
                checkDigit += int.Parse(Value.Substring(Value.Length - 1 - j, 1)) * (i == 0 ? weight1[j] : weight2[j]);
            }
            checkDigit = checkDigit % 11;

            if (checkDigit == 0) return "0";
            if (checkDigit != 1) break;
        }

        return (11 - checkDigit).ToString();
    }
    catch
    { }
    return null;
}

C#でモジュラス10/ウェイト2の計算

/// 
/// モジュラス10/ウェイト2 計算
/// NW-7
/// 
/// /// 
public static string GetModulus10Weight2(string Value)
{
    bool result = Regex.IsMatch(Value, @"^[0-9]+$");
    if (!result)
    {
        return null;
    }

    int checkDigit = 0;

    for (int i = 0; i < Value.Length; i++)
    {
        checkDigit += int.Parse(Value.Substring(Value.Length - 1 - i, 1)) * (i % 2 == 0 ? 2 : 1);
    }

    checkDigit = 10 - (checkDigit % 10);

    checkDigit = checkDigit == 10 ? 0 : checkDigit;

    return checkDigit.ToString();
}

C#でモジュラス11の計算

/// 
/// モジュラス11 計算
/// NW-7
/// 
/// /// 
public static string GetModulus11(string Value)
{
    bool result = Regex.IsMatch(Value, @"^[0-9]+$");
    if (!result || 6 > Value.Length)
    {
        return null;
    }

    string val = Value.Substring(0, 6);

    int checkDigit = 0;
    for (int i = 0; i < val.Length; i++)
    {
        checkDigit += int.Parse(val.Substring(i, 1)) * (7 - i);
    }
    checkDigit = checkDigit % 11;

    if (checkDigit == 0)
    {
        return "1";
    }
    else if (checkDigit == 1)
    {
        return "0";
    }
    else
    {
        return (11 - checkDigit).ToString();
    }
}

C#で9DRの計算

/// 
/// 9DR 計算
/// NW-7
/// 
/// /// 
public static string Get9DR(string Value)
{
    bool result = Regex.IsMatch(Value, @"^[0-9]+$");
    if (!result)
    {
        return null;
    }

    return (ulong.Parse(Value) % 9).ToString();
}

C#で7DSRの計算

/// 
/// 7DSR 計算
/// NW-7
/// 
/// /// 
public static string Get7DSR(string Value)
{
    bool result = Regex.IsMatch(Value, @"^[0-9]+$");
    if (!result)
    {
        return null;
    }

    string value =(ulong.Parse(Value) % 7 == 0 ? 0 : 7 - (ulong.Parse(Value) % 7)).ToString();

    return value;
}

C#でモジュラス16の計算

/// 
/// モジュラス16 計算
/// NW-7
/// 
/// /// 
public static string GetModulus16(string Value)
{
    bool result = Regex.IsMatch(Value, @"^[0-9|\-|$|:|/|・|+|A-D]+$");
    if (!result)
    {
        return null;
    }

    long digit = 0;
    for (int i = 0; i < Value.Length; i++)
    {
        digit += Array.IndexOf(modulus16CharList, Value[i]);
    }

    string value = modulus16CharList[(digit % 16) == 0 ? 0 : 16 - (digit % 16)].ToString();

    return value;
}

C#で7DRの計算

/// 
/// 7DR 計算
/// NW-7
/// 
/// /// 
public static string Get7DR(string Value)
{
    bool result = Regex.IsMatch(Value, @"^[0-9]+$");
    if (!result)
    {
        return null;
    }

    string value = (ulong.Parse(Value) % 7).ToString();

    return value;
}

2013年7月9日火曜日

C#でモジュラス43の計算

バーコードのCode39で使うチェックディジットです

private static char[] modulus43CharList =
{
    '0','1','2','3','4','5','6','7','8','9',
    'A','B','C','D','E','F','G','H','I','J',
    'K','L','M','N','O','P','Q','R','S','T',
    'U','V','W','X','Y','Z','-','.',' ','$',
    '/','+','%'
};

/// 
/// モジュラス43 計算
/// CODE39
/// 
public static string GetModulus43(string Value)
{
    if (!Regex.IsMatch(Value, @"^[A-Z|0-9|\-|.| |$|/|+|%]+$"))
    {
        throw new FormatException();
    }

    long x = 0;
    for (int i = 0; i < Value.Length; i++)
    {
        x += Array.IndexOf(modulus43CharList, Value[i]);
    }

    return modulus43CharList[x % 43].ToString();
}

C#でモジュラス10/ウェイト3の計算

だいぶ前に書いたバーコード作成ソフトのコードを見直してます。
ソースもさらしていきます

JANコードなどで使うチェックディジットです

Modulus10W3コード
/// 
/// モジュラス10/ウェイト3 計算
/// JAN8 JAN13 ISBN13 ITF NW-7
/// 
public static string GetModulus10Weight3(string Value)
{
    if (!Regex.IsMatch(Value, @"^[0-9]+$"))
    {
        throw new FormatException();
    }

    int x = 0;
    for (int i = 0; i < Value.Length; i++)
    {
        x += int.Parse(Value[Value.Length - 1 - i].ToString()) * ((i % 2 == 0) ? 3 : 1);
    }

    x = (10 - (x % 10)) % 10;

    return x.ToString();
}


XMLコメントが入るとおかしくなるなあ
タグが消えたりしてしまう なんでだろう

2011年6月3日金曜日

C#でバーコード

1次元バーコードの描画処理を作ってみました。
バーコードの解説サイトを見ながら、
Code39、Code128、ISBN13、JAN13、JAN8、JapanesePostal(カスタマーバーコード)を実装。

JapanesePostalは手持ちのバーコードリーダーで読めないため、目視によるチェック
自信なし。

Code128は制御コードも入力できるようにしてみた。
フリーでは制御コード対応しているものが見つけれず、
参考にするものが無いため、うまく動いているのかわからない><

簡易的な出力ツールを作ってみました。
Code128で使えるコード表も置いておきます。




バグあったらコメントお願いします><


2011年4月12日火曜日

C# 円錐形グラデーション

円錐形のグラデーションの描画方法が分からなかったので、
書いてみた。

本当はもっと簡単な方法があるのかもしれない。



2011年4月8日金曜日

C# RGB ⇔ CIEXYZ ⇔ CIELAB 相互変換

RGB⇔CIEXYZ⇔CIELABの相互変換できる構造体を作成した

//RGBからL*a*b*へ
CIELAB Lab = new CIELAB(Color.Blue);
//L*a*b*からRGBへ
Color col = Lab.ToColor();
//RGBからCIEXYZへ
CIEXYZ xyz = new CIEXYZ(Color.Blue);
//CIEXYZからRGBへ
Color col = xyz.ToColor();
//L*a*b*からXYZへ
CIEXYZ xyz = new CIEXYZ(Lab);
//XYZからL*a*b*へ
CIELAB Lab = new CIELAB(xyz);

こんなかんじで。


2011年4月7日木曜日

C# RGB ⇔ HSV 相互変換 改

RGB⇔HSVができるHSVの構造体です。

コード

C# RGB⇔HSVの交互変換

ペインターやフォトショのカラーピッカーに憧れがあるため、

ういきぺでぃあのHSVの項目
を元にC#でRGBtoHSVの交互変換を行えるクラスを書いてみました。
Hは0~360° SとVは0~255になってます。




コード
//RGBからHSV
HSV hsv = new HSV(Color.Blue);
//HSVからRGB
Color col = hsv.ToColor();
  
こんなかんじで。

C# ヒストグラム表示のコントロール



画像のヒストグラムを表示します。
Imageに画像データをセット

コード

2010年4月14日水曜日

カラーピッカーのライブラリ化

最近仕事と別件で忙しく、こうしん出来ず。
べっ べつに飽きたんじゃなからね!

とりあずカラーピッカーしか作ってなかったので成果物としてうpします。

dotnet2.0以降対応

http://ux.getuploader.com/tekitou000/download/2/DOTNET2.0%E3%80%80%E3%82%AB%E3%83%A9%E3%83%BC%E3%83%94%E3%83%83%E3%82%AB%E3%83%BC%E3%83%A9%E3%82%A4%E3%83%96%E3%83%A9%E3%83%AAv1.00.zip

うんこでごめんね><