i have a listbox and i would want to display a label displaying:
scrolling through items XXX to XYY of ZZZ.
how do I do this because using SelectedIndex will not be useful, as i would like the label to update even when nothing is selected. (scrolling too, it does not select an item).
update:
for example I have 200 items in my listbox. at any one time i can only display only 10 items because of my listbox's height. so the label should read:
displaying items 1 to 10 of 200
or
displaying items 5 to 15 of 200
however i must take into account that there may not be any indices selected because i can simply scroll and not select anything.
You can get the top index value using listbox.TopIndex and the count using listbox.Items.Count but I can't see any way to get the bottom item withotu calculating it from the result of listbox.GetItemHeight() and listbox.ClientSize.Height:
int visibleCount = listBox1.ClientSize.Height / listBox1.ItemHeight;
this.Text = string.Format("{0:d} to {1:d} of {2:d}", listBox1.TopIndex + 1, listBox1.TopIndex + visibleCount, listBox1.Items.Count);
This can be done on a timer as I see no scroll event.
Just use the Scroll event of the ListBox. Oh wait, there isn't one. You can add one:
public class ListBoxEx : ListBox {
public event EventHandler Scrolling;
private const int WM_VSCROLL = 0x0115;
private void OnScrolling() {
if (Scrolling != null)
Scrolling(this, new EventArgs());
}
protected override void WndProc(ref Message m) {
base.WndProc(ref m);
if (m.Msg == WM_VSCROLL)
OnScrolling();
}
}
Once you use this, it's just math (refactor as needed):
private void listBoxEx1_Resize(object sender, EventArgs e) {
DisplayRange();
}
private void listBoxEx1_Scrolling(object sender, EventArgs e) {
DisplayRange();
}
private void DisplayRange() {
int numFrom = listBoxEx1.TopIndex + 1;
int numTo = numFrom + (listBoxEx1.ClientSize.Height / listBoxEx1.ItemHeight) - 1;
this.Text = numFrom.ToString() + " to " + numTo.ToString();
}
If IntegralHeight=False then you might have to play with the range number to determine whether or not to include partial rows or not.
If using DrawMode=OwnerDrawVariable, then you need to loop through the visible rows with the MeasureItem event.
This is a Draw, you can do this
private int min = 1000;
private int max = 0;
private void comboBox3_DrawItem(object sender, DrawItemEventArgs e)
{
if (min >= e.Index) min = e.Index+1;
if (max <= e.Index) max = e.Index+1;
float size = 10;
System.Drawing.Font myFont;
FontFamily family = FontFamily.GenericSansSerif;
System.Drawing.Color animalColor = System.Drawing.Color.Black;
e.DrawBackground();
Rectangle rectangle = new Rectangle(2, e.Bounds.Top + 2,
e.Bounds.Height, e.Bounds.Height - 4);
e.Graphics.FillRectangle(new SolidBrush(animalColor), rectangle);
myFont = new Font(family, size, FontStyle.Bold);
e.Graphics.DrawString(comboBox3.Items[e.Index].ToString(), myFont, System.Drawing.Brushes.Black, new RectangleF(e.Bounds.X + rectangle.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
e.DrawFocusRectangle();
label1.Text = String.Format("Values between {0} and {1}",min,max);
}
you have to find the right event to reset min and max values and the right values to redraw the combobox.
This code is not a solution, it's only an idea of how you can implement your requirement.
best regards.
Related
I'm trying to set the Height of the form while I'm resizing it, if a condition is met. I have it set to only allow the width of the form to be altered manually using the code provided by in this answer.
I have a FlowLayoutPanel displaying a collection of PictureBox controls, each with a fixed Height of 50 pixels. Initially, the form's Height is 38 (Size.Height - ClientSize.Height) + 50 + 6 (Margin.Top + Margin.Bottom of an image) = 94.
If the controls overflow, by default the FlowLayoutPanel pushes them down onto a new line. What I want to do is resize the form when this happens, or when the form width is manually changed which might cause the controls to jump to the next line.
The following code works, and is called whenever a new control is added to the FlowLayoutPanel (itemPanel):
private void ResizeForm()
{
if (itemPanel.Controls.Count < 1) return;
var lastElement = itemPanel.Controls[itemPanel.Controls.Count - 1];
// The Form is the correct size, no need to resize it:
if (lastElement.Bottom + lastElement.Margin.Bottom == itemPanel.Height) return;
Height = 38 + lastElement.Bottom + lastElement.Margin.Bottom;
}
However, when called within my SizeChange event, this method causes the Form to "flash" between the initial Height and the new Height:
private void MainForm_SizeChanged(object sender, EventArgs e)
{
ResizeForm();
}
I'm guessing the reason is because setting Height will fire the SizeChange event again, but I don't know how to resolve this issue. When I print out the values of lastElement.Bottom + lastElement.Margin.Bottom and itemPanel.Height after setting the Height, they are identical, but the code is still somehow reaching that point.
In a nutshell, I want only the form Width to be manually altered, but the Height of the form to change when items are added or the Width is changed, so that all controls inside the FlowLayoutPanel can be viewed.
However, when called within my SizeChange event, this method causes
the Form to "flash" between the initial Height and the new Height
Basically any of the stock "resize" events for your Form will occur too late for you to change the size without it being noticeable.
You'll want to trap the WM_SIZING message:
Sent to a window that the user is resizing. By processing this
message, an application can monitor the size and position of the drag
rectangle and, if needed, change its size or position.
This will allow you to change the size of the Form before it has actually been updated on the screen.
It would look something like this:
public partial class Form1 : Form
{
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
enum HitTest
{
Caption = 2,
Transparent = -1,
Nowhere = 0,
Client = 1,
Left = 10,
Right = 11,
Top = 12,
TopLeft = 13,
TopRight = 14,
Bottom = 15,
BottomLeft = 16,
BottomRight = 17,
Border = 18
}
private const int WM_SIZING = 0x214;
private const int WM_NCHITTEST = 0x84;
public Form1()
{
InitializeComponent();
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
switch (m.Msg)
{
case WM_NCHITTEST:
var result = (HitTest)m.Result.ToInt32();
if (result == HitTest.Top || result == HitTest.Bottom)
m.Result = new IntPtr((int)HitTest.Caption);
if (result == HitTest.TopLeft || result == HitTest.BottomLeft)
m.Result = new IntPtr((int)HitTest.Left);
if (result == HitTest.TopRight || result == HitTest.BottomRight)
m.Result = new IntPtr((int)HitTest.Right);
break;
case WM_SIZING:
// Retrieve the "proposed" size of the Form in "rc":
RECT rc = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT));
// ... do something with "rc" ...
// this is your code (slightly modified):
if (itemPanel.Controls.Count > 0)
{
var lastElement = itemPanel.Controls[itemPanel.Controls.Count - 1];
if (lastElement.Bottom + lastElement.Margin.Bottom != itemPanel.Height)
{
int Height = 38 + lastElement.Bottom + lastElement.Margin.Bottom;
rc.Bottom = rc.Top + Height; // <--- use "Height" to update the "rc" struct
}
}
// Put the updated "rc" back into message structure:
Marshal.StructureToPtr(rc, m.LParam, true);
break;
}
}
}
Give this a try:
private void ResizeForm()
{
this.SuspendLayout(); // Suspends the layout logic until ResumeLayout() is called (below)
if (itemPanel.Controls.Count < 1) return;
var lastElement = itemPanel.Controls[itemPanel.Controls.Count - 1];
// The Form is the correct size, no need to resize it:
if (lastElement.Bottom + lastElement.Margin.Bottom == itemPanel.Height) return;
Height = 38 + lastElement.Bottom + lastElement.Margin.Bottom;
this.ResumeLayout(); // ADD THIS AS WELL
}
This question already has answers here:
How to print values from a DataGridView control
(3 answers)
Closed 8 years ago.
i want to print entire rows of an DGV.
this should be done by extracting the values not taking picture...(if you don't understand please comment below)
is there any way i can do this?
Still facing problem...
https://drive.google.com/file/d/0BxqCNfHpYEJlUVg3VW5aZlpHMlk/view?usp=sharing
Here is an absolutely minimal code example.
It prints all rows of a dgv, including simple headers and footers..
You need to add a PrintDocument printDocument1 to your form.
Also a Button cb_printPreview.
// a few variables to keep track of things across pages:
int nextPageToPrint = -1;
int linesOnPage = -1;
int linesPrinted = -1;
int maxLinesOnPage = -1;
private void printDocument1_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
// for each fresh page:
linesOnPage = 0;
nextPageToPrint++;
// a short reference to the DataGridView we want to print
DataGridView DGV = yourDataGridView;
// I prefer mm, pick your own unit!
e.Graphics.PageUnit = GraphicsUnit.Millimeter;
// I want to print the columns at these fixed positions
// the first one is the left margin,
// the last one a dummy at the right page margin
int[] tabStops = new int[4] {15, 30, 100, 190};
// I want only one column to be right-aligned:
List<int> rightAlignedCols = new List<int>() { 1 };
// we need to keep track of the horizontal position
// this is also the top margin:
float y = 35f;
// we add a little space between the lines:
float leading = 1.5f;
// we will need to measure the texts we print:
SizeF size = Size.Empty;
// we use only one font:
using (Font font = new Font("Consolas", 13f))
{
// a simple header:
e.Graphics.DrawString("List " + printDocument1.DocumentName,
font, Brushes.Black, 50, y);
y += size.Height + 15;
// I loop over the all rows:
for (int row = linesPrinted; row < DGV.Rows.Count; row++)
{
// I print a light gray bar over every second line:
if (row % 2 == 0)
e.Graphics.FillRectangle(Brushes.Gainsboro,
tabStops[0], y - leading / 2, e.PageBounds.Width - 25, size.Height);
// I print all (3) columns in black, unless they're empty:
for (int col = 0; col < DGV.ColumnCount; col++)
if (DGV[0, row].Value != null)
{
string text = DGV[col, row].FormattedValue.ToString();
size = e.Graphics.MeasureString(text, font, 9999);
float x = tabStops[col];
if (rightAlignedCols.Contains(col) )
x = tabStops[col + 1] - size.Width;
// finally we print an item:
e.Graphics.DrawString(text, font, Brushes.Black, x, y);
}
// advance to next line:
y += size.Height + leading;
linesOnPage++;
linesPrinted++;
if (linesOnPage > maxLinesOnPage) // page is full
{
e.HasMorePages = true; // more to come
break; // leave the rows loop!
}
}
e.Graphics.DrawString("Page " + nextPageToPrint, font,
Brushes.Black, tabStops[3] -20, y + 15);
}
}
private void cb_printPreview_Click(object sender, EventArgs e)
{
PrintPreviewDialog PPVdlg = new PrintPreviewDialog();
PPVdlg.Document = printDocument1;
PPVdlg.ShowDialog();
}
private void printDocument1_BeginPrint(object sender,
System.Drawing.Printing.PrintEventArgs e)
{
// create a demo title for the page header:
printDocument1.DocumentName = " Suppliers as of "
+ DateTime.Now.ToShortDateString();
// initial values for a print job:
nextPageToPrint = 0;
linesOnPage = 0;
maxLinesOnPage = 30;
linesPrinted = 0;
}
The actual printing can be triggered from the PrintPreviewDialog.
There is also a PrintDialog and a PrintPreview control for more options.
Obviously one can add many more things, including graphics, multiple fonts etc, but this should give you an idea.. For full tutorials please look refer to the WWW & MSDN!
How can I get PropertyGrid's TextBox from specified field?
I need this TextBox to set Pointer to the end of text.
var num = txtBox.GetCharIndexFromPosition(Cursor.Position);
txtBox.SelectionStart = num + 1;
txtBox.SelectionLength = 0;
So how can I get this TextBox from PropertyGrid?
Also, property in PropertyGrid is read-only.
If what you want is the cursor to be located right after the last character written in the textbox, you can rely on the following code (triggered by the TextChanged Event of the TextBox):
private void txtBox_TextChanged(object sender, EventArgs e)
{
int newX = txtBox.Location.X + TextRenderer.MeasureText(txtBox.Text, txtBox.Font).Width;
int newY = txtBox.Bottom - txtBox.Height / 2;
if (newX > txtBox.Location.X + txtBox.Width)
{
newX = txtBox.Location.X + txtBox.Width;
}
Cursor.Position = this.PointToScreen(new Point(newX, newY));
}
Bear in mind that its Y position is always in the middle.
----- UPDATE AFTER THE KINGKING COMMENT
As far as the code in the question was referred to the TextBox, I focused my answer on the TextBox. Nonetheless, KingKing is right and the PropertyGrid has to be brought into consideration. Below these lines I adapted the code you can find in MSDN for PropertyGrid:
private void Form1_Load(object sender, EventArgs e)
{
PropertyGrid propertyGrid1 = new PropertyGrid();
propertyGrid1.CommandsVisibleIfAvailable = true;
propertyGrid1.Location = new Point(10, 20);
propertyGrid1.Size = new System.Drawing.Size(400, 300);
propertyGrid1.TabIndex = 1;
propertyGrid1.Text = "Property Grid";
this.Controls.Add(propertyGrid1);
propertyGrid1.SelectedObject = txtBox;
}
After txtBox is added to propertyGrid1, its position is updated and thus the original code can be used without any problem.
In summary, the idea is not looking for the TextBox inside the PropertyGrid, but accessing directly the TextBox control (which is added at runtime to the PropertyGrid).
I have developed the following custom combo box to increase the height of the items. Once that is done, a blank space appears at the end of the drop down menu when there is a scrollbar.
How can I correct the issue?
class MyComboBoxXX : ComboBox
{
public MyComboBoxXX():base()
{
this.DrawMode = DrawMode.OwnerDrawVariable;
this.DropDownStyle = ComboBoxStyle.DropDownList;
this.MaxDropDownItems = 5;
this.IntegralHeight = false;
}
protected override void OnMeasureItem(MeasureItemEventArgs e)
{
e.ItemHeight = 40;
this.DropDownHeight = 40 * 5;
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
e.DrawBackground();
var index = e.Index;
if (index < 0 || index >= Items.Count) return;
using (var brush = new SolidBrush(e.ForeColor))
{
Rectangle rec = new Rectangle(e.Bounds.Left, e.Bounds.Top + ((e.Bounds.Height - ItemHeight) / 2), e.Bounds.Width, ItemHeight);
e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, new SolidBrush(this.ForeColor), rec);
}
e.DrawFocusRectangle();
}
}
If you look closely, it appears that the DropDown area has a 1 pixel border at the top, and also at the bottom. You can get rid of the space by adding 2 pixels to the DropDownHeight.
protected override void OnMeasureItem(MeasureItemEventArgs e)
{
e.ItemHeight = 40;
this.DropDownHeight = (40 * 5) + 2; //add 2 pixels to include the border
}
Result:
I think you should reduce the DropDownHeight value to the appropriate number of items if it's less than the max number in your OnMeasureItem method override.
Need to store off the default ItemHeight since changing the DrawMode from Normal causes padding of 2 pixels to be added to the item height. Retrieving the Handle is required prior to the call to ItemHeight since it creates the handle. Without this, the value of the ItemHeight property is not correct.
http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/ComboBox.cs
We have a Custom ComboBox working on a form that displays some shapes instead of text. To do that all I had to do was to override the OnDrawItem function and it displays what we want. Here is a snippet for reference:
protected override void OnDrawItem(DrawItemEventArgs e)
{
base.OnDrawItem(e);
e.DrawBackground();
if (e.Index >= 0)
{
Brush brush = new SolidBrush(Color.LightGray);
int size = this.Height/2;
int origenX = e.Bounds.X + 1;
int origenY = e.Bounds.Y + 3;
System.Drawing.Drawing2D.GraphicsPath path =
new System.Drawing.Drawing2D.GraphicsPath();
switch (e.Index)
{
case 0:
e.Graphics.FillRectangle(brush, origenX, origenY, size, size);
Rectangle r = new Rectangle(origenX, origenY, size, size);
ControlPaint.DrawBorder(e.Graphics, r, Color.Black,
ButtonBorderStyle.Solid);
break;
case 1:
path.AddEllipse(origenX, origenY, size, size);
e.Graphics.FillPath(brush, path);
e.Graphics.DrawPath(Pens.Black, path);
break;
}
}
}
So, if you add that to a form and add a couple of items to your collection all you see is a square and a circle in the drop down.
Ok, so, what I want to do now is add this same combo box to a DataGridView. I know that this control has a DataGridViewComboBoxColumn. I was trying to extend the control, however, I don't see this OnDrawItem function to override. I guess there exists something similar?
Any help would be appreciated.
Thanks!
You need to interperet the DataGridViewComboBox as your custom Combobox.
private void dgTest_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (dgTest.CurrentCell.ColumnIndex == 0) // Which column ever is your DataGridComboBoxColumn
{
// This line will enable you to use the DataDridViewCOmboBox like your
// Custom ComboBox.
CustomComboBox combo = e.Control as CUstomComboBox;
}
}