Any idea how to determine if the vertical scroll bar is active in a multiline textbox?

Active as in, the user can click the arrows and the text is long enough to scroll. Similiar to being enabled/disabled?

I'm trying to have a mouse event so when the user moves their mouse over a specific text box and if the scroll bar is active, after a second or so, a small transparent image appears in the bottom right side of the textbox indicating if the user clicks that image the text in the box will open in a larger window for them to view and resize instead of scrolling.

Recommended Answers

All 7 Replies

I could not find any built in way to deal with this. If you know how many lines your textbox can hold before it has to enable the scrollbars you can use TextBox.Lines.Length and see if there are more lines than you can display.

Momerath's solution is probably the cleanest but just for completeness this is an alternative.
It works when word wrap is on or off.

private void textBox3_TextChanged(object sender, System.EventArgs e)
{
	// get graphics for textbox
	var g = textBox3.CreateGraphics();
	SizeF textSize;
	if (textBox3.WordWrap)
		// get size of text when word wrap is on
		textSize = g.MeasureString(textBox3.Text, textBox3.Font, textBox3.ClientRectangle.Width);
	else
		// get size of text when word wrap is off
		textSize = g.MeasureString(textBox3.Text, textBox3.Font);
	// show button (or what ever) when size is not inside textbox client area
	// modify test area to RectangleF to get best accuracy
	button3.Visible = !((RectangleF)textBox3.ClientRectangle).Contains((PointF)textSize);
}

private void button3_Click(object sender, System.EventArgs e)
{
	// crude full text display :)
	MessageBox.Show(textBox3.Text);
}

Hmmm the textbox has a top, left and right anchor to the form, so it's width grows and shrinks with the form size, but not the height.

I considered the number of characters, but if someone has a higher dpi screen resolution that could change, could it not? I'm not sure about Nick's idea, I'll have to look at that one too...hmmm

Works fine with anchors if you attach the test code to the Resize event also.
[Edit] Better if you rename the handler if you attach the same handler to both TextChanged and Resize.[/Edit]

Awesome! I'll give it a shot. Thanks! :)

Perhaps you could have a look at the Visible property of the scrollbar, look here for examples.

The problem with that though, is the scroll bar is always visible, even if the textbox is blank, it's just disabled.

But I'll look at that one too. Thanks!

You guys are always so helpful :)

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.