自分のブログ名

sheephumanのブログ

ひつじ好きな人間のブログ。

C# SplitContanerで表示(UI)を切り替えるTips(WinFormApp)

タイトルには非常に頭を悩ませました。
別に大した事はしてないです。



WinFormApp(Windowsフォームアプリ)はレガシーな上、バグが残っていたり癖のある挙動をします。
今回はC#で、コード内でOrientationプロパティを切り替えます。3ペイン(3分割表示)です。

至極単純なため、VB等でも同じように動作するでしょう。




概要

  1. splitContainerを1つ配置します。
  2. Panel1側にもう一つsplitContainer2を貼り付けます。
  3. 両方のBoderStyleをFixedSingle(None以外)にします。 💡ポイント
  4. CheckBoxで表示を切り替えます。
  5. 内部でSplitterDistanceプロパティも変えます。


やりたいのはUI(見た目)の切り替えというシンプルなものですが、Win32APIで動作するWinFormAppでは多少の理解が必要です(しかもスキルとはあんまり関係しない)。
ちなみにBoderStyleがNoneのままだと、splitContainerは見えなくなります。





ソースコード(通常版でもコンパイル可能です)
drive.google.com



ちなみに、Visual Studio 2019 Preview .net Coreで動作させています。一部コンポーネントが無いようです。
↓概要(参考にした)
rksoftware.hatenablog.com



動画
www.youtube.com



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class MainForm1 : Form
    {
        public MainForm1()
        {
            InitializeComponent();
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (!checkBox1.Checked)
            {  splitContainer1.Orientation = Orientation.Horizontal;

                splitContainer1.SplitterDistance = 200;


                //((System.ComponentModel.ISupportInitialize)(this.splitContainer1)).EndInit();
                //this.splitContainer1.ResumeLayout(false); ;
            }
            else
            {
               
                splitContainer1.Orientation = Orientation.Vertical;
                splitContainer1.SplitterDistance = 360;
              
            }
        }

       
    }
}

〜