【C#】パネルのスクロールバー設定について

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
からあげ

【C#】パネルのスクロールバー設定について

#1

投稿記事 by からあげ » 8年前

C#でパネルのスクロールバーの設定がうまくいかなくて困っています。
パネルサイズをユーザー入力の設定に変更し、一定サイズを超えていたらスクロールバーを表示する
ようにしたいのですが、スクロールバーが表示されません。
パネルは背景色のみで画像等はありません。

以下コードの問題部分です。
パネルサイズが定数panelMaxShowSizeXとpanelMaxShowSizeYを超えたら
それぞれ水平方向、垂直方向にスクロールバーが表示されるようにしたいです。

コード:

        private void settingPanel()
        {
            /** 非表示パネルを表示状態に設定 */
            mapPanel.Visible = true;
            /** サイズをユーザー入力の値にリサイズ */
            mapPanel.Size = new Size(mapSizeX, mapSizeY);
            /** 背景を白に設定 */
            mapPanel.BackColor = Color.FromArgb(Byte.MaxValue, Byte.MaxValue, Byte.MaxValue);

            /** スクロールバー設定 */
            if (mapPanel.Width > panelMaxShowSizeX)
            {
                mapPanel.HorizontalScroll.Enabled = true;
                mapPanel.HorizontalScroll.Visible = true;
            }
            //if ( mapPanel.Height > panelMaxShowSizeY ) {
            //}
        }
水平方向の時点で表示されないので垂直方向は実装してないです。
C#経験はあまりないので、どなたかご教授頂けると助かります。

Math

Re: 【C#】パネルのスクロールバー設定について

#2

投稿記事 by Math » 8年前

Windows10,VS2015Community-C#http://csi.nisinippon.com/cs01.png
Form1.cs

コード:

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        // メンバー変数宣言-----------------------------------------------------------------
        Panel mapPanel;
        int mapSizeX = 90;
        int mapSizeY = 90;
        int panelMaxShowSizeX = 100;
        int panelMaxShowSizeY = 100;
        public Form1()                     //コンストラクター
        {
            InitializeComponent();         //Form デザイナーの初期化処理
            mapPanel = new Panel();        //Panel インスタンス------------------------------
            this.Controls.Add(this.mapPanel);//Form1にAdd
            mapPanel.Visible = true;       //非表示パネルを表示状態に設定
            mapPanel.Size=new Size(90, 90);//サイズを初期値にリサイズ
                                           //背景を白に設定
            mapPanel.BackColor = Color.FromArgb(Byte.MaxValue, Byte.MaxValue, Byte.MaxValue);
            mapPanel.Location = new Point(20, 50);//------------------------------------------          
        }

        private void button1_Click(object sender, EventArgs e)
        {
            mapSizeX = int.Parse(textBox1.Text);
            mapSizeY = int.Parse(textBox2.Text);
            settingPanel();
        }

        private void settingPanel()
        {
            /** 非表示パネルを表示状態に設定 */
            mapPanel.Visible = true;
            /** サイズをユーザー入力の値にリサイズ */
            mapPanel.Size = new Size(mapSizeX, mapSizeY);
            /** 背景を白に設定 */
            mapPanel.BackColor = Color.FromArgb(Byte.MaxValue, Byte.MaxValue, Byte.MaxValue);

            /** スクロールバー設定 */
            if (mapPanel.Width > panelMaxShowSizeX)
            {
                mapPanel.HorizontalScroll.Enabled = true;
                mapPanel.HorizontalScroll.Visible = true;
            }
            if (mapPanel.Height > panelMaxShowSizeY)
            {
                mapPanel.VerticalScroll.Enabled = true;
                mapPanel.VerticalScroll.Visible = true;
            }
        }

     }
}
Form1.Designer.cs

コード:

namespace WindowsFormsApplication1
{
    partial class Form1
    {
        /// <summary>
        /// 必要なデザイナー変数です。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 使用中のリソースをすべてクリーンアップします。
        /// </summary>
        /// <param name="disposing">マネージ リソースを破棄する場合は true を指定し、その他の場合は false を指定します。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows フォーム デザイナーで生成されたコード

        /// <summary>
        /// デザイナー サポートに必要なメソッドです。このメソッドの内容を
        /// コード エディターで変更しないでください。
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(197, 12);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(12, 16);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(86, 19);
            this.textBox1.TabIndex = 1;
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(104, 16);
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(87, 19);
            this.textBox2.TabIndex = 2;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 261);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.TextBox textBox2;
    }
}
Program.cs

コード:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// アプリケーションのメイン エントリ ポイントです。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

からあげ

Re: 【C#】パネルのスクロールバー設定について

#3

投稿記事 by からあげ » 8年前

パネルの大きさを親フォームより大きくするとスクロールバーがついててもはみ出して表示されるみたいですね...。
それに気づけたのでパネルを重ねる方法でやりたいことができました。ありがとうございました。
作りかけですが一応ソース載せておきます
main

コード:

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;
using System.IO;

namespace MapEditerForBox2DAction
{
    public partial class MainForm : Form
    {
        Panel mapPanel;
        private int mapSizeX;
        private int mapSizeY;

        public MainForm()
        {
            InitializeComponent();
            mapPanel = new Panel();
        }


        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void CallConfig_Click(object sender, EventArgs e)
        {
            Config configForm = new Config();
            configForm.StartPosition = FormStartPosition.CenterParent;
            configForm.ShowDialog(this);

            /** Okボタンで終了時 */
            if(configForm.getIsOk()){
                if ( configForm.getMapData() == null ) {
                    /** マップサイズ受け取り */
                    mapSizeX = configForm.getSizeX();
                    mapSizeY = configForm.getSizeY();
                }else
                {
                    /** マップデータフォーマットチェック */

                    /** マップ読み込み */
                }
                /** パネル設定 */
                settingPanel();
            }
        }

        /** 読み込みデータフォーマットチェック */
        private bool mapDataFormatCheck(StreamReader sr)
        {
            bool result = true;
            return result;
        }

        /** パネルの設定 */
        private void settingPanel()
        {
            /** 非表示パネルを表示状態に設定 */
            mapPanel.Visible = true;
            /** サイズをユーザー入力の値にリサイズ */
            mapPanel.Size = new Size(mapSizeX, mapSizeY);
            /** 背景を白に設定 */
            mapPanel.BackColor = Color.FromArgb(Byte.MaxValue, Byte.MaxValue, Byte.MaxValue);

            parentPanel.Controls.Add(mapPanel);
        }

    }
}
designer

コード:

namespace MapEditerForBox2DAction
{
    partial class MainForm
    {
        /// <summary>
        /// 必要なデザイナー変数です。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 使用中のリソースをすべてクリーンアップします。
        /// </summary>
        /// <param name="disposing">マネージ リソースを破棄する場合は true を指定し、その他の場合は false を指定します。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows フォーム デザイナーで生成されたコード

        /// <summary>
        /// デザイナー サポートに必要なメソッドです。このメソッドの内容を
        /// コード エディターで変更しないでください。
        /// </summary>
        private void InitializeComponent()
        {
            this.fileSystemWatcher1 = new System.IO.FileSystemWatcher();
            this.CallConfig = new System.Windows.Forms.Button();
            this.parentPanel = new System.Windows.Forms.Panel();
            ((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).BeginInit();
            this.SuspendLayout();
            // 
            // fileSystemWatcher1
            // 
            this.fileSystemWatcher1.EnableRaisingEvents = true;
            this.fileSystemWatcher1.SynchronizingObject = this;
            // 
            // CallConfig
            // 
            this.CallConfig.Location = new System.Drawing.Point(698, 21);
            this.CallConfig.Name = "CallConfig";
            this.CallConfig.Size = new System.Drawing.Size(75, 23);
            this.CallConfig.TabIndex = 0;
            this.CallConfig.Text = "マップ設定";
            this.CallConfig.UseVisualStyleBackColor = true;
            this.CallConfig.Click += new System.EventHandler(this.CallConfig_Click);
            // 
            // parentPanel
            // 
            this.parentPanel.AutoScroll = true;
            this.parentPanel.Location = new System.Drawing.Point(13, 13);
            this.parentPanel.Name = "parentPanel";
            this.parentPanel.Size = new System.Drawing.Size(640, 480);
            this.parentPanel.TabIndex = 1;
            // 
            // MainForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(923, 523);
            this.Controls.Add(this.parentPanel);
            this.Controls.Add(this.CallConfig);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Name = "MainForm";
            this.Text = "MapEditer";
            ((System.ComponentModel.ISupportInitialize)(this.fileSystemWatcher1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private System.IO.FileSystemWatcher fileSystemWatcher1;
        private System.Windows.Forms.Button CallConfig;
        private System.Windows.Forms.Panel parentPanel;
    }
}
やりたかったことはこんな感じです。
http://www.dotup.org/uploda/www.dotup.o ... 8.png.html

閉鎖

“C言語何でも質問掲示板” へ戻る