C#|TextBoxにもマウスジェスチャー機能を簡単に追加

 C#|WebBrowserにマウスジェスチャー機能を簡単に追加のTextBox版を書きました。コンテキストメニューの表示設定の切り替えのタイミング以外は元のコードと殆ど同じです。このコンテキストメニューの表示設定の切り替えタイミングの試行錯誤に作業時間の殆どを費やしました。

 これをしないと、マウスジェスチャが終わった後にメニューが開いちゃうんです。で、開かないようにしたら、二度と開かなかったりします。この微妙なタイミングが難しかったです。

 なんでTextBox用にも書いたかと言うと、VB.NETで書いた電八用のビューアにマウスジェスチャ機能を付けたかったからです。↓で閉じる、これだけの為にです。

 ここでVisualStudioだとVBのプロジェクトを参照設定してスタートアッププロジェクトに設定すれば楽なのでしょうが、僕が使っているのはC#、VBそれぞれのExpress Editionです。複数の言語を同時には扱えないので、C#でビルド→VBでテストという繰り返しでした。VSが欲しいです。MSはアカデミックパックに加えてNEETパックとかフリーターパックでも販売するべきです。

 C#とVB.NETを交互に書くのは不思議な感じでした。

C#|どう書く?org 文字列の八方向検索2

 他の皆のコードよりかなり長かったので、短くしてみました。半分になりました。処理時間は倍になった気がします。

 他の皆の他言語のコードも読めれば参考に出来るのですが、難しいです。

//http://ja.doukaku.org/
//http://ja.doukaku.org/99/投稿用
using System;
using System.Collections.Generic;
namespace どう書く_org文字列の八方向検索 {
  class Program {
    static string search;
    static List<string> list;
    static void Main(string[] args) {
      string sample = "リオウウリウ\nウオリウオリ\nオリリオリウ\nリリオオウオ";
      search = "ウオリ";
      char sp = new char { '\n' };
      int width = sample.Split(sp)[0].Length;
      int height = sample.Split(sp).Length;
      list = new List<string>(sample.Split(sp));
      for(int y = 0; y < list.Count; y++) { //縦ループ
        for(int x = 0; x < list[y].Length; x++) { //横ループ
          for(int dx = -1; dx <= 1; dx++) { //左右方向ループ
            for(int dy = -1; dy <= 1; dy++) { //上下方向ループ -1は上、左 1は下、右を表す
              try {
                string strb = "";
                for(int i = 0; i < search.Length; i++) { //iは移動量
                  strb += list[y + i * dy][x + i * dx]; //移動方向をdy,dxで乗算することで反転
                }
                if(search == strb) {
                  string directionStr = "";
                  if(dx < 0) {
                    directionStr += "左";
                  } else if(dx > 0) {
                    directionStr += "右";
                  }
                  if(dy < 0) {
                    directionStr += "上";
                  } else if(dy > 0) {
                    directionStr += "下";
                  }
                  Console.WriteLine("(" + x + "," + y + ")" + "," + directionStr);
                }
              } catch(ArgumentOutOfRangeException) { } catch(IndexOutOfRangeException) { }
            }
          }
        }
      }
      Console.ReadLine();
    }
  }
}

C#|論理エラーでロンリー

 お手製アドレス帳のCSVインポート機能のコードを書いていたのですが、

if(hoge.Text.Contains(“ZONU”)){

  hoge.Text = hoge.Text.Replace(“ZONU”, “DQN”);

  huga.Test = hoge.Text;

}

みたいなコードを書いて3行目のコードは動いて、2行目では置換されないってどういうことですか…?二日三日悩んでどうにもならなくてどうでも良くなりました。

 Vectorでも漁ってればそのうち良いアドレス帳が見付かるでしょう。

C#|どう書く?org 文字列の八方向検索

 どう書く?orgのお題、文字列の八方向検索投稿用に書きました。

