Maximum axis x Chart c# - c#

I'm working on a project where I work with two graphs, and on the lower graph if I see the maximum of the x-axis (24) but on the upper graph no. How can I make it appear up on 24? I am using the chart windows in visual studio, the default chart in windows forms
Chart image
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
chart1.ChartAreas[0].AxisX.Minimum = 0;
for (int i = 0; i <= 24; i++)
{
chart1.Series[0].Points.AddXY(i,1+i);
}
}
}

Change your code to (more information here):
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Maximum/Minimum controls the length of the Axis
chart1.ChartAreas[0].AxisX.Maximum = 30;
chart1.ChartAreas[0].AxisX.Minimum = 0;
chart1.ChartAreas[0].AxisY.Maximum = 30;
chart1.ChartAreas[0].AxisY.Minimum = 0;
// Interval controls the interval between values on the chart
chart1.ChartAreas[0].AxisX.Interval = 1;
chart1.ChartAreas[0].AxisY.Interval = 1;
}
private void button1_Click(object sender, EventArgs e)
{
chart1.ChartAreas[0].AxisX.Minimum = 0;
for (int i = 0; i <= 24; i++)
{
chart1.Series[0].Points.AddXY(i, 1 + i);
}
}
}
Chart looks like:

Related

How to create MaterialSingleLineTextField Controls dynamically

I would like to add MaterialSingleLineTextField dynamically to a Form.
I have used MaterialSkin NuGet package:
I am trying to create multiple MaterialSkin TextBoxes dynamically on Form.Load. But no Controls are displaying in the hosing Panel.
private void Form1_Load(object sender, EventArgs e)
{
int n = 5;
int pointX = 30;
int pointY = 40;
//panel1.Controls.Clear();
for (int i = 0; i < n - 1; i++)
{
MaterialSingleLineTextField a = new MaterialSingleLineTextField();
a.Text = (i + 1).ToString();
a.Visible = true;
a.Location = new Point(pointX, pointY);
panel1.Controls.Add(a);
panel1.Show();
pointY += 20;
}
}
This code block works perfectly fine for normal TextBoxes.
Is there any way to add MaterialSingleLineTextField dynamically?
A sample Form initialization with default Theme values.
The MaterialSkinManager is initialized in the Form Constructor, setting the Theme to MaterialSkinManager.Themes.LIGHT and default color scheme. The Form base Type is set to MaterialForm.
A specified number of MaterialSingleLineTextField controls is added to a parent container (a Panel), starting from a defined Location downwards.
These Controls are anchored to the parent and the Height is set to Parent.Font.Height + 4.
It's important that you specify the Size of these Controls, otherwise you'll get a minimal size that prevents the Controls from showing their content.
As you can see in the AddTextFields() method, it's important that you dispose of the previous Controls added to the Parent container, if you want to replace the existing with new ones. Even more important using this Library.
Calling the Clear() method of a Control.Controls collection (as in the line you have commented out) doesn't dispose of anything, those controls are still alive.
public partial class Form1 : MaterialForm
{
private readonly MaterialSkinManager msManager = null;
public Form1()
{
InitializeComponent();
msManager = MaterialSkinManager.Instance;
msManager.AddFormToManage(this);
msManager.Theme = MaterialSkinManager.Themes.LIGHT;
}
private void Form1_Load(object sender, EventArgs e)
{
AddTextFields(panel1, 5, new Point(30, 10), false);
}
private void AddTextFields(Control parent, int controlsCount, Point startPosition, bool ClearExisting)
{
if (clearExisting && parent.Controls.Count > 0) {
for (int i = parent.Controls.Count - 1; i >= 0; i--) {
parent.Controls[i].Dispose();
}
}
int controlHeight = parent.Font.Height + 4;
int yIncrement = 0;
for (int i = 0; i < controlsCount; i++) {
var textField = new MaterialSingleLineTextField() {
Text = (i + 1).ToString(),
Size = new Size(parent.ClientSize.Width - startPosition.X - 4, controlHeight),
Location = new Point(startPosition.X, startPosition.Y + yIncrement),
Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right
};
parent.Controls.Add(textField);
yIncrement += (controlHeight + 10);
}
}
private void matBtnChangeTheme_Click(object sender, EventArgs e)
{
msManager.Theme = MaterialSkinManager.Themes.DARK;
msManager.ColorScheme = new ColorScheme(Primary.Blue600, Primary.Blue900, Primary.Blue500, Accent.LightBlue200, TextShade.WHITE);
}
private void matBtnAddControls_Click(object sender, EventArgs e)
{
AddTextFields(panel1, 7, new Point(30, 10), true);
}
}
Sample functionality:

