Overlapping of controls - c#

I'm fairly new at coding so I need your help!!
I have controls overlapping. I have two richtextBoxes, one pictureBox and two buttons where richtextBoxes have the same location and size. The pictureBox overlaps on the left half of richtextBox. All but buttons are hidden when the form initializes. Here's how the code flows:
`
private void Button1_Click(Object sender, EventArgs e)
{
richtextBox2.Visible = false;
richtextBox1.Visible = true;
pictureBox.Visible = true;
}
private void Button2_Click(Object sender, EventArgs e)
{
richtextBox1.Visible = false;
richtextBox2.Visible = true;
pictureBox1.Visible = true;
}
Button1_click works fine but when I hit Button2_click my richtextBox2 overlaps on the pictureBox1 whereas I want the pictureBox1 to always overlap all richtextBoxes.

Go to the [Design] view of your form, right click pictureBox1 and select "Bring to Front".
Hope that helps :)

Related

Panels behavior : separation

I have 3 panels on top of each others and 3 buttons. What I want to achieve is with every button click its correspondent panel appears, currently I am using panel.Visible = true; and panel.Visible = false; but since every element over a panel in a WFA is considered a child of that panel all I get is either they are all visible or they are all invisible.
Q: How to make each panel behave separately?
This is the visibility control code:
private void btnHome_Click(object sender, EventArgs e)
{
panelHome.Visible = true;
panelContact.Visible = false;
panelOther.Visible = false;
}
private void btnContact_Click(object sender, EventArgs e)
{
panelHome.Visible = false;
panelContact.Visible = true;
panelOther.Visible = false;
}
private void btnOther_Click(object sender, EventArgs e)
{
panelHome.Visible = false;
panelContact.Visible = false;
panelOther.Visible = true;
}
This issue is easly solved using the graphical user interface:
You just need to carefully place every panel on top of the previous one until the blue guidelines appear.
PS: You need to check 2 guidelines: one vertical (left or right) and one horizontal (top or bottom)

How to change the backgound color and deactivate the inactive botton

We have 2 buttons, button1 is called (green) and button2 is called (blue).
Clicking on the button (Green) changes the backcolor to green and deactivates the (Blue) button, while clicking on the button (blue) changes the backcolor to blue and deactivates the (greenn) button.
At the start of the application, the button (Blue) should be deactivated :)
private void button1_Click(object sender, EventArgs e)
{
BackColor = Color.Green;
}
private void button2_Click(object sender, EventArgs e)
{
BackColor = Color.Blue;
}
Excellent! We can work with your code now :)
As for changing the button backcolors and enabled states, your existing code should look like below. You must define the object that you are trying to manipulate, even when you are in a method fired by that object. Also, I added the option of turning the opposite button white so that when it is clicked again in the future it can change to the desired color again.
Since each button sets its opposite as not enabled and white, you will get the effect that only one button (the one you clicked last) can be active at a time.
Let me know if this has helped!
private void button1_Click(object sender, EventArgs e)
{ button1.BackColor = Color.Green;
button1.Enabled = true;
button2.BackColor = Color.White;
button2.Enabled = false;}
private void button2_Click(object sender, EventArgs e)
{ button2.BackColor = Color.Blue;
button2.Enabled = true;
button1.BackColor = Color.White;
button1.Enabled = false; }
As for your blue button being disabled when form is activated, you should find this event in your code (I have Form1 as your form name, change it if you have named your form something else). Just set the button as disabled at this point, or you can have it set as enabled = false as the default in the properties tab.
private void Form1_Activated(object sender, System.EventArgs e)
{button2.Enabled = false;}

Toggle Group Box Visibility

I have two Group Boxes grpMeter and grpTag. I have to place grpMeter over grpTag.. both need same location and size..
On button click, I have to make them visible alternately. Is it possible? I tried many times but only 1 group box becomes visible. Maybe because of the overlapping problem. I tried with panel, but the same problem arises. Is there any solution?
public void ShowMeter()
{
grpMeter.Visible = true;
grpTags.Visible = false;
}
public void ShowTag()
{
grpTags.Visible = true;
grpMeter.Visible = false;
}
Place both group boxes next to each other so that they don't overlap and see if it works then. If you made it work, don't move the one group box with the mouse, but select it only and then set the coordinates manually in the Properties list.
That way you can prevent the one group box from accidentially becoming the child of the other group box.
Try this logic inside a button_click event:
private void btn_Click(object sender, EventArgs e)
{
if (grpTags.Visible)
ShowMeter();
else
ShowTag();
}
Try this:
private void button_Click(object sender, EventArgs e)
{
grpMeter.Visible = !grpMeter.Visible;
grpTags.Visible = !grpTags.Visible;
}
See the code below. The button Click will toggle visibilty. Also it's important that you set one of the groupboxes as visible and the other one as invisible in your constructor
using System;
using System.Windows.Forms;
namespace TestForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//This is important. Set one of them to be Visible and the other one to be invisible
grpMeter.Visible = false;
grpTags.Visible = true;
}
private void button1_Click(object sender, EventArgs e)
{
grpMeter.Visible = !grpMeter.Visible;
grpTags.Visible = !grpTags.Visible;
}
}
}
I am not sure but what you are looking for seems like FlowLayoutPanel. Then you can put group boxes next to each other and positioning will be handled automatically. This prevents accidentally putting one GroupBox into another or shifting locations. Also provides an easier working at design time.
One of the good way is to use RadioButton. Take two Radio buttons and place it inside a groupbox.
Something like this would work:
private void rdMeter_CheckedChanged(Object sender, EventArgs e)
{
grpMeter.Visible = rdMeter.Checked;
grpTag.Visible = !rdMeter.Checked;
}
private void rdTag_CheckedChanged(Object sender, EventArgs e)
{
grpTag.Visible = rdTag.Checked;
grpMeter.Visible = !rdTag.Checked;
}

Panel within another Panel

I have 2 panels. Each start at the same location.(Let's say 10,10) and have the same size.
I have 2 buttons. One shows the first panel and the other shows the second panel.
My code is:
private void button1_Click(object sender, EventArgs e)
{
panel1.Visible = true;
panel2.Visible = false;
}
private void button2_Click(object sender, EventArgs e)
{
panel1.Visible = false;
panel2.Visible = true;
}
When I press button one the the first panel shows up, but when click button 2 the second panel doesn't show up. The panel's visible properties are initially false..
What is wrong?
Make sure that Panel2 is not a child window of Panel1.

How to expand the picturebox into another form?

Currently, my application displays 6 picture boxes, each displaying an picture which is being constantly updating.
Now, I want upon clicking any picture box that picture box extends and fill up the whole screen just showing that chosen picture box.
Is this possible? Must i create another form to do this?
Thanks In Advance,
Perumal
in the onclick event for each the picture box (they can all point to this same method)
picturebox_Click(object sender .....)
{
PictureBox pb= (PictureBox)sender;
if (pb.dock==DockStyle.None)
{
pb.dock=DockStyle.Fill;
pb.BringToFront();
}
else
pb.dock=DockStyle.None;
}
Not seeing any code, here is how you can programmatically change a picture box on click.
pictureBox1.Dock = DockStyle.Fill
So you need to create a on click event handler and call your picture box's Dock function like the above.
update in response to comments
There is a DockStyle.None to revert the picture back to original size.
If i understand you correctly, you want to have 6 pictures and then when you click one it fills, click again, shrinks, click another one, fills etc etc...
To do this, you would use the Dock and Visible properties on the picture boxes. Now it also seems as if you are asking how to actually write the code. Well if you show some code, I could give pointers, with nothing to go on the way I'd approach it is to:
Put all your picture boxes in a list and assign a state to them Big or Small.
Write a OnClick for each picture box to change the state of the picture box clicked on.
Each OnClick then calls a helper function that iterates through each picture box in the list and hides the small one and DockStyle.Fill the big one.
Does the above algorithm accomplish what you need?
try something like this. the code is not re factored but I am sure you can do that
private bool isfill = false;
private void pictureBox1_Click(object sender, EventArgs e)
{
if (!isfill)
{
pictureBox1.Dock = DockStyle.Fill;
pictureBox2.Visible = false;
isfill = true;
}
else
{
pictureBox1.Dock = DockStyle.None;
pictureBox2.Visible = true;
isfill = false;
}
}
private void pictureBox2_Click(object sender, EventArgs e)
{
if (!isfill)
{
pictureBox2.Dock = DockStyle.Fill;
isfill = true;
pictureBox1.Visible = false;
}
else
{
pictureBox2.Dock = DockStyle.None;
isfill = false;
pictureBox1.Visible = true;
}

Categories