【VisualStudio】他フォームが呼べない場合の対処

外部からインポートしたプロジェクトで
他フォームをShowやShowDialogで開く際、
FileNotFoundが発生している場合、プロジェクトフォルダ内の「App.config」を見直す。

外部ライブラリ等を参照している場合、パスが記載されていることがあり、
そのパスに外部ライブラリがないことで発生している。

【C#】WebBrowser読み込み完了後にCookieを取得しWebClientに引き渡す

WindowsForm上にWebBrowserを表示し、
読み込んだサイトのCookieをWebBrowserからWebClientに引き渡して処理を行います。

# Form1.cs
namespace WindowsFormsApplication1
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            webBrowser1.Navigate("http://test1/");
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            // クッキーを取得する。
            string cookieStr = webBrowser1.Document.Cookie;

            // WebClientを生成する。
            WebClient wc = new WebClient();
            Encoding enc = Encoding.UTF8;

            // WebClientのヘッダ設定を行う。
            // wc.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows XP)");
            wc.Headers[HttpRequestHeader.Cookie] = cookieStr;

            // WebClientでクッキーを引き継いで他のサイトにアクセスする。
            byte[] result = wc.DownloadData("http://test2/");
            string html = enc.GetString(result);
            Console.WriteLine(html);

            // クッキーの中身を確認する。
            // string[] cookieAry = cookieStr.Split(';');
            // foreach (string str in cookieAry)
            // {
            //    MessageBox.Show(str);
            // }
        }
    }
}

【VisualStudio】オブジェクトにイベント追加

WindowsForm上にあるオブジェクトにイベントを追加します。
ボタンであればデザイナ上でボタンをダブルクリックすれば
イベント追加の設定が自動で行われますが、他のオブジェクトの場合の追加方法です。

オブジェクトをアクティブにしたら右下のプロパティの稲妻マークをクリックします。
ここに表示されているものが定義できるイベントの一覧で
白いテキストボックスをクリックするとイベント追加の設定が自動で行われます。

例として、WindowsForm上に「WebBrowser」を配置し、
WebBrowserでページ読み込み完了後を検知して処理を行いたい場合、
オブジェクトをアクティブにしたら右下のプロパティの稲妻マークをクリックし、
「DocumentCompleted」を選択すると、
イベント追加の設定が自動で行われ、「webBrowser1_DocumentCompleted」が生成されます。

# Form1.Designer.cs
namespace WindowsFormsApplication1
    partial class Form1
    {
        private void InitializeComponent()
        {
            this.webBrowser1.Location = new System.Drawing.Point(52, 193);
            this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
            this.webBrowser1.Name = "webBrowser1"
            this.webBrowser1.Size = new System.Drawing.Size(508, 213);
            this.webBrowser1.TabIndex = 2;
            this.webBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted);
        }
    }
}
# Form1.cs
namespace WindowsFormsApplication1
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            // ここにページ読み込み完了後の処理を記載する。
        }
    }
}

【VisualStudio】SQLServerCompact4.0導入

ローカルDBと呼ばれるSQLServerCompact4.0を使えるようにします。
SQLServerCompact4.0はVisual Studio 2013でサポートを打ち切ったようですが、
ツールを導入すれば利用できるようになります。

「ツール」-「拡張機能と更新プログラム」を選択し、
「オンライン」を選択し、右側の検索テキストボックスで
「SQL Server Compact / SQLite Toolbox」を入力し、検索する。

検索結果でHITした「SQL Server Compact / SQLite Toolbox」を選択し、
「ダウンロード」ボタンを押下する。

インストール後、Visual Studio 2015を再起動し、
サーバーエクスプローラーを開く。
サーバーエクスプローラーのミニツールバーの右側に追加された
「SQL Server Compact / SQLite Toolbox」のアイコンをクリックする。

「Data Connections」を選択し、右クリックし、
「Add SQL Server Compact 4.0 Connection」を選択する。
Filenameで任意のDB名を入力し、Passwordに任意のパスワードを入力し、
「Create」ボタンを押下する。
「Test Connection」ボタンを押下し、接続確認できれば問題なし。

作成したDB名.sdfを選択し、右クリックし、
「Build Table」選択し、テーブルを作成する。

このようにサーバーエクスプローラーと同じ感覚で
SQL Server Compactに対して操作が可能となる。

【VisualStudio】VisualStudio導入

  • VisualStudio導入
    Visual Studio Professional 2015と同機能を持つ
    個人専用のVisual Studio Community 2015を導入しました。
    公式サイトからダウンロードし、画面の指示に従い、
    カスタムで以下をインストールしました。

    Windows and Web Development - Microsoft SQL Server Data Tools
    Windows and Web Development - Microsoft Web Developer Tools
    Common Tools - Visual Studio Extensibility Tools Update 2
    

    インストール後、アカウントのSign inを促されますが、
    スキップしたまま起動できます。
    初期状態だと英語表示なので一旦終了し、日本語パッチを適用します。

    日本語パッチ適用後、Visual Studio Community 2015を再度起動し、
    「Tools」-「Options」-「Environment」-「International Setings」を選択し、
    プルダウンから「日本語」を選択し、
    再度、Visual Studio Community 2015を再起動します。

    補足ですが、Visual Studio Community 2015をインストールすると
    2つの統合開発環境(IDE)がインストールされます。
    Windowsの左下のメニューから確認できます。

    Blend for Visual Studio 2015
    Visual Studio 2015
    

    Blend for Visual Studio 2015はXAMLを使った開発でUIデザインを行うのに適したツールらしいです。
    また、同じプロジェクトファイルを参照できるという親和性があるとか。
    通常であれば、Visual Studio 2015で十分です。
    Blend for Visual Studio 2015はデータベースエクスプローラーがなかったりで
    しばらく困惑しました。