Panel within another Panel - c#

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.

Related

Click Event not fired in User Control when multiple instances are created

I've a user control with 2 labels and two textboxes. When the label is clicked the textbox's visible prop is set to true. Here's the code I've used:
private void label_Heading_Click(object sender, EventArgs e)
{
label_Heading.Visible = false;
textBox_Heading.Text = label_Heading.Text;
textBox_Heading.Visible = true;
textBox_Heading.Focus();
}
After the textbox lose focus, it's visible prop is set to false and the label is updated with the text. Code:
private void textBox_Heading_Leave(object sender, EventArgs e)
{
textBox_Heading.Visible = false;
if(textBox_Heading.Text != "")
label_Heading.Text = textBox_Heading.Text;
label_Heading.Visible = true;
}
The code to create user controls on click:
private void label1_Click(object sender, EventArgs e)
{
TaskCard _taskCard = new TaskCard(++TOTAL_ITEM_COUNT, PanelName);
panel_DeletedItem.Controls.Add(_taskCard);
panel_DeletedItem.Refresh();
}
These code work fine when a single user control of this type is added to a panel. But if I add more than one, the code works only for the first user control, but it wont work for the new ones, although the event is fired for every user control. What am I missing here? Please suggest.
If I add a mbox to this code, the mbox is displayed for any control, but the rest of the code won't work, except for the first one.
private void label_Heading_Click(object sender, EventArgs e)
{
MessageBox.Show("Test"); // this will display, but the rest of the code is not executed or changes are not visible, i.e., the teboxes are not displayed even if I click the labels
label_Heading.Visible = false;
textBox_Heading.Text = label_Heading.Text;
textBox_Heading.Visible = true;
textBox_Heading.Focus();
}

Overlapping of controls

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 :)

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;}

C# what the methods do

protected void Page_Load(object sender, EventArgs e)
{
Panel1.Visible = True;
Panel2.Visible = false;
LoadQuestion(); // auto choose question from database and add into Panel1
LoadQuestion1(); // auto choose question from database and add into Panel2
}
When I start my program, my form automatically loads questions into my textbox and radio button list. I click link button2 to make my Panel1 visible = false and Panel2 visible = true to continue answering question. But after I clicked the link button 2 or 1, it will go back to the Page_Load() method and causes my questions keep on changing.
You should check if it's a postback. You want to execute this only on first load.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack) {
Panel1.Visible = True;
Panel2.Visible = false;
LoadQuestion(); // auto choose question from database and add into Panel1
LoadQuestion1(); // auto choose question from database and add into Panel2
}
}
Source
This is because the Load event happens every time the server processes a request to your page.
There are two kinds of request: initial page load (when you go to the URL) and postback (when you click a button). What you do in your Page_Load method is kinda initialization, so it should be done only initially, but not during the postback.
protected void Page_Load(object sender, EventArgs e)
{
if( !IsPostBack )
{
// The code here is called only once - during the initial load of the page
}
// While the code here is called every time the server processes a request
}
Try:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Panel1.Visible = True;
Panel2.Visible = false;
LoadQuestion(); // auto choose question from database and add into Panel1
LoadQuestion1(); // auto choose question from database and add into Panel2
}
}

Categories