Set Form dimensions related to panel dimensions - c#

I'm using C# and I have a WinForms application.
I have a main form which contains some buttons, lets say button A and B.
I have another form,FormB which have 2 panels with different dimensions.
What I'm trying to do is when the user clicks on button A is to show FormB with
the dimensions of the first panel and present the first panel.
And when the user clicks on button B is to show formB with the second panel dimensions
and present the second panel.
I know How to present those panel in each case but the form size doesn't change as I expected.
I tried to use the Dock property and set it to fill.. but nothing happens.
private void FormB_Load(object sender, EventArgs e)
{
panel1.Location = panel2.Location = new Point();
timer1.Start();
if (!first)
{
panel1.Visible = false;
panel2.Visible = true;
}
else
{
panel1.Visible = true;
panel2.Visible = false;
}
this.Dock = DockStyle.Fill;
}

You should use panel1.Dock = DockStyle.Fill or panel2.Dock = DockStyle.Fill, not this.Dock = DockStyle.Fill because you want to set the Dock property of the Panel not the Form.
private void FormB_Load(object sender, EventArgs e)
{
panel1.Location = panel2.Location = new Point();
timer1.Start();
if (!first)
{
panel1.Visible = false;
panel2.Visible = true;
panel2.Dock = DockStyle.Fill;
}
else
{
panel1.Visible = true;
panel2.Visible = false;
panel1.Dock = DockStyle.Fill;
}
}
EDIT But previous code will change the size of the panels according to the size of the form. Since you want to set the size of the form to be the size of the panel(s), then you should set the Size property of the Form like this:
private void FormB_Load(object sender, EventArgs e)
{
panel1.Location = panel2.Location = new Point();
timer1.Start();
if (!first)
{
panel1.Visible = false;
panel2.Visible = true;
this.Size = new Size(panel2.Size.Width + 16, panel2.Size.Height + 38);
}
else
{
panel1.Visible = true;
panel2.Visible = false;
this.Size = new Size(panel1.Size.Width + 16, panel1.Size.Height + 38);
}
}

Related

How to make the event wait until a boolean is changed

I have an event registered.
layout.SizeChanged += parentToggleBox.Resize;
Problem is, I would like this Resize event to wait until my mouse has moved away from the box.
How to make this event wait until a boolean is changed from false to true?
or wait until OnMouseLeave(object sender, MouseEventArgs e) is triggered?
Here's an example that I put together:
public class Form1 : Form
{
private Panel Panel1;
private Label Label1;
public Form1()
{
this.Panel1 = new Panel()
{
BackColor = System.Drawing.Color.Red,
};
this.Label1 = new Label()
{
Top = 200,
Text = "Click",
};
bool clicked = false;
bool entered = false;
this.Panel1.Click += (s, e) =>
{
this.Panel1.BackColor = System.Drawing.Color.Blue;
clicked = true;
};
this.Panel1.MouseEnter += (s, e) =>
{
this.Panel1.BackColor = System.Drawing.Color.Yellow;
clicked = false;
entered = true;
};
this.Panel1.MouseLeave += (s, e) =>
{
if (entered && clicked)
{
this.Panel1.BackColor = System.Drawing.Color.Green;
this.Label1.Text = "Success";
}
};
this.Controls.Add(this.Panel1);
this.Controls.Add(this.Label1);
}
}
I've used a Click event rather than a Resize to demonstrate the technique.
Effectively this is just setting two booleans and when the right combination of entered and clicked occurs then the code in the block that contains this.Label1.Text = "Success"; will run.
The colours are set for debugging purposes.
To run, do this:
var form1 = new Form1();
form1.ShowDialog();

C# Searchable, Positionable PictureBox

