Hi,

I have 2 questions about the scrollbars on the Form.

1. Is it possible to change the Width of the scrollbar control itself?
(Ex: for the vertical scrollbar in this browser window is about 1 cm.)

2. Is it possible to change the Color of the scrollbar in any way?

Thank You!

Form2::VerticalScroll

I'm guessing you're running Managed C++ code and have a scrollbar control structure implemented in your form.

#include <windows.h>
#include <iostream>

#using <System.DLL>
#using <System.Windows.Forms.DLL>
#using <System.Drawing.DLL>

using namespace System::Windows::Forms;
using namespace System::Drawing;

namespace MyForm
{
      ref class MyForm : public Form
      {
      public:

            HScrollBar^ myhscrollbar;
            VScrollBar^ myvscrollbar;

            void Initialize()
            {
                  myhscrollbar = gcnew HScrollBar;
                  myhscrollbar->Location = Point(0,100);
                  myhscrollbar->Width = 100; //Sets width
                  myhscrollbar->Height = 25; //Sets height
                  Controls->Add(myhscrollbar);

                  //respectively
                  myvscrollbar = gcnew VScrollBar;
                  myvscrollbar->Location = Point(100,0);
                  myvscrollbar->Width = 25;
                  myvscrollbar->Height = 100;

                  Controls->Add(myvscrollbar);
            }
            
            MyForm()
            {
                  Initialize();
	          std::cout<<"Width of hscroll: "<<myhscrollbar->Width<<"\n";
		  std::cout<<"Height of hscroll: "<<myhscrollbar->Height<<"\n\n";
		  std::cout<<"Width of vscroll: "<<myvscrollbar->Width<<"\n";
		  std::cout<<"Height of vscroll: "<<myvscrollbar->Height<<"\n";
            }
      };
}

int main()
{
      Application::Run(gcnew MyForm::MyForm());
      system("pause");
      return 0;
}

As for how to change the color of the scrollbar. I don't know of a way :$ I've tried this on occasions but they seem to have no effect, and it baffles me that they implement such properties with set functionality without actually having effect on the control itself.

Anyway, one way to solve it could be to use custom images, just change background image to some of your own preference.

//operating from same code as above
      myhscrollbar->BackgroundImage = BackgroundImage->FromFile("filepath\\hscrollbar_colored_blue.png");
      myvscrollbar->BackgroundImage = BackgroundImage->FromFile("filepath\\vscrollbar_colored_blue.png");

Thank you for answer!

It works fine. I beleive I have to experiment with the value to have them exactly in place. So I have an example that works for the horizontal scrollbar below.

Form1 has these width and height: (405, 346)

My question is, if I now change the Form1 Height and Width by dragging it in debug mode. The scrollbar will not follow along to cover the Width and Height of the Form.

1. I wonder what technique I will use for this? Now the scrollbar is static in this location.


2. I also wonder now that if I debug this Form, nothing happens when I clicking the scrollbar, it has no functionality. I can click the scrollbar but the form is not moving to the right or left?

public:
     HScrollBar^ myhscrollbar;            


		void Initialize()
		{
			myhscrollbar = gcnew HScrollBar;
			myhscrollbar->Location = Point(0, Form1::Height - 59);
			myhscrollbar->Width = Form1::Width - 7; //Sets width
			myhscrollbar->Height = 25; //Sets height
			Controls->Add(myhscrollbar);
		}

	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) 
	{
			Initialize();
	}

Okay, I can see that you have not properly been introduced to the whole Managed C++ construct. Ah well, I'll show you some easy steps to creating 'events' and then show you some useful links where you can learn more. Alright.

... I can click the scrollbar but the form is not moving to the right or left?

There is nothing to show in the form...! In fact, adding a scrollbar and putting it in a control doesn't do everything for you. You get full control over how you want it to react when it starts scrolling.

Question 1 and 2:

This is quite fun, because to make it functional, we have to implement an event that always makes sure that the scrollbar is the client's width and/or height.

There is never a simple answer when it comes to Managed C++, but lets dive right into the code and look at how we setup an event with the form.

/*It is usually best to add the resize events (functions to be executed) at initialization time so let's do just that*/
void Initialization()
{
   //....
   this->Resize += gcnew EventHandler(this, &MyForm::MyForm_Resize);

   //if outside the form class
   //MyForm->Resize += gcnew EventHandler(&MyForm, &MyForm_Resize);
}

