Scrolling in DataGridView causes overwritten text - c#

I have a scrollable DataGridView with text and images. But if it contains more rows then fit the screen scrolling doesn't work properly. If you scroll the part of the text that should become hidden remains on top of the new text. So the top and the bottom of my DataGridView contains double text written on top of each other. The middle part is fine, though.
After the creation of the view I resize both the textsize and the cell size. That might be the problem. I honestly don't really know where to start loking for a solution. Only thing I can come up with is to try a repaint on a scroll event. I've tried this similar issue, but it didn't fix it for me. I also see that a lot of people have issues if they paint in the datagridview, but I don't (So please don't mark this as a duplicate of one of those issues). I didn't write this part of the code, but the picture is just passed on by setting it in DataGridViewCellFormattingEventArgs.Value in dgvAlarms_CellFormatting.
When I tried making it DoubleBuffered everything except the headers and the images (that I cut out in the last picture) in the DataGridView turned black like this:
Thank you for your help.

Ok, I got It to work with a combination of TaW's anwser in the comment and adding
e.CellStyle.BackColor = Color.White;
e.CellStyle.SelectionBackColor = Color.White;
to the beginning of the CellFormatting method.
Funny that the backgroundcolor gets set to black if you use the double buffer, but remains white (like I defined) if you I do not.

Related

Visual Studio 2017 Community button size greater than images in image list

I'm trying to precisely align an array of buttons within the rows and columns of a table layout panel. Each button is assigned to the same image list. The image list contains three images, all the same pixel dimensions (200px wide X 18px high). I want the buttons to exactly stack one on top of one another vertically, with no vertical gaps.
The problem is, when I look at the button properties, the size of the image is described as 206x24. It looks like somewhere VS is adding a 3 pixel boundary around the images, and I can't figure out how to get rid of that.
Margins for the button are all set to zero. Padding for the button is all set to zero. FlatStyle is set to "Flat".
What button properties do I set to get the images to stack exactly?
Is there a parameter in the table layout panel that might be causing this?
Here is an image of the button property box:
Image of property box
Any help would be much appreciated.
Would using a picturebox with a clickable change of image work better than a button?
You've only talked about your button styling. Look at padding and margins for your table elements, such as TD, TR, etc. A quick way to see this visually is to add colors for these elements in css. If you make a TD red and see a 3 pixel red space, you've got your culprit. Try this with ANY elements that are in your markup. ANYTHING could be a culprit.
BTW, I've noticed a downvote on your post. I think folks here appreciate if you post your code so they can see what you've tried so far. Just a friendly reminder.
I continued researching the best way to handle the look I was going for, and others that have tried to use buttons, and had similar problems. I have abandoned the button, and will use a picturebox instead in each of the cells of the table view control.
My apologies for the imprecise question.

Screen has flickering

I have a problem with screen flickering. I read some other topics on this case, but there are solutions that didn't work for me, I think that's because I don't know what exactly causes the problem.
My screen is has a large number of controls, maybe this is what causes the problem. I'll try to describe it as best, as I can.
First of all, I am using WinForms.
I am making a video game, so the screen should be maximized all the time.
To allow stretching all the controls I am using TablePanels, one large that is docked to fill the whole form and a few smaller that also dock fill the large Table cells. In smaller cells of those tables, Buttons are docked fill.
To show the background drawn buttons, I made control buttons completely transparent. It needs to stay that way.
The screen flickers white at positions of TableLayoutPanels borders.
The screen flickers when a mouse enters the position of a Button, any Button, no matter where it is located.
For now, only one element changes actively during gameplay - a Label. When mouse enters the field of button, this label shows what this button does. For example if I enter the area of "Use" button, the label displays the word "USE".
I haven't tried that yet, but I must implement, that some images of button will change or become transparent, or lose transparency during game-play. Like there could be one image for closed cupboard, but when player opens it, another image of open cupboard appears. I think I know how to do it, all I want is to prevent flickering.
If you suggest using some code (and I expect it will be needed), please specify where I should put it.
It seems problem was solved.
I'll answer my question myself, in case someone else needs it.
The problem was caused by one of labels, although I can't guess why.
I am using a number of TableLayoutPanels, one of them fills the whole form. It has a number of rows, each of them is also a TableLayoutPanel, or just a Panel.
Flickering appeared when I tried to put a label inside main TableLayoutPanel by its own, and not in a sub panel. When I put a Panel inside main Table, and label inside it, the majority of flickering was gone.
To remove it completely, I used both recommendations just in case:
1) DoubleBuffering: Here (answer from Fabjan)
2) Article also recommended by Fabian
After that, only a few minor glitches remained. I did a few tests and it seems that the last glitches happened when mouse left the screen, then returned back inside. My game screen is maximized, so it took some time to check. To fix it, I used an easy command:
"Cursor.Clip = this.Bounds;" on FormLoad
I learned it from another question from this site: Here
Unfortunately, I didn't understand, why misplaced label was behind flickering, but since the problem was solved, I decided to write it here, in case someone else needs it.

