How to fix black/negative background on livecharts chart in winforms - c#

When adding a usercontrol at runtime (with a button click for example) that has a chart on, it causes the background of the chart to go black/negative.
When I add the usercontrol during the forms load it does not have this issue.
private void Form1_Load(object sender, EventArgs e)
{
//this one loads fine
testingControl tc = new testingControl();
this.Controls.Add(tc);
tc.Location = new Point(470, 53);
//......
}
private void Button1_Click(object sender, EventArgs e)
{
testingControl tc = new testingControl();
this.Controls.Add(tc);
//tc.Visible = false;
//tc.Visible = true;
tc.Location = new Point(0, 53);
}
I found out after some google searches, that hiding it and then setting it visible again kind of fixes it, but that seems a bit janky.
Does anyone know what I can do to fix this?
P.S. the data that is in the chart I got from the 'livecharts' website, but the problem happens no matter if there is data or not.
The problem also happens on all charts.

Got similar problem & made few changes to get it fixed ,
Just change the chart visibility false
Change
Then , Make it visible on form load .
private void Form1_Load(object sender, EventArgs e)
{
pieChart1.Visible = true;
....
}
Hope this will help you !.

Related

Problems with datagridview / autoresizecolumns and rows

I have a huge problems every time I try to resize my columns and rows. I'm trying to autoresize the columns and the rows with the function:
dataGridView1.AutoResizeColumns();
dataGridView1.AutoResizeRows();
If i put this two lines after I pass the datasource to dataview, it doesn't work. I tried to handle the DataSourceBindingComplete, and idem it doesn't work. I tried to set it in the form.designer.cs and it doesn't work. then
I tried to make a button
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.AutoResizeColumns();
dataGridView1.AutoResizeRows();
}
and when I click the button everything works perfectly!!! It resizes all my columns and my rows. But i don't want this. I want it automatic.
Can you guys help me please and explain why it does that? Doesn't make sense, inside the original code it doesn't work, but in a separate button it works.
This has worked for me.
Set AutoSizeColumnsMode and AutoSizeRowsMode values to AllCells from None in the designer.
Have you tried setting the DataGridViewAutoSizeColumnsMode before you set the source, if possible?
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
dataGridView1.DataSource = SourceList; // Your Collection here
dataGridView1.AutoResizeRows();
}
I apologize that it took me more than a year to see this post...
Here's what I did to make this "automatic".
private void AutoResizeGrid()
{
if (dataGridView.Columns.Count < 1) return;
dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.DisplayedCells);
dataGridView.AutoResizeRows(DataGridViewAutoSizeRowsMode.DisplayedCells);
}
private void MatrixDataGridView_ClientSizeChanged(object sender, EventArgs e)
{
if (!this.Visible) return;
AutoResizeGrid();
}
private void MatrixDataGridView_Scroll(object sender, ScrollEventArgs e)
{
if (!this.Visible) return;
AutoResizeGrid();
}
Before adding this code, I went to the Designer and I set "AutoSizeColumnsMode" and "AutoSizeRowsMode" both to "DisplayedCells". This causes automation when the grid first receives the data. The code above causes automation when a resize or scroll event happens.

Datagridview shall disappear when clicking on the Form in the background

