How to add a ZedGraph programmatically? - c#

I want to add a ZedGraph when I click a button, but the ZedGraph is not coming up when I click my button. Here is my button click handler:
ZedGraphControl zg1 = new ZedGraphControl();
zg1.Dock = DockStyle.Fill;
GraphPane myPane = new GraphPane();
BarItem myBar = new BarItem("Bar1");
myBar.AddPoint(1, 10);
myBar.AddPoint(2, 20);
myBar.Bar.Fill = new Fill(Color.AliceBlue, Color.White, Color.AliceBlue);
zg1.AxisChange();
zg1.Invalidate();
zg1.Show();

The main thing that jumps out at me is that I don't see you adding your new Control to your Container Object wether it be a Form or a Panel. Also you are not associating your Pane or your Bar to your ZedGraphControl
Try Something like this
ZedGraphControl zg1 = new ZedGraphControl();
zg1.Dock = DockStyle.Fill;
zg1.GraphPane = new GraphPane();
BarItem myBar = new BarItem("Bar1");
myBar.AddPoint(1, 10);
myBar.AddPoint(2, 20);
myBar.Bar.Fill = new Fill(Color.AliceBlue, Color.White, Color.AliceBlue);
zg1.GraphPane.CurveList.Add(myBar);
zg1.AxisChange();
zg1.Invalidate();
zg1.Show();
this.Controls.Add(zg1);

Related

Create GroupBox and labels inside it programmatically

I want to create a groupbox programmatically and inside it put labels. Now I created this but with design and I have like this:
I'm trying but I don't know how can I assign labels in correct position and how can I assign it to a specific group box
GroupBox groupBox1 = new GroupBox();
Panel grid1 = new Panel();
Label lbl1 = new Label { Text = "Completed" };
Label lbl2 = new Label { Text = "label" };
Label lbl3 = new Label { Text = "In progress" };
Label lbl4 = new Label { Text = "label" };
//etcetera
groupBox1.Width = 185;
groupBox1.Height = 160;
grid1.Height = 185;
grid1.Width = 160;
How can I achieve that? Regards
Update
As the comments below I try
GroupBox groupBox1 = new GroupBox();
this.Controls.Add(groupBox1);
Panel grid1 = new Panel();
groupBox1.Controls.Add(grid1);
groupBox1.Location = new Point(20, 250);
grid1.Location = new Point(20, 250);
Label lbl1 = new Label { Text = "test" };
Label lbl2 = new Label { Text = "Test2" };
groupBox1.Name = "TESTTT";
groupBox1.Width = 222;
groupBox1.Height = 149;
grid1.Height = 218;
grid1.Width = 145;
grid1.Controls.Add(lbl1);
grid1.Controls.Add(lbl2);
Result:
But my group box it just clear without name and without labels, why it happen?
Controls in WinForms are arranged so that they reside inside one another. So your Form has a Controls collection which is essentially a Collection of type Control. So if you add a GroupBox to the form, then you must add it to the Controls collection of the form. Then if you add a control to your GroupBox then you need to add it to the GroupBox collection of controls.
With that in mind, you can do something like this:
private void AddGroupBoxAndLables()
{
GroupBox groupBox1 = new GroupBox();
groupBox1.SetBounds(50, 50, 300, 200);
this.Controls.Add(groupBox1);
Label lblCompleted = new Label { Name = "lblCompleted", Text = "Completed" };
lblCompleted.Location = new Point(20, 20);
groupBox1.Controls.Add(lblCompleted);
Label valCompleted = new Label { Name = "valCompleted" };
valCompleted.Location = new Point(80, 20);
groupBox1.Controls.Add(valCompleted);
Label lblInProgress = new Label { Name = "lblInProgress", Text = "In Progress" };
lblInProgress.Location = new Point(20, 60);
groupBox1.Controls.Add(lblInProgress);
Label valInProgress = new Label { Name = "valInProgress" };
valInProgress.Location = new Point(80, 60);
groupBox1.Controls.Add(valInProgress);
}
simplest way for solution is that your code is already created when you did design. Code is created automatically in respective form's designer.cs or designer.vb file. To see this file, in solution explorer click on button 'Show all files'. Still for your understanding let me explain code
You have created groupbox using
GroupBox groupBox1 = new GroupBox();
To see this group box on form you need to add this groupbox to form
this.Controls.Add(groupBox1);
Similar in case of panel. If you want to add panel inside of groupbox then
groupbox1.Controls.Add(grid1);
Then add all labels inside of panel.
You will find similar kind of code in form's designer.cs.

How to create multiple RichTextBoxes

I need to crate a certain number of RichTextBoxes depending on User Input.
I can create one in visual studio using the toolbox, but how would I go about creating multiple through code?
UPDATE:
This is now my code:
RichTextBox richTextBox = new RichTextBox();
richTextBox.Location = new Point(12, 169);
richTextBox.Width = 62;
richTextBox.Height = 76;
this.Controls.Add(richTextBox);
Nothing happens when I run this
OK. Here is a sample that shows that it works:
void Main()
{
Form f = new Form();
Button b = new Button();
b.Click += (sender, args) =>
{
RichTextBox richTextBox = new RichTextBox
{
Name = "rtbBlahBlah",
Location = new System.Drawing.Point(12, 169),
Width = 62,
Height = 76
};
f.Controls.Add(richTextBox);
};
f.Controls.Add(b);
f.Show();
}
Call this.Refresh() to refresh the Control and re-draw all the children within it.
From the Docs:
Forces the control to invalidate its client area and immediately redraw itself and any child controls.

C# - Reordering controls by z-index in Panel

I have a Panel with a PictureBox with Dock = DockStyle.Fill. I need to dynamically add controls to the Panel, but they must stay above the PictureBox.
This is easy within the Designer, but when I do this programmatically, neither SetChildIndex(), BringToFront() or SendToBack() work.
I have to use PictureBox, I can't just set Panel.BackgroundImage because it's glitchy.
I fount this to be an issue with the order of the controls in the panel in the design.cs
Remove the controls from the panel and add them in the correct order.
this.panel1.Controls.Add(this.pictureBox1);
this.panel1.Controls.Add(this.button1);
this.panel1.Location = new System.Drawing.Point(12, 12);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(260, 238);
this.panel1.TabIndex = 0;
this.panel1.Controls.Add(this.button1);
this.panel1.Controls.Add(this.pictureBox1);
this.panel1.Location = new System.Drawing.Point(12, 12);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(260, 238);
this.panel1.TabIndex = 0;
Once you add your dynamic control to the Controls find it and BringToFront like as follows:
TextBox tb = new TextBox
{
Location = new Point(100, 100),
Name = "Textbox1"
};
this.Controls.Add(tb);
var contr = Controls.Find("Textbox1", true)[0];
contr.BringToFront();
Alternatively, once you add new dynamic control. Apply SendToBack to the PictureBox.
pictureBox1.SendToBack();

Can't access buttons from Form_Load in event handler

I have three buttons in Form_Load, to every button I give size, location and text.
When I click one of the buttons a new button appears, which should take me to the first screen with the three original buttons. I clear the screen and add the buttons, but I get an error "the name 'button' does not exist in the current context". What can I do to have access to these buttons. Thanks.
Form_Load:
Button play = new Button();
Button howtoplay = new Button();
Button puzzles = new Button();
play.Size = new Size(175, 70);
puzzles.Size = new Size(175, 70);
howtoplay.Size = new Size(175, 70);
play.Location = new Point((ClientRectangle.Right/2)-(play.Width/2), 135);
puzzles.Location = new Point((ClientRectangle.Right / 2) - (play.Width / 2), 210);
howtoplay.Location = new Point((ClientRectangle.Right / 2) - (play.Width / 2), 285);
play.Text = "Play";
howtoplay.Text = "How To Play";
puzzles.Text = "Puzzles";
Controls.Add(play);
Controls.Add(howtoplay);
Controls.Add(puzzles);
howtoplay.Click += new EventHandler(howtoplay_click);
howtoplay_click:
play.Hide();
puzzles.Hide();
howtoplay.Hide();
Button backB = new Button();
backB.Size = new Size(100, 50);
backB.Location = new Point((ClientRectangle.Right - backB.Width - 10), (ClientRectangle.Bottom - backB.Height - 10));
backB.Text = "Back";
backB.Click += new EventHandler(Back_Click);
Controls.Add(backB);
Back_Click:
Controls.Clear();
Controls.Add(play); //error
Controls.Add(puzzles); //error
Controls.Add(howtoplay); //error
You have declared buttons as local variables of Form_Load event handler method:
private void Form_Load(object sender, EventArgs e)
{
Button play = new Button();
Button howtoplay = new Button();
Button puzzles = new Button();
// ...
}
These variables are not available outside the method. You should use form fields instead:
// available in all instance methods of form
Button play;
Button howtoplay;
Button puzzles;
private void Form_Load(object sender, EventArgs e)
{
play = new Button();
howtoplay = new Button();
puzzles = new Button();
// ...
}
Note: Usually you should manually create controls only when those controls should be added to your form dynamically at runtime. But you are creating controls in Form_Load event handler, so I suggest you use designer to create controls. It will create a class field for each control and add appropriate initialization code. All you need to do is drag-and-drop control (buttons in this case) from toolbox to form and setup each control properties.

add control to Panel and control doesn't display anymore c#

I have one textbox (textBox1) and panel (Panel1) I have code like this
Panel1.Controls.Add(textBox1)
so when I run it I can't see textbox anymore, If I do like this I can see textBox
textBox1.Location = Panel1.Location
can anyone tell me what's problem?
When a textbox (or any control) is part of a panel the top left of the panel is point(0.0);
so when textBox1.Location = Panel1.Location the textbox probably falls out of view in the panel.
try something like this instead/
//
// panel1
//
this.panel1.Controls.Add(this.textBox1);
this.panel1.Location = new System.Drawing.Point(59, 27);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(193, 176);
this.panel1.TabIndex = 1;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(0, 0);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 0;
I believe the reason you're not able to see the Textbox has to do with the Panel's properties. Try setting the AutoSize property to true and the AutoSizeMode property to GrowAndShrink.

Categories

Resources