How to refresh piechart in c# - c#

I have made a method including the code of pie chart and call that method everywhere i need to refresh the chart, but whenever i click on those buttons where i have called the method then the pie chart duplicates the value automatically. And also i have tried Refresh() and Update option too but it doesn't work.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
chart();
}
private void chart()
{
chart1.Series["new"].Points.AddXY("Peter", "1000");
chart1.Series["new"].Points.AddXY("Julia", "1000");
}
private void button1_Click(object sender, EventArgs e)
{
chart();
}
}

If I've understood the problem correctly, I think you simply need to clear down the Series' Points collection.
For example:
private void chart()
{
chart1.Series["new"].Points.Clear();
chart1.Series["new"].Points.AddXY("Peter", "1000");
chart1.Series["new"].Points.AddXY("Julia", "1000");
}
Is this what you were looking to do?

Related

how to put Background image property for trackbar

I am trying to get the background image fot the trackbar only. I used following snippet but the image is not visible.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}[enter image description here][1]
private void trackBar1_Scroll(object sender, EventArgs e)
{
//this.trackBar1.
this.trackBar1.BackgroundImage =
Image.FromFile(#"C:\Users\310276521\Documents\Aftab\Untitled.jpg");
}
}
I want to implement this kind of layout:
try this
private void trackBar1_Scroll(object sender, EventArgs e)
{
//this.trackBar1.
this.trackBar1.BackgroundImage = Image.FromFile(#"C:\\Users\\310276521\\Documents\\Aftab\\Untitled.jpg");
}
Please make sure you are having the image in that location

Fill a List<String> of a Winform from other Winform while both forms are active

I'm trying to fill a List<String> from another form while both forms are open.
In the form WorkOrder i have a list: List<String> materialsList, then in my other form AddMaterials i have other List<String> list, this is the list that i want to pass to the WorkOrder form, but both forms are open and i don't know it is possible to do that.
Here are my forms:
If i click the Add Materials button in the WorkOrder form then the form AddMaterials opens. Now with the form AddMaterials open, i fill one by one the elements of the list with the button Add new Material, then when i click finish i want to pass the List<String> list to the WorkOrder list: List<String> materialsList.
This is the code that i'm trying to solve this:
In the WorkOrder form:
List<String> materialsList = new List<string>();
public void setList(List<String> l)
{
this.materialsList = l;
}
In the AddMaterials form:
public partial class AddMaterials : Form
{
List<String> list = new List<string>();
public AddMaterials()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Adding Materials
list.Add(material.Text);
}
private void button2_Click(object sender, EventArgs e)
{
//Passing the list with Method setList()
WorkOrder o = new WorkOrder();
o.setList(list);
}
}
Any question post on comments.
You could pass a reference to either the WorkOrder Form or the WorkOrder.materialsList in the constructor of your AddMaterials Form.
So your AddMaterials code could be
public partial class AddMaterials : Form
{
WorkOrder wo;
public AddMaterials(WorkOrder wo)
{
InitializeComponent();
this.wo = wo;
}
private void button1_Click(object sender, EventArgs e)
{
//Adding Materials
this.wo.materialsList.Add(material.Text);
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
In WorkOrder you'd have this function which opens a new AddMaterials Form. You could place in logic here before you create a new AddMaterials Form to clear the list or do something else if you have multiple materials list for a WorkOrder.
private void button1_Click(object sender, EventArgs e)
{
// Add logic here to deal with multiple AddMaterial Forms
// on one work order
new AddMaterials(this).Show();
}
What you're doing is not working since you are using two completely different lists.
You can create a custom constructor for AddMaterials:
public AddMaterials(List<string> materials)
{
InitializeComponent();
this.materials = materials;
}
and declaring materials as a member of AssMaterials class:
private List<string> materials;
If you want to keep things even more separated you can pass a delegate:
public delegate void Del(string str);
class AddMaterials
{
private Del addMaterialDelegate;
public AddMaterials(Del AddMaterialDelegate) : Form
{
InitializeComponent();
this.addMaterialDelegate = AddMaterialDelegate;
}
private void button1_Click(object sender, EventArgs e)
{
//Adding Materials
addMaterialDelegate(material.Text);
}
}
and then from WorkOrder:
private void button1_Click(object sender, EventArgs e)
{
new AddMaterials(OnAddMaterialDelegate).Show();
}
private void OnAddMaterialDelegate(string material)
{
this.materialsList.Add(material);
}
This will allow you to change your mind in the future without too much rework (for example if you want to switch from a List to a different type).

Order of form_load method execution

I am creating a winforms application which uses Gmaps.net. I am unable to alter the order in which on Load methods are being called. For some reason the map_load is being called before the man_Load. Is there any way to change the order of this ?
If I can provide any more information to help just ask.
Thanks!
Dan.
public partial class main : Form
{
public main()
{
InitializeComponent();
}
private void main_Load(object sender, EventArgs e)
{
MessageBox.Show("main_load");
}
private void map_Load(object sender, EventArgs e)
{
MessageBox.Show("map_load");
}
}
It seems that you used the WinForms designer to create the map. The code behind is in the InitializeComponent() method and seems that the map is being loaded before the MainForm is loaded.
My recommendation is to create the map, once the MainForm has been loaded:
public partial class main : Form
{
public main()
{
InitializeComponent();
}
private void main_Load(object sender, EventArgs e)
{
Control map = CreateMap();
map.Docking = DockStyle.Fill;
this.Controls.Add(map);
}
private Control CreateMap()
{
// Create a new GMaps.NET object, intialize it and return
}
}
Hope it helps.

View list on another form

Can someone tell me what i'm doing wrong here?
I've got a list in a database class that I want to view in a listbox on my form yet it's not showing anything.
I call the form from a button click of my first form which is where I enter the data and it works if I put a listbox on that form, but I'm wanting to open another form which will only show the data if that makes sense?
here's my code for the form that I want to view what's in the list:
public partial class Summary : Form
{
public Summary()
{
InitializeComponent();
}
private Database viewlist = new Database();
private void Summary_Load(object sender, EventArgs e)
{
}
private void sum()
{
List<String> listofPicks = viewlist.listPickups();
listBox1.Items.AddRange(listofPicks.ToArray());
}
private void button1_Click(object sender, EventArgs e)
{
sum();
}
}
Also may i make it clear that this code works if it's all done on the same form
Try this (kind of), load the list when the form loads, not on a button click:
public partial class Summary : Form
{
private Database _viewlist = new Database();
public Summary()
{
InitializeComponent();
}
private void Summary_Load(object sender, EventArgs e)
{
LoadList();
}
private void LoadList()
{
listBox1.Items.Clear();
listBox1.Items.AddRange(_viewlist.Pickups.ToArray());
}
}
public class Database
{
public List<string> Pickups
{
get { return new List<string> {"alfa", "beta"}; }
}
}

call variable from another form C#

I have a DataGridView in Form1 and I'm using this code to display another form called Generator:
private void button1_Click(object sender, EventArgs e)
{
Form gen = new Generator();
// Form gen = new Generator(Form this); //* I tried this but is not working *//
gen.Show();
}
In the Generator form I need to read or modify something in the datagridview which is in the Form1.
public partial class Generator : Form
{
public Form myForm;
public Generator()
{
InitializeComponent();
}
public Generator(Form frm)
{
myForm = frm;
}
private void button1_Click(object sender, EventArgs e)
{
myForm.mydatagridview.! // this is not working
}
}
How can I resolve this problem, so I can manipulate the DataGridView from the Generator form?
Form 1:
private void button1_Click(object sender, EventArgs e)
{
Form gen = new Generator(this.mydatagridview);
gen.Show();
}
Generator Form:
DataGridView _dataGridView;
public Generator(DataGridView dataGridView)
{
InitializeComponent();
this._dataGridView = dataGridView;
}
private void button1_Click(object sender, EventArgs e)
{
this._dataGridView...! // this will work
}
Things that you must do, and know (just tips, you are not forced to do these but I believe you will be a better programmer if you do! ;)
Always call InitializeComponent() in all form constructors. In your sample you didn't call it in one of the constructors.
C# knows only information of the type you have passed. If you pass a Form, then you only get Form properties (i.e. the properties of type Form), not the properties of your own form.
Try to encapsulate things. Do not pass a whole form to another form. Instead, pass things that you would like to use on the other form.
A few things are going on here.
You have to use the constructor of Generator that take in a form as a parameter.
You have to expose the datagridview as a public or internal property on the form that you will pass into Generator.
The normal Form class will not know about this property, so you should cast the variable appropriately.
You should call the default constructor of Generator when the other constructor is used to make sure everything is initialized correctly. See code sample below.
Something like this should work:
public class Generator
{
private MyForm myForm;
public Generator()
{
InitializeComponent();
}
public Generator(MyForm frm) : this() // DON'T FORGET THIS()
{
myForm = frm;
}
private void button1_Click(object sender, EventArgs e)
{
myForm.MyDataGridView... // Yay, it works!
}
}
public class MyForm : Form
{
public MyForm()
{
InitializeComponent(); // a datagridview is created here, say "datagridview1"
}
public DataGridView MyDataGridView
{
get { return datagridview1; }
}
}
And then in your button click event (which I assume is defined somewhere in MyForm):
private void button1_Click(object sender, EventArgs e)
{
Form gen = new Generator(this);
gen.Show();
}
The easiest way from there is to open the designer for the DataGridView (myDataGridView) on Form1 and set the Modifiers property from private to internal or public
This will let you call myForm.myDataGridView from the Generator form.

Categories

Resources