As described in the title, I have a Form with a Datagridview on the front. The datagridview is smaller than my form in the back and I want the Datagridview to disappear whenever I click anywhere else but the Datagridview.
My code looks like this:
this.dataGridView1.Leave += new System.EventHandler(this.focus);
and the Eventhandler is defined like this:
private void focus(object sender, EventArgs e)
{
if(dataGridView1.Focused == false)
{
dataGridView1.Visible = false;
}
}
My problem is that my Datagridview only disappears when a new event in my form is activated but not when I click for example in a textbox on my form.
Can anyone help me?
The Leave event will not raise if you click on Form, or a ToolStripButton, PictureBox or any other non-selectable control.
If you expect a behavior like a dropdown, you can host DataGridView in a ToolStripControlHost and show it using a ToolStripDropDown. This way when you click anywhere outside the `DataGridView, it will disappear. It will act like a dropdown menu. Also the grid can be larger than your form:
private void button1_Click(object sender, EventArgs e)
{
this.dataGridView1.Margin = new Padding(0);
var host = new ToolStripControlHost(this.dataGridView1);
this.dataGridView1.MinimumSize = new Size(200, 100);
host.Padding = new Padding(0);
var dropdown = new ToolStripDropDown();
dropdown.Padding = new Padding(0);
dropdown.Items.Add(host);
dropdown.Show(button1, 0,button1.Height);
}
Important Note: It's an example. It's better to pay attention to disposing of objects in a real world application. For example, use just a single ToolStripdropDown and dispose it when closing the form.
Change the event handler assigning to:
this.dataGridView1.Leave += new System.EventHandler(fokussiert);
Worked for me when focusing on a textbox
you want your dgv also to disapear when you click on your textbox? is that what you mean?
private void dataGridView1_Leave(object sender, EventArgs e)
{
dataGridView1.Visible = false;
}
private void Form1_Click(object sender, EventArgs e)
{
dataGridView1.Visible = false;
}

TextBox shown in the wrong place after a scroll down

i have a from c# and i want to show text Box after a click in a check box.
but when i scroll down, and check , the text Box is shown in the wrong place !!!
the text Boxes must be in the same level with check Boxes.
private void checkBox1_Checkedchanged(object sender, EventArgs e)
{
textBox1.Visible = true;
}
and changing the Location of the text Box don't give good results !
thanks for help.
You are running into an awkward quirk of the Panel control, it only scrolls controls that are visible. When you make it visible in your code, it will have the wrong Location property if you've used the scrollbar. You will need to make the correction yourself. Make it look like this:
private void checkBox1_Checkedchanged(object sender, EventArgs e)
{
if (!textBox1.Visible) {
textBox1.Location = new Point(textBox1.Left + panel1.AutoScrollPosition.X,
textBox1.Top + panel1.AutoScrollPosition.Y);
textBox1.Visible = true;
}
}
A better alternative is to use the Enabled property instead, also much less disorienting for the user. Set it to False in the designer and then:
private void checkBox1_Checkedchanged(object sender, EventArgs e)
{
textBox1.Enabled = true;
}

Change Page when entering fullscreen mode

my problem is quite simple: when user changes selection in a ListBox, I need my app to go to fullscreen mode, but I need to change the displayed page. I use Silverlight 4
private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
PresentationPage currentPresentationPage = new PresentationPage();
App.Current.RootVisual = currentPresentationPage;
App.Current.Host.Content.IsFullScreen = true;
}
When the code above is executed, the app goes to fullscreen, but the Page does not change, it only resizes. Can anybody tell me what's wrong with that code? Thanks
You can't change Application.RootVisual after it is assigned. What you need to do is include a panel that you can change it's content and make that panel your RootVisual.
private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
PresentationPage currentPresentationPage = new PresentationPage();
(App.Current.RootVisual as Panel).Children.Clear();
(App.Current.RootVisual as Panel).Children.Add(currentPresentationPage);
App.Current.Host.Content.IsFullScreen = true;
}
Then in your App's Startup event do something like so.
Panel grid = new Grid();
grid.Children.Add(new MainPage());
App.Current.RootVisual = grid;

How can I "double-click, edit and save while-leaving" a Windows Forms DataGridView CurrentCell?

I'm barely new with WinForm's DataGridView control and quite honestly I'm having a hard time trying to mix and match its events and methods (e.g. CommitEdit() method didn't do what I've expected) to implement the simple concept of "entering edit mode by double-clicking the cell, modify its value (hopefully doing some sort of validation) and saving changes when leaving the aforementioned cell. My actual code looks like this and it's definitely incomplete:
// DEBUG
private void myDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
this.myDataGridView.BeginEdit(true);
this.myDataGridView.CurrentCell.ReadOnly = false;
}
private void myDataGridView_CellLeave(object sender, DataGridViewCellEventArgs e)
{
this.myDataGridView.EndEdit();
}
private void myDataGridView_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
DataGridViewCellStyle editCellStyle = new DataGridViewCellStyle();
editCellStyle.BackColor = System.Drawing.Color.DarkOrange;
editCellStyle.ForeColor = System.Drawing.Color.Black;
this.myDataGridView.CurrentCell.Style.ApplyStyle(editCellStyle);
}
private void myDataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
DataGridViewCellStyle defaultCellStyle = new DataGridViewCellStyle();
defaultCellStyle.BackColor = System.Drawing.Color.White;
this.myDataGridView.CurrentCell.Style.ApplyStyle(defaultCellStyle);
}
// DEBUG
So as you might notice any help you could provide will be really valuable and definintely appreciated. Thanks much you guys in advance!
Wow, turns out the "Entity" (LINQ-to-SQL) I'm dealing with has no PK hence LINQ is unable to update it so it's not DGV's fault. Sorry about that.

Categories