using System.Collections.Generic;
using System.Text;
using Microsoft.VisualBasic;
namespace どう書く_org文字列の八方向検索 {
  class Program {
    static string search;
    static List<string> list;
    static void Main(string[] args) {
      string sample =
@"リオウウリウ
ウオリウオリ
オリリオリウ
リリオオウオ";
      search = "ウオリ";
      char sp = new char { '\n' };
      int width = sample.Split(sp)[0].Length;
      int height = sample.Split(sp).Length;
      list = new List<string>(sample.Split(sp));
      for(int y = 0; y < list.Count; y++) {
        for(int x = 0; x < list[y].Length; x++) {
          if(list[y][x] == search[0]) {
            if(E(x, y) == search) {
              Console.WriteLine("(" + x + "," + y + ")" + "," + "右");
            }
            if(W(x, y) == search) {
              Console.WriteLine("(" + x + "," + y + ")" + "," + "左");
            }
            if(S(x, y) == search) {
              Console.WriteLine("(" + x + "," + y + ")" + "," + "下");
            }
            if(N(x, y) == search) {
              Console.WriteLine("(" + x + "," + y + ")" + "," + "上");
            }
            if(NE(x, y) == search) {
              Console.WriteLine("(" + x + "," + y + ")" + "," + "右上");
            }
            if(SE(x, y) == search) {
              Console.WriteLine("(" + x + "," + y + ")" + "," + "右下");
            }
            if(NW(x, y) == search) {
              Console.WriteLine("(" + x + "," + y + ")" + "," + "左上");
            }
            if(SW(x, y) == search) {
              Console.WriteLine("(" + x + "," + y + ")" + "," + "左下");
            }
          }
        }
      }
      Console.ReadLine();
    }
    static string E(int x, int y) {
      try {
        return list[y].Substring(x, search.Length);
      } catch(ArgumentOutOfRangeException) { return ""; }
       catch(IndexOutOfRangeException) { return ""; }
    }
    static string W(int x, int y) {
      try {
        return Strings.StrReverse(E(x - search.Length + 1, y));
      } catch(ArgumentOutOfRangeException) { return ""; }
       catch(IndexOutOfRangeException) { return ""; }
    }
    static string S(int x, int y) {
      try {
        StringBuilder strb = new StringBuilder();
        for(int i = 0; i < search.Length; i++) {
          strb.Append(list[y + i][x]);
        }
        return strb.ToString();
      } catch(ArgumentOutOfRangeException) { return ""; }
       catch(IndexOutOfRangeException) { return ""; }
    }
    static string N(int x, int y) {
      try {
        return Strings.StrReverse(S(x, y - search.Length + 1));
      } catch(ArgumentOutOfRangeException) { return ""; }
       catch(IndexOutOfRangeException) { return ""; }
    }
    static string SE(int x, int y) {
      try {
        StringBuilder strb = new StringBuilder();
        for(int i = 0; i < search.Length; i++) {
          strb.Append(list[y + i][x + i]);
        }
        return strb.ToString();
      } catch(ArgumentOutOfRangeException) { return ""; }
       catch(IndexOutOfRangeException) { return ""; }
    }
    static string NW(int x, int y) {
      try {
        return Strings.StrReverse(SE(x - search.Length + 1, y - search.Length + 1));
      } catch(ArgumentOutOfRangeException) { return ""; }
       catch(IndexOutOfRangeException) { return ""; }
    }
    static string NE(int x, int y) {
      try {
        StringBuilder strb = new StringBuilder();
        for(int i = 0; i < search.Length; i++) {
          strb.Append(list[y - i][x + i]);
        }
        return strb.ToString();
      } catch(ArgumentOutOfRangeException) { return ""; }
       catch(IndexOutOfRangeException) { return ""; }
    }
    static string SW(int x, int y) {
      try {
        return Strings.StrReverse(NE(x - search.Length + 1, y + search.Length - 1));
      } catch(ArgumentOutOfRangeException) { return ""; }
       catch(IndexOutOfRangeException) { return ""; }
    }
  }
}