Strange Window Control Problem

  • Thread starter Thread starter chau_fai
  • Start date Start date
C

chau_fai

I have a window Form called "Form2". There is only one button and a
panel returned from a static method of the "Coord" in this form.
In the panel returned, there is a button that can choose image from
"OpenFileDialog" and then display it in a picturebox.
This actually works properly before the button1 is clicked.
Button1 is a button to open a new form and again it gets a panel
returned from the static method inside "Coord" and perform the same
function as methioned above.

Now, the new form being opened by Button1 works properly. It can show
a image selected by the openfiledialog. When this form is closed and
return back to "Form2" .... the controls in the original panel doesn't
work properly. The picturebox display nothing although a file is
selected. It seems that the event is partly fired only. But the
Button1 in the Form2 still works properly that can open a new form and
the new form is working very well also.
Actually .... i don't know what is the problem ....


//Form 2

private void Form2_Load(object sender, System.EventArgs e)
{
this.Controls.Add(Coord.ReturnPanel();
}

private void button1_Click(object sender, System.EventArgs e)
{
Form a = new Form();
a.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
a.ClientSize = new Size(790,550);
a.Controls.Add(Coord.ReturnPanel());
//a.Location = new System.Drawing.Point(500, 500);
a.StartPosition = FormStartPosition.CenterScreen;
a.Name = "frmAddMember";
a.FormBorderStyle = FormBorderStyle.Fixed3D;
a.Text = "New Member";
a.ShowDialog();
}

//Coord
private static Button test;
private static PictureBox picTest;
private static Panel pan;
private static OpenFileDialog open;

public static Panel ReturnPanel()
{
pan = new Panel();
test = new Button();
picTest = new PictureBox();
open = new OpenFileDialog();

pan.Location = new System.Drawing.Point(8, 8);
pan.Name = "panelMember";
pan.Size = new System.Drawing.Size(784, 536);
pan.Controls.Add(test);
pan.Controls.Add(picTest);

picTest.Location = new System.Drawing.Point(552, 72);
picTest.Name = "this.picUser";
picTest.Size = new System.Drawing.Size(192, 200);
picTest.TabIndex = 1;
picTest.TabStop = false;


test.Location = new System.Drawing.Point(696, 24);
test.Size = new System.Drawing.Size(24, 24);
test.Click += new System.EventHandler(btn_Click);

return pan;
}

private static void btn_Click(object sender, System.EventArgs e)
{

open.Title = "Select a image file";
open.Filter = "Image Files(*.BMP;*.JPG;)|*.BMP;*.JPG;

if(open.ShowDialog() == DialogResult.OK)
{
Bitmap userImage = new Bitmap(open.FileName);
picTest.Image = (Image)userImage;
}
}

Any Helps will be appreciated ~~
 
Hi chau_fai,
I tested your code so here is what I found
Now, the new form being opened by Button1 works properly. It can show
a image selected by the openfiledialog. When this form is closed and
return back to "Form2" .... the controls in the original panel doesn't
work properly. The picturebox display nothing although a file is
selected.

In my test the PictureBox in Form2 cannot be changed if the modal form has
been opened at least once. That is if I first open a picture on the Form2
and then click on button1 I see the same picture and cannot change it. If I
first click on button1 I cannot load a picture in Form2.

So the problem is here
private static void btn_Click(object sender, System.EventArgs e)
{
if(open.ShowDialog() == DialogResult.OK)
{
Bitmap userImage = new Bitmap(open.FileName);
picTest.Image = (Image)userImage;
}
}

You load the picture always in the last created picture box because picTest
is a static variable, which value you change each time you call
ReturnPanel().

So, if you click the button on the Form2 before clicking on button1 the last
panel created is the panel you set in Form2 and picTest references the
PictureBox in the Form2. When you click on button1, though, you call
ReturnPanel and from this moment on picTest references the new PictureBox
which you see on the modal form. You may close that form but picTest still
references that PictureBox. Thus, next time you click the button on Form2 it
loads the picture in that PictureBox, wich is hidden BTW.

My suggestion is to change the logic of your prorgram. Create one user
control that has the picture box and the button. Build-in all picture
loading logic in this control (no static methods and variables) When you
need new contorol just create one with *new* operator and add the control or
better use designer to add the control to forms. Actually this might be not
right solution for you, but I don't know what your goals are. But judging by
the example you posted it will work for you.
 
Thx For Your Help ~~

I have already changed the controls to be a normal one that mean "all
static methods and controls" become back to "private || public " .....

i need to create a new object of "coord" to use the "ReturnPanel"
method ...
but the result is just the same as before ....

how can i deal with this problem except from the suggestion you
mentioned ?
 
"Stoitcho Goutsev \(100\) [C# MVP]" <[email protected]> wrote in message news:<[email protected]>...

AnyBody Here can give me some guides ?

I have already changed the controls to be a normal one that mean "all
static methods and controls" become back to "private || public " .....

i need to create a new object of "coord" to use the "ReturnPanel"
method ...
but the result is just the same as before ....

how can i deal with this problem except from the suggestion you
mentioned ?
 
Hi chau_fai,
Can you post your new code? I want to see what you have changed. I can't
quite well understand what your changes are.

--
B\rgds
100 [C# MVP]

chau_fai said:
(e-mail address removed) (chau_fai) wrote in message
"Stoitcho Goutsev \(100\) [C# MVP]" <[email protected]> wrote in message
 
All the controls in "Coord" are not static now ....
i need to create an object of "Coord" first ....
but the result was again same as before

//Form 2

private void Form2_Load(object sender, System.EventArgs e)
{
this.Controls.Add(Coord.ReturnPanel();
}

private void button1_Click(object sender, System.EventArgs e)
{
Form a = new Form();
a.AutoScaleBaseSize = new System.Drawing.Size(5, 15);
a.ClientSize = new Size(790,550);
a.Controls.Add(new Coord().ReturnPanel()); ---->>>>>>>> the main difference
//a.Location = new System.Drawing.Point(500, 500);
a.StartPosition = FormStartPosition.CenterScreen;
a.Name = "frmAddMember";
a.FormBorderStyle = FormBorderStyle.Fixed3D;
a.Text = "New Member";
a.ShowDialog();
}

//Coord
private Button test;
private PictureBox picTest;
private Panel pan;
private OpenFileDialog open;

public Panel ReturnPanel()
{
pan = new Panel();
test = new Button();
picTest = new PictureBox();
open = new OpenFileDialog();

pan.Location = new System.Drawing.Point(8, 8);
pan.Name = "panelMember";
pan.Size = new System.Drawing.Size(784, 536);
pan.Controls.Add(test);
pan.Controls.Add(picTest);

picTest.Location = new System.Drawing.Point(552, 72);
picTest.Name = "this.picUser";
picTest.Size = new System.Drawing.Size(192, 200);
picTest.TabIndex = 1;
picTest.TabStop = false;


test.Location = new System.Drawing.Point(696, 24);
test.Size = new System.Drawing.Size(24, 24);
test.Click += new System.EventHandler(btn_Click);

return pan;
}

private void btn_Click(object sender, System.EventArgs e)
{

open.Title = "Select a image file";
open.Filter = "Image Files(*.BMP;*.JPG;)|*.BMP;*.JPG;

if(open.ShowDialog() == DialogResult.OK)
{
Bitmap userImage = new Bitmap(open.FileName);
picTest.Image = (Image)userImage;
}
}
 
All the controls in "Coord" are not static now ....
i need to create an object of "Coord" first ....
but the result was again same as before
HI Stoitcho Goutsev,

I have found my way to solve the problem ...
I use the "Coord" to inherit the panel class...

Each time when a forms need such kind of interface ... it will create
a new object. This works without any problem since the "Coord" class
has its own interface as it inherits from the panel class.

Actually, the solution is very simply but i don't why i had made it so
complicated as the method i used before ...

thx for your guide anyway ...
 
Hi chau_fai,
Glad you managed to solve your problem. However you might consider using
UserControl. You won't have some of the panel's functionality, but you'll
have some designer support. However you should choose what is best for you.

--
B\rgds
100 [C# MVP]

chau_fai said:
(e-mail address removed) (chau_fai) wrote in message
"Stoitcho Goutsev \(100\) [C# MVP]" <[email protected]> wrote in message
All the controls in "Coord" are not static now ....
i need to create an object of "Coord" first ....
but the result was again same as before
HI Stoitcho Goutsev,

I have found my way to solve the problem ...
I use the "Coord" to inherit the panel class...

Each time when a forms need such kind of interface ... it will create
a new object. This works without any problem since the "Coord" class
has its own interface as it inherits from the panel class.

Actually, the solution is very simply but i don't why i had made it so
complicated as the method i used before ...

thx for your guide anyway ...
 
Back
Top