I've been searching about this for a while and haven't found what I'm looking for.
I want to be able to have an image in a picturebox that can be positioned to a point by means of a "search box". Basically, mapping locations in pixels to certain phrases or letters that then shift the position on the picturebox.
I tried to set the location using points, however this does not change the picture at all; the location after calculating the code below stubbornly stays at (3,3).
The picture also is large (~3500 x ~3000) and needs scrolls bars to fully view it.
Here's my code
public Form1()
{
InitializeComponent();
flowLayoutPanel1.AutoScroll = true;
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
flowLayoutPanel1.Controls.Add(pictureBox1);
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "m")
{
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBox1.Image = Image.FromFile(#"C:\User\Desktop\map.jpg");
pictureBox1.Location = new Point(700, 200);
// ^ The location does not change and stays at (3,3)
// The picturebox is not set as locked
}
Do I need to do something different? Or is the issue with my picture for not allowing me to change the location?
EDIT I found the Solution thanks to the help below. I had to use a panel and place the picturebox within. Below is the code I used.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
panel1.AutoScroll = true;
panel1.Controls.Add(pictureBox1);
pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
pictureBox1.Image = Image.FromFile(#"C:\Desktop\image.jpg");
}
private void button1_Click(object sender, EventArgs e)
{
pictureBox1.Visible = true;
label1.Visible = true;
int Hvalue = panel1.HorizontalScroll.Value;
label1.Text = Hvalue.ToString();
label2.Visible = true;
int Vvalue = panel1.VerticalScroll.Value;
label2.Text = Vvalue.ToString();
if (textBox1.Text == "m")
{
// these are just values that I put in
panel1.HorizontalScroll.Value = 616;
panel1.VerticalScroll.Value = 90;
}
}

How to make buttons backcolor different from eachother

How to make button2 Change its BackColor if its the same Color as in button1?
How does my program work? Well i have to press Start (button5) to mix my Colors, after that i press Check button to see the answer.
As you can see on the Picture, i have two green Buttons and two orange Buttons but I dont want any of the button.backcolors to be equal. They all supposed to be different.
In another words, if button2.backcolor is the same as button1.backcolor, its supposed to mix its backcolor until it gets different. The only Colors that im allowed to use are Green,Red,Blue,Purple,Orange,Yellow
Any ideas how can i achieve this?
Random random = new Random();
List<Color> possibleColors = new List<Color>()
{
Color.Red,
Color.Green,
Color.Orange,
Color.Blue,
Color.Purple,
Color.Yellow,
};
private Color GetRandomColorOfLoist()
{
return possibleColors[random.Next(0, possibleColors.Count)];
}
private void button5_Click(object sender, EventArgs e)
{
button1.BackColor = GetRandomColorOfLoist();
button2.BackColor = GetRandomColorOfLoist();
button3.BackColor = GetRandomColorOfLoist();
button4.BackColor = GetRandomColorOfLoist();
button1.Visible = false;
button2.Visible = false;
button3.Visible = false;
button4.Visible = false;
}
private void button6_Click(object sender, EventArgs e)
{
button1.Visible = true;
button2.Visible = true;
button3.Visible = true;
button4.Visible = true;
}
EDIT 1
Arul Manivannans idea worked but after pressing on start and on check, if i press start again, the game simply crashes. My last question is, how can i hide start button (button5) after clicking on check button (button6)?
My code:
Random random = new Random();
List<Color> possibleColors = new List<Color>()
{
Color.Red,
Color.Green,
Color.Orange,
Color.Blue,
Color.Purple,
Color.Yellow,
};
private Color GetRandomColorOfLoist()
{
int index = random.Next(0, possibleColors.Count);
Color ColorToReturn = possibleColors[index];
possibleColors.Remove(possibleColors[index]);
return ColorToReturn;
}
private void button5_Click(object sender, EventArgs e)
{
button1.BackColor = GetRandomColorOfLoist();
button2.BackColor = GetRandomColorOfLoist();
button3.BackColor = GetRandomColorOfLoist();
button4.BackColor = GetRandomColorOfLoist();
button1.Visible = false;
button2.Visible = false;
button3.Visible = false;
button4.Visible = false;
List<Color> possibleColors = new List<Color>()
{
Color.Red,
Color.Green,
Color.Orange,
Color.Blue,
Color.Purple,
Color.Yellow,
};
}
private void button6_Click(object sender, EventArgs e)
{
button1.Visible = true;
button2.Visible = true;
button3.Visible = true;
button4.Visible = true;
}
EDIT 2
Ok, i got it. Thanks for help
In order to, not to have a repeated color, how about removing the color item from the list?
private Color GetRandomColorOfLoist()
{
int index = random.Next(0, possibleColors.Count);
Color ColorToReturn = possibleColors[index];
possibleColors.Remove(possibleColors[index]);
return ColorToReturn;
}
Just remove the Color returned from GetRandomColorOfLoist out of the possibleColors List. At start of button5_Click you have to refill the List of possible Colors again. Then it should work.

Access controls and their events in same class

I write a custom control.This control create a Form when double mouse double clicked.I also added other controls(button and label etc).But I create textbox1 and textbox2 outside of function.I write events of this controls but this didnt work.Guys I write textbox_press event.Because of this event I can only write digit or letter but I run this program and clicked on my control new form display but this events dont work
namespace Deneme
{
public partial class Direnc : Control
{
public Direnc()
{
InitializeComponent();
}
private string res_name;
private int res_value;
Form form = new Form();
TextBox textBox1 = new TextBox();
TextBox textBox2 = new TextBox();
protected override void OnDoubleClick(EventArgs e)
{
base.OnDoubleClick(e);
//
// label1
//
Label label1 = new Label();
AutoSize = true;
label1.Location = new System.Drawing.Point(27, 35);
label1.Name = "label1";
label1.Size = new System.Drawing.Size(27, 13);
label1.TabIndex = 0;
label1.Text = "İsmi";
//
// label2
//
Label label2 = new Label();
AutoSize = true;
label2.Location = new System.Drawing.Point(13, 89);
label2.Name = "label2";
label2.Size = new System.Drawing.Size(41, 13);
label2.TabIndex = 1;
label2.Text = "Değeri";
//
// textBox1
//
textBox1.Location = new System.Drawing.Point(58, 32);
textBox1.Name = "textBox1";
textBox1.Size = new System.Drawing.Size(100, 22);
textBox1.TabIndex = 2;
//
// textBox2
//
textBox2.Location = new System.Drawing.Point(58, 86);
textBox2.Name = "textBox2";
textBox2.Size = new System.Drawing.Size(100, 22);
textBox2.TabIndex = 3;
//
// button1
//
Button button1 = new Button();
button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
button1.Location = new System.Drawing.Point(64, 145);
button1.Name = "button1";
button1.Size = new System.Drawing.Size(75, 48);
button1.TabIndex = 4;
button1.Text = "Kaydet";
button1.UseVisualStyleBackColor = true;
//
// form
//
form.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
form.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
form.BackColor = System.Drawing.Color.RoyalBlue;
form.ClientSize = new System.Drawing.Size(176, 205);
form.Controls.Add(button1);
form.Controls.Add(textBox2);
form.Controls.Add(textBox1);
form.Controls.Add(label2);
form.Controls.Add(label1);
form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
form.MaximizeBox = false;
form.MinimizeBox = false;
form.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
form.Text = "Direnç";
form.TopMost = true;
form.ResumeLayout(false);
form.PerformLayout();
form.ShowDialog();
button1.Click += new EventHandler(button1_Click);
textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);
textBox2.KeyPress += new KeyPressEventHandler(textBox2_KeyPress);
}
void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !char.IsLetter(e.KeyChar) && !char.IsControl(e.KeyChar)
&& !char.IsSeparator(e.KeyChar);
}
void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if(char.IsLetter(e.KeyChar) && char.IsControl(e.KeyChar))
e.Handled = true;
}
void button1_Click(object sender, EventArgs e)
{
res_name = textBox1.Text;
res_value = Convert.ToInt32(textBox2.Text);
MessageBox.Show(res_name + res_value.ToString());
}
}
Based on your description, I think this is what you want. If not you need to give us more info on what doesn't work.
I think the problem you are facing is that your event are assigned AFTER you call ShowDialog(), which is modal. This means that the method will block the current thread and any code after it will not execute until the form is closed. The solution below was not tested but should hopefully help you solve your issue. Also as I said above in my comment it is safer from a maintenance point of view for you to add an actual form to your project with all the required controls. Dynamic controls can have surprises when you least expect them.
protected override void OnDoubleClick(EventArgs e)
{
base.OnDoubleClick(e);
// The rest of you code here
// The rest of you code here
// The rest of you code here
button1.Click += delegate
{
res_name = textBox1.Text;
res_value = Convert.ToInt32(textBox2.Text);
MessageBox.Show(res_name + res_value.ToString());
};
textBox1.KeyPress += delegate(object sender, KeyPressEventArgs ev)
{
ev.Handled = !char.IsLetter(ev.KeyChar) && !char.IsControl(ev.KeyChar) && !char.IsSeparator(ev.KeyChar);
};
textBox2.KeyPress += delegate(object sender, KeyPressEventArgs ev)
{
if (char.IsLetter(e.KeyChar) && char.IsControl(e.KeyChar))
e.Handled = true;
};
form.ShowDialog(); // <----------- MODAL call, all the code is added BEFORE it
}

