I am having a windows form application which contains a panel. I want to access that panel when I call a specific method in a class file and change the status of the panel to visible.
I tried to create an object of the form within the method and change the status. It does not gives an error, but it does not display the panel.
public class Compare
{
public void Comp(a,b)
{
form1 f=new form1();
if(a
f.panel1.visible=true;
else if(a>b)
f.panel2.visible=true;
else
f.panel3.visible=true;
}
}
Can anyone tell me how to do this in C#?
Thanks in advance.
FroglegPosted Jun 21, 2011, 3:22 AM
public partial class Form1 : Form
{
Compare cmp = new Compare();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
hidePanels();
}
public void hidePanels()
{
panel1.Visible = false;
panel2.Visible = false;
panel3.Visible = false;
}
private void button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text) | string.IsNullOrEmpty(textBox2.Text))
{
return;
}
hidePanels();
int a = Convert.ToInt32(textBox1.Text);
int b = Convert.ToInt32(textBox2.Text);
cmp.Comp(a, b, panel1, panel2, panel3);
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar < '0' || e.KeyChar > '9')
{
e.Handled = true;
}
}
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar < '0' || e.KeyChar > '9')
{
e.Handled = true;
}
}
}
in class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HiddenPanels
{
class Compare
{
public void Comp(int a, int b, Panel p1, Panel p2, Panel p3)
{
if (a < b)
{
p1.Visible = true;
}
else if (a > b)
{
p2.Visible = true;
}
else
{
p3.Visible = true;
}
}
}
}