Y values shift up and down

I have plotted a graph in c#. How can I move the graph vertically up and down when a button is pressed.
private void button1_Click(object sender, EventArgs e)
{
chart1.Series[0].Points.Clear();
if (button1.Enabled == true)
{
for (int i = 0; i < 20; i++)
chart1.Series[0].Points.AddXY(i, Y1scale(i) + 1);
}
}
this is the code I have coded. But only on the first click it works. Further I want to move the graph continuously like in an oscilloscope without jumping from 1 y unit to another
I use a counter and each mouse click is counted and y values are shifted adding count value to the existing y value.
public Form1()
{
InitializeComponent();
}
double ch1_count_up=0;
double ch1_count_down = 0;
private void button1_Click(object sender, EventArgs e)
{
if(radioButton1.Checked==true)
{
ch1_count_up++;
chart1.Series[0].Points.Clear();
for (int i = -20; i < 20; i++)
chart1.Series[0].Points.AddXY(i, Y1scale(i) + (ch1_count_up / 10) - (ch1_count_down / 10));
}

How to gradually resize a picturebox?

I have been trying to code this for a while and after a couple of weeks of searching the web for an answer I decided to ask
All I want to do is gradually resize pictureBox1 to a set limit from a variable starter value when the mouse hovers over it, the furthest I got was using a forloop which made it instantly change size. I would like it to also change height and width at the same time (pictureBox1 will be a square and i just want it to be a bigger square with a bit of smooth movement)
Also I need it to gradually change back to the original size once the mouse moves off of pictureBox1.
I have been toying about with a couple of solutions found on websites but none seem to work properly, also you might need to know that I have two forms involved in this code; Form1 and frmMenu and because of a mass amount of errors I commented out the bottom two methods.
I do not get any errors but it just doesn't work.
public partial class frmMenu : Form
{
//private int size = 100;
public Timer timer1;
public frmMenu()
{
InitializeComponent();
pictureBox1.MouseEnter += new EventHandler(pictureBox1_MouseEnter);
//pictureBox1.MouseLeave += new EventHandler(pictureBox1_MouseLeave);
}
private string frmMenu_Load
{
set
{
timer1.Interval = 1;
}
}
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
//for (int i = 140; i > size; size++)
//{
//}
{
timer1.Interval = 1;
}
timer1.Enabled = true;
if (pictureBox1.Height <= 140)
{
pictureBox1.Size = new Size(pictureBox1.Size.Width, pictureBox1.Size.Height + 1);
}
else
{
timer1.Enabled = false;
}
}
// private void pictureBox1_MouseLeave(object sender, EventArgs e)
// {
// if (size > 100)
// for (int i = size; i > 100; i--)
// {
// size = i;
// }
// pictureBox1.Height = pictureBox1.Width = size;
// }
// private void pictureBox1_Click(object sender, EventArgs e)
// {
// var Form1 = new Form1();
// Form1.Show();
// var Menu = new frmMenu();
// Menu.Close();
// }
}
This is my first time asking so sorry if I haven't given enough information ^.^
Try this code:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Test
{
public partial class Form1 : Form
{
bool mouseHover;
int width;
int height;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 25;
timer1.Tick += timer1_Tick;
width = pictureBox1.Width;
height = pictureBox1.Height;
timer1.Start();
}
void timer1_Tick(object sender, EventArgs e)
{
if (mouseHover)
{
pictureBox1.Width += (pictureBox1.Width < 100) ? 5 : 0;
pictureBox1.Height += (pictureBox1.Height < 100) ? 5 : 0;
}
else
{
pictureBox1.Width += (pictureBox1.Width > width) ? -5 : 0;
pictureBox1.Height += (pictureBox1.Height > height) ? -5 : 0;
}
}
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
mouseHover = true;
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
mouseHover = false;
}
}
}
You can adjust the interval to how you like it, but increasing at 5 pixels horizontally/vertically every 25 milliseconds is pretty smooth. You need to set the initial height and width so you can go back to that size after the mouse leaves the picture box. I use the null coalescing operator so you don't have to stop the timer. As long as the condition on the left side of the ? is true, it will evaluate to the value on the left side of the :. When the condition is false, it evaluates to the right side of the :.