Resizing label in Windows Forms application

I have a Windows Forms application which contains a couple of labels, a button and a combobox all wrapper inside a Panel.
this.pnlSuboptions.Controls.Add(this.label1);
this.pnlSuboptions.Controls.Add(this.cboPtSize);
this.pnlSuboptions.Controls.Add(this.label2);
this.pnlSuboptions.Controls.Add(this.btnSelect);
I facing an issue with my labels when I try loading localized strings for my labels. The localized strings for some languages are larger than the English strings. In such cases, a part of the label gets hidden under the combo box or the button.
I want the label to increase in size towards the left instead of right. I've set my labels' AutoSize property to true and also played around with the Anchor property but nothing seems to work.
I found an SO link which contains a solution to this problem when the label text changes but I'm sure how I can apply this in my scenario where the label is read only once during the form load.
Any suggestions?
You could put them into a TableLayoutPanel with 2 columns and two rows. Each label goes in left side of each row and both combo box/buttons goes in the others cells (right side of each row).
Then you must dock both elements (Dock Fill) and set the columns to AutoSize. (As you can see in the image)
You also may want to dock the TablePanelLayout to your common panel.
As you can see in the image below, both TablePanelLayout have the same components. But in the secoend I just changed the label3's text.
Hope it helps. (Also sorry for my bad english, it isn't my native language. Please feel free to correct any wrong spelling, thanks!)
First suggestion from me would be:
set the AutoSize property to False
Make sure that the width is alot larger than it needs to be by resizing the label.
The label should now fit nicely regardless of what content it receive.
Second suggestion, you can use the GDI+ to determine the size of the text and then resize the label accordingly See http://msdn.microsoft.com/en-us/library/6xe5hazb(v=vs.110).aspx
Graphics gfx = this.label1.CreateGraphics(); // I think its called that, cant remember :)
Font stringFont = new Font("Arial", 16);
// Measure string.
SizeF stringSize = new SizeF();
stringSize = gfx.MeasureString(this.label1.Text, stringFont);
this.label1.Size = new Size((int)stringSize.Width, (int)stringSize.Height);
Oh yeah I almost forgot. Make sure that you're panel is not the cause of the clipping. I mean, check if the panel is large enough for the labels to fit :-)
Hope this helps!
Best regards,
Zerratar

Remove a StackPanel / Grid Unwanted Default Border?

I'm designing a UI in Visual Studio 2010, and I've come across a slight problem. I'm fairly new to XAML and this method of design, so this might be a rookie mistake. I've looked around but can't seem to find reference to a similar problem.
Basically, I have a grid with 2 rows, one of which contains a stackpanel and 5 columns. The stackpanel sits behind the columns and is used to paint the background of the entire row. When I add a background colour to the other row, I get an annoying line at the top of the stackpanel which just looks out of place. I've tried several things (showGridLines = false, transparent borders etc) but cannot seem to get rid of it. The best way to show it is through an image:
See http://img227.imageshack.us/img227/2220/tempym.jpg
One oddity I've noticed is that when I resize the window, at certain points the line disappears. Obviously this is not a solution (not least because it's going to be run full screen), but adds to the mystery of the line...
This is a trivial problem, but it is annoying me and will continue to do so until I can get rid of it, so any help would be appreciated. I've omitted the xaml for simplicity, but will post if required.
Based on the oddity you noticed, try setting SnapsToDevicePixels to true on the grid or window. Sounds like the borders are falling in between pixel boundaries and is being anti-aliased.
Or rather, if you're using .NET 4.0, use UseLayoutRounding instead.

width of the dropdown arrow

So in winforms, every dropdown combobox has this little arrow thingy to the right that tells the user it's a dropdown, kinda like this:
Now how do I figure out how wide that is in pixels? Reason is, I'm using ControlDrawToBitmap, this doesn't draw the text properly for the combo boxes, and I can redraw the contents, I just whack some of the arrows (which are drawn properly).
First idea that comes to mind: Check to see if the combobox button width tracks with the scrollbar width. The scrollbar width can be modified in user preferences. Use GetSystemMetrics() API to get the width of the various scrollbar pieces. If you change your system scrollbar width and it does not affect the size of a normal combobox, then ignore this.
Second idea: use the edit control's formatting rect to find out what the edit control thinks is the usable display area (minus the combo box). See EM_GETRECT in MSDN.
However, it sounds like this is just a hack workaround for your real problem: If you could get the controls to draw correctly to bitmap, then you wouldn't need this hackery.
I calculated it to be 9 pixels wide in photoshop

Categories

Resources