/*Inside our form we declare a function which will execute each time the window resizes
Usually some events are linked to the function name, like MyForm_EVENT, but often when adding new events you can name them whatever you want.*/

void MyForm::MyForm_Resize(Object^ /*obj*/,EventArgs^ /*e*/ )
{
}

Albeit old, still usefull Managed C++ Tutorial - CodeProj
MSDN is the best teacher, if you ask me, as long as you know where to look, MSDN Windows Forms Namespace

Alright, now we can do whatever we want with resize, since we have access to our scrollbars its easy to manipulate them.

The principle to keeping the scrollbars width and/or height the same as the client is essentially to use the client's Rectangle as help.

//....
Drawing::Rectangle rect = MyForm->ClientRectangle;
//where
rect.Bottom; //= height
//and
rect.Right; //= width 
//...

Now then, I'll give you the working code, with commentary in it. Hope it helps you.

#include <windows.h>
#include <iostream>

#using <System.DLL>
#using <System.Windows.Forms.DLL>
#using <System.Drawing.DLL>

using namespace System;
using namespace System::Windows::Forms;
using namespace System::Drawing;

namespace MyForm
{
	ref class MyForm : public Form
	{
	protected:

		void OnScrolling(Object^ o, ScrollEventArgs^ e)
		{
			//Do scrolling events here, like move everything in the form down or up
		}

		/*Similarly you can use the event MouseWheel to add mouse scrolling*/
		void OnMouseScroll(Object^, MouseEventArgs^ e)
		{
			//Select the scrollbar so that it can be affected by scrolling
			myvscrollbar->Select();
                        switch(e->Delta)
			{
				case 120:
					this->Width += 25;
					break;
				case -120:
					this->Width -=25;
					break;	
			}
		}

		void MyForm_Resize(Object^ /*obj*/,EventArgs^ /*e*/ )
		{
			//get the rectangle for the form
			Drawing::Rectangle rect = this->ClientRectangle;

			//adjust the scrollbar to the top and to the left
			myvscrollbar->Top = 0; //at the top
			myvscrollbar->Left = rect.Right - myvscrollbar->Width; //we want to be to the far right, but we also want our scrollbar visible
			myvscrollbar->Height = rect.Bottom - myvscrollbar->Width; //why remove width? to create a nice square in the corner

			//and now we do the same to the horizontal scrollbar
			myhscrollbar->Top = rect.Bottom - myhscrollbar->Height;
			myhscrollbar->Left = 0;
			myhscrollbar->Width = rect.Right-myvscrollbar->Width;
		}
	public:

		VScrollBar^ myvscrollbar;
		HScrollBar^ myhscrollbar;

		void Initialize()
		{
			myvscrollbar = gcnew VScrollBar;
			this->Controls->Add(myvscrollbar);

			myhscrollbar = gcnew HScrollBar;
			this->Controls->Add(myhscrollbar);

			//Event handlers
			//vertical
			myvscrollbar->Scroll += gcnew ScrollEventHandler(this, &MyForm::OnScrolling);
			myvscrollbar->MouseWheel += gcnew MouseEventHandler(this, &MyForm::OnMouseScroll);

			//horizontal
			myhscrollbar->Scroll += gcnew ScrollEventHandler(this, &MyForm::OnScrolling);

			//Form
			this->Resize += gcnew EventHandler(this, &MyForm::MyForm_Resize);
			this->MouseWheel +=gcnew MouseEventHandler(this, &MyForm::OnMouseScroll); //so that we can scroll even though the scroller isn't selected

			//Refit the scrollbars
			MyForm_Resize(this, EventArgs::Empty); 
		}

		MyForm()
		{
			this->Text = "Scrollbars & Events";
			this->Size = Drawing::Size(500,500);
			this->Left = 0;
			this->Top = 0;
			this->StartPosition = FormStartPosition::Manual;
			Initialize();
		}
	};
}

int main()
{
	FreeConsole();
	Application::Run(gcnew MyForm::MyForm());
	return 0;
}

This should give you an insight on how much you can actually manipulate in the form using Managed C++, and I've barely scratched the surface here. Not only can you change the form at runtime, but you also have the access to some input, for instance mousewheel, or keydown. You can do AWESOME stuff here, the only thing that stops you is experience and access to documentation, luckily you can get both on the internet!

I'm not that great at teaching people, but I am great at learning.
I accept construct..ive (hehe) feedback :cool:, there is always a better way I say.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.