Form Updating while Calculation Runs

Started learning to code.
Have a console writing a 10 by ten grid of numbers randomly increasing in value.
Tried to do same in a form (DataGridView).
It works fine but there is no window until calcs are finished (I had to put a limit in - instead of an infinite loop).
I came here and read about refresh and doevent - but they dramatically slow everything down. But at least I can see the calcs happening.
I've tried to get my head around backgroundworker and I'm afraid I can't. If I'm in a loop to calculate - how do I separate those calcs from a screen update.
EDIT : Followed Nico's help(Thanks!!) and got this. Much better, but still laggy with big numbers. But a rocket with small numbers.
Any help to make faster?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace stackoverflowTest
{
public partial class Form1 : Form
{
Random rnd = new Random((Int32)DateTime.Now.Ticks);
private static int numSides = 8;
int numDice=2;
private int numRolls = 100000000;
private int max = 1;
private int min = 0;
private int diff = 0;
private double pdiff = 0d;
int[] array = new int[numSides *numSides];
public Form1()
{
InitializeComponent();
}
private void Form1Load(object sender, EventArgs e)
{
SetupDataForm1();
SetupDataForm2();
var bw = new BackgroundWorker();
bw.DoWork += BwDoWork;
bw.RunWorkerAsync(); //Start the worker
}
private void BwDoWork(object sender, DoWorkEventArgs e)
{
for (int rolls = 1; rolls < numRolls+1; rolls++)
{
// Roll two dice and increase that slot in the table
int y = Dice.Roll(numSides, rnd);
int x = Dice.Roll(numSides, rnd);
int k = Convert.ToInt32(dataGridView1.Rows[x].Cells[y].Value);
dataGridView1.Rows[x].Cells[y].Value = k + 1;
//Enter table into an array to work out max/min etc later
for (int i = 0; i < (numSides * numSides); i++)
{
int row = i / numSides;
int col = i % numSides;
array[i] = Convert.ToInt32(dataGridView1.Rows[row].Cells[col].Value);
}
max = array.Max();
min = array.Min();
diff = max - min;
if (max > 0) pdiff = (((double)diff / (max)) * 100);
dataGridView2.Rows[0].Cells[0].Value = rolls;
dataGridView2.Rows[1].Cells[0].Value = max;
dataGridView2.Rows[2].Cells[0].Value = min;
dataGridView2.Rows[3].Cells[0].Value = diff;
dataGridView2.Rows[4].Cells[0].Value = pdiff.ToString("0.000");
dataGridView2.Rows[5].Cells[0].Value = (array.Average()).ToString("0");
dataGridView2.Rows[6].Cells[0].Value = ((array.Average()/rolls)*100).ToString(("0.0000000"));
}
}
private void SetupDataForm1()
{
dataGridView1.Font = new Font("Microsoft Sans Serif", 6F);
dataGridView1.RowTemplate.Height = 11;
//Add Columns
for (int i = 0; i < numSides; i++)
{
dataGridView1.Columns.Add(i.ToString(), (i+1).ToString());
dataGridView1.Columns[i].Width = 35;
}
// Add Rows
for (int i = 0; i < numSides; i++)
{
dataGridView1.Rows.Add();
if (i % 2 != 0)
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.LightGray;
}
dataGridView1.Rows[i].HeaderCell.Value = ((i+1)*10).ToString();
}
}
private void SetupDataForm2()
{
dataGridView2.Font = new Font("Microsoft Sans Serif", 8F);
dataGridView2.RowTemplate.Height = 16;
//Add Columns
for (int i = 0; i < 1; i++)
{
dataGridView2.Columns.Add(i.ToString(), "");
dataGridView2.Columns[i].Width = 65;
}
// Add Rows
for (int i = 0; i < numSides; i++)
{
dataGridView2.Rows.Add();
if (i % 2 != 0)
{
dataGridView2.Rows[i].DefaultCellStyle.BackColor = Color.LightGray;
}
}
dataGridView2.Rows[0].HeaderCell.Value = "Rolls";
dataGridView2.Rows[1].HeaderCell.Value = "Max";
dataGridView2.Rows[2].HeaderCell.Value = "Min";
dataGridView2.Rows[3].HeaderCell.Value = "Diff";
dataGridView2.Rows[4].HeaderCell.Value = "%";
dataGridView2.Rows[5].HeaderCell.Value = "Avg";
dataGridView2.Rows[6].HeaderCell.Value = "Av%";
}
public class Dice
{
public static int Roll(int numberOfSides, Random rnd)
{
return rnd.Next(0, numberOfSides);
}
}
}
}
If there is no window, you perform the calculations in the form's constructor. To make the form visible before starting the calculation, put the code in the form's Load event. Therefore double click the event in the form's properties window and a method will be created. Put your code into this method.
If you want to use a background worker, the procedure is similar. However, you need to create the Backgroundworker. E.g. in code:
private void Form1_Load(object sender, EventArgs e)
{
var bw = new BackgroundWorker();
bw.DoWork +=
If you start typing that, Visual Studio suggests to create a method for the event. Press Tab twice to generate it and you'll basically get the following code:
private void Form1_Load(object sender, EventArgs e)
{
var bw = new BackgroundWorker();
bw.DoWork += bw_DoWork;
bw.RunWorkerAsync(); //Start the worker
}
void bw_DoWork(object sender, DoWorkEventArgs e)
{
throw new NotImplementedException(); //remove this
}
Put your calculation code in the bw_DoWork method an it will be executed in the background without affecting the user interface.

C# change labels created in public class from form button click

Having hard time understanding classes and why I can't access certain object.
How can i modify the code so I can change "map"(which is a bunch of labels) properties in all of my classes/events?
The method Draw2d() creates a couple of labels on the main form that I wish to change on different events(button click in this example).
Can someone help me, or just hint me into the right direction.
The Code:
public partial class Form1 : Form
{
public void Draw2d()
{
const int spacing = 20;
Label[][] map = new Label[5][];
for (int x = 0; x < 5; x++)
{
map[x] = new Label[5];
for (int y = 0; y < 5; y++)
{
map[x][y] = new Label();
map[x][y].AutoSize = true;
map[x][y].Location = new System.Drawing.Point(x * spacing, y * spacing);
map[x][y].Name = "map" + x.ToString() + "," + y.ToString();
map[x][y].Size = new System.Drawing.Size(spacing, spacing);
map[x][y].TabIndex = 0;
map[x][y].Text = "0";
}
this.Controls.AddRange(map[x]);
}
}
public Form1()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
Draw2d();
}
private void button1_Click(object sender, EventArgs e)
{
map[0][0].Text = "1"; // <-- Doesn't work
}
}
Thanks!
you have to declare the map as property(global to class)
public partial class Form1 : Form {
public Label[][] map;
....
}
then you can use inside class like
this->map[...][...]
or from outside like
objClass->map[...][...]
My guess is that you added
public Label[][] map;
but forgot to change the second line of Draw2d from
Label[][] map = new Label[5][];
to
map = new Label[5][];
I just tried your code, and it works fine if you change those two lines. If that's not the problem, could you say what error you're getting, please?

Categories

Resources