C# always false bool [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Can anyone help? I can't work out why the bool floorOne is always set to false when I press button7, even if I press button3 or button1 first. This should be a fairly simple issue, it's only made false when initialised, when button2 is pressed or when button4 is pressed. I have no idea how it's returning to false.
It probably has a relatively simple solution, but I can't find it, thank you for your time.
Edit: When I debug, it shows up as false, I don't know if that information'll help at all. I know a lot of the code probably doesn't need to be included here, but just in case there was an issue in there somehow I thought I should add it in.
Edit2: Fantastic, thank you very much everyone!
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Elevator
{
public partial class Form1 : Form
{
public bool doorsOpen;
public bool floorOne;
public bool groundFloor;
public Form1()
{
InitializeComponent();
pictureBox1.Visible = false; // This is the bottom floor doors closed picture
pictureBox2.Visible = true; // This is the bottom floor doors open picture
pictureBox3.Visible = false; // This is the top floor doors closed picture
pictureBox4.Visible = false; // This is the top floor doors open picture
floorOne = false;
richTextBox1.Text = "Ground floor";
button1.BackColor = Color.Gray;
button2.BackColor = Color.Gray;
button3.Enabled = true;
button4.Enabled = false; // This makes it impossible to click the buttons if
button5.Enabled = false; // the lift is already on that floor, to start with this is the
button6.Enabled = true; // ground floor.
button5.BackColor = Color.Black;
button6.BackColor = Color.Red;
doorsOpen = true;
richTextBox2.Text = "Doors open";
}
public void Form1_Load(object sender, EventArgs e)
{
}
public void button3_Click(object sender, EventArgs e)
{
if (doorsOpen == true)
{
doorsOpen = false;
richTextBox2.Text = "Doors closed";
}
pictureBox1.Visible = false; // This is the bottom floor doors closed picture
pictureBox2.Visible = false; // This is the bottom floor doors open picture
pictureBox3.Visible = true; // This is the top floor doors closed picture
pictureBox4.Visible = false; // This is the top floor doors open picture
bool floorOne = true;
button1.BackColor = Color.Gray;
button2.BackColor = Color.Gray;
richTextBox1.Text = "First floor";
button3.Enabled = false;
button4.Enabled = true;
button5.Enabled = true;
button6.Enabled = false;
button5.BackColor = Color.Red;
button6.BackColor = Color.Black;
}
public void button4_Click(object sender, EventArgs e)
{
if (doorsOpen == true)
{
doorsOpen = false;
richTextBox2.Text = "Doors closed";
}
pictureBox1.Visible = true; // This is the bottom floor doors closed picture
pictureBox2.Visible = false; // This is the bottom floor doors open picture
pictureBox3.Visible = false; // This is the top floor doors closed picture
pictureBox4.Visible = false; // This is the top floor doors open picture
bool floorOne = false;
button1.BackColor = Color.Gray;
button2.BackColor = Color.Gray;
richTextBox1.Text = "Ground floor";
button3.Enabled = true;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = true;
button5.BackColor = Color.Black;
button6.BackColor = Color.Red;
}
public void button7_Click(object sender, EventArgs e)
{
doorsOpen = true;
richTextBox2.Text = "Doors open";
if (floorOne == true)
{
pictureBox1.Visible = false; // This is the bottom floor doors closed picture
pictureBox2.Visible = false; // This is the bottom floor doors open picture
pictureBox3.Visible = false; // This is the top floor doors closed picture
pictureBox4.Visible = true; // This is the top floor doors open picture
}
else if (floorOne != true)
{
pictureBox1.Visible = false; // This is the bottom floor doors closed picture
pictureBox2.Visible = true; // This is the bottom floor doors open picture
pictureBox3.Visible = false; // This is the top floor doors closed picture
pictureBox4.Visible = false; // This is the top floor doors open picture
}
}
public void button1_Click(object sender, EventArgs e)
{
if (doorsOpen == true)
{
doorsOpen = false;
richTextBox2.Text = "Doors closed";
}
pictureBox1.Visible = false; // This is the bottom floor doors closed picture
pictureBox2.Visible = false; // This is the bottom floor doors open picture
pictureBox3.Visible = true; // This is the top floor doors closed picture
pictureBox4.Visible = false; // This is the top floor doors open picture
bool floorOne = true;
button1.BackColor = Color.Yellow;
button2.BackColor = Color.Gray;
richTextBox1.Text = "First floor";
button3.Enabled = false;
button4.Enabled = true;
button5.Enabled = true;
button6.Enabled = false;
button5.BackColor = Color.Red;
button6.BackColor = Color.Black;
}
public void button2_Click(object sender, EventArgs e)
{
if (doorsOpen == true)
{
doorsOpen = false;
richTextBox2.Text = "Doors closed";
}
pictureBox1.Visible = true; // This is the bottom floor doors closed picture
pictureBox2.Visible = false; // This is the bottom floor doors open picture
pictureBox3.Visible = false; // This is the top floor doors closed picture
pictureBox4.Visible = false; // This is the top floor doors open picture
bool floorOne = false;
button1.BackColor = Color.Gray;
button2.BackColor = Color.Yellow;
richTextBox1.Text = "Ground floor";
button3.Enabled = true;
button4.Enabled = false;
button5.Enabled = false;
button6.Enabled = true;
button5.BackColor = Color.Black;
button6.BackColor = Color.Red;
}
}
}
Because in some of your methods you are declaring a new floorOne variable, instead of modifying the existing Form1's field.
Replace
bool floorOne = true;
with
floorOne = true;
In Button3 and button4 click event remove the declaration of bool variable and just update the value of it. Because you already have defined the bool floorOne so you don't need to declare it again.
Update
bool floorOne = true;
To
floorOne = true;
this realy have a simple solution.
Lets say, you click button1 and you want to set floorOne to true, but what you realy do is:
create new LOCAL boolean value floorOne and set it to true (bool floorOne = true;). But this doesnt change your GLOBAL value floorOne. It has the same name, but that doesnt matter in this case.
Delete the "bool" text in your code in button1 and then "floorOne" will be your global value.
new code for button1 (same change for all other buttons):
public void button1_Click(object sender, EventArgs e)
{
if (doorsOpen == true)
{
doorsOpen = false;
richTextBox2.Text = "Doors closed";
}
pictureBox1.Visible = false; // This is the bottom floor doors closed picture
pictureBox2.Visible = false; // This is the bottom floor doors open picture
pictureBox3.Visible = true; // This is the top floor doors closed picture
pictureBox4.Visible = false; // This is the top floor doors open picture
floorOne = true; // HERE IS THE CHANGE
button1.BackColor = Color.Yellow;
button2.BackColor = Color.Gray;
richTextBox1.Text = "First floor";
button3.Enabled = false;
button4.Enabled = true;
button5.Enabled = true;
button6.Enabled = false;
button5.BackColor = Color.Red;
button6.BackColor = Color.Black;
}
Try this:
public partial class Form1 : Form
{
public bool floorOne; // global value (automatically set to false (default value of false)
public Form1()
{
InitializeComponent();
}
public void button3_Click(object sender, EventArgs e)
{
bool floorOne = true; // new local value named floorOne (doesn change your global value)
MessageBox.Show("value of global floorOne is: " + this.floorOne.ToString());
// this.floorOne is your global value (try to find something about "this.")
this.floorOne = true;
MessageBox.Show("value of global floorOne is: " + this.floorOne.ToString());
}
}

Categories