problems with user control

  • Thread starter Thread starter Jason Winters
  • Start date Start date
J

Jason Winters

I have implemented a user control in c# .net 2002 but the problem is that
the control keeps disappearing off of the form for no reason. I am not
making any changing to the form or the control. It disappears off of all
the forms that it is on. Any hints why this is happening?
 
Jason,

Jason Winters said:
I have implemented a user control in c# .net 2002 but the problem is that
the control keeps disappearing off of the form for no reason. I am not
making any changing to the form or the control. It disappears off of all
the forms that it is on. Any hints why this is happening?


Can you post the code for the user control?

TT
 
using System;

using System.Collections;

using System.ComponentModel;

using System.Drawing;

using System.Data;

using System.Windows.Forms;

namespace Biller

{

/// <summary>

/// Summary description for StoreSelector.

/// </summary>

public class StoreSelector : System.Windows.Forms.UserControl

{

private static int index;

public System.Windows.Forms.ComboBox comboBox1;

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

public StoreSelector()

{

// This call is required by the Windows.Forms Form Designer.

InitializeComponent();


// TODO: Add any initialization after the InitForm call

}

public static void setIndex(int i)

{

index = i;

}

public static int getIndex()

{

return index;

}

public static Store getSelectedStore()

{


return StoresCollection.getStoreAtIndex(index);


}

private void storeList()

{

this.comboBox1.Items.Clear();

ArrayList stores = new ArrayList(StoresCollection.getStores());

Object storesNames = new Object[stores.Count];

for(int x=0; x< stores.Count;x++)

{

this.comboBox1.Items.Add(((Store)stores[x]).storeName);

}


if(this.comboBox1.Items.Count > 0)

this.comboBox1.SelectedIndex = index;



}

public void load()

{

try

{

this.storeList();

this.comboBox1.SelectedIndex = index;

}

catch{}

}



/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if(components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Component Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.comboBox1 = new System.Windows.Forms.ComboBox();

this.SuspendLayout();

//

// comboBox1

//

this.comboBox1.ItemHeight = 13;

this.comboBox1.Name = "comboBox1";

this.comboBox1.Size = new System.Drawing.Size(176, 21);

this.comboBox1.TabIndex = 0;

this.comboBox1.SelectedIndexChanged += new
System.EventHandler(this.comboBox1_SelectedIndexChanged);

//

// StoreSelector

//

this.Controls.AddRange(new System.Windows.Forms.Control[] {

this.comboBox1});

this.Name = "StoreSelector";

this.Size = new System.Drawing.Size(176, 24);

this.Resize += new System.EventHandler(this.StoreSelector_Resize);

this.ResumeLayout(false);

}

#endregion

private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs
e)

{

index = this.comboBox1.SelectedIndex;

}

public void addSelectedEvent(System.EventHandler eh)

{

this.comboBox1.SelectedIndexChanged += new System.EventHandler(eh);


}

private void StoreSelector_Resize(object sender, System.EventArgs e)

{

this.comboBox1.Size = this.Size;

}

public void setSelectedIndex(int i)

{

this.comboBox1.SelectedIndex = i;

index = i;

}

}



}
 
Hi Jason

Firstly, I think that the Designer calls the constructor of your user
control when the designer displays the control. So, if you have any
errors in your constructor, then the designer will rip the control off
your form. In a way, the designer is like a moody teenager. It just
throws its hands in the air and says 'stuff this' !

I found that my designer settled right down when I did these things:
(1) Ensured that there is error handling in every user controls
constructor
(2) Put the user controls into their own project
(3) If I was going to change the user controls:
(a) Close any form that has the user controls on it
(b) Change the user controls in their own project
(c) Compile the user controls project
(d) Re-open any forms that have the user controls on them

Oh, one other thing I have found - changing from debug to release mode
(or vice versa) whilst you have a form open that contains user controls
..... has caused me problems as well.

Hope this Helps
Bill
 
Jason,

"Jason Winters" <[email protected]> schreef in bericht
[...]

There is one question: Where does the control disappear?

When you are designing the form, or at run time?

TT.
 
Another thing I wouldn't do is making that 'index' static. I see no reason
for that.

TT

Jason Winters said:
using System;

using System.Collections;

using System.ComponentModel;

using System.Drawing;

using System.Data;

using System.Windows.Forms;

namespace Biller

{

/// <summary>

/// Summary description for StoreSelector.

/// </summary>

public class StoreSelector : System.Windows.Forms.UserControl

{

private static int index;

public System.Windows.Forms.ComboBox comboBox1;

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

public StoreSelector()

{

// This call is required by the Windows.Forms Form Designer.

InitializeComponent();


// TODO: Add any initialization after the InitForm call

}

public static void setIndex(int i)

{

index = i;

}

public static int getIndex()

{

return index;

}

public static Store getSelectedStore()

{


return StoresCollection.getStoreAtIndex(index);


}

private void storeList()

{

this.comboBox1.Items.Clear();

ArrayList stores = new ArrayList(StoresCollection.getStores());

Object storesNames = new Object[stores.Count];

for(int x=0; x< stores.Count;x++)

{

this.comboBox1.Items.Add(((Store)stores[x]).storeName);

}


if(this.comboBox1.Items.Count > 0)

this.comboBox1.SelectedIndex = index;



}

public void load()

{

try

{

this.storeList();

this.comboBox1.SelectedIndex = index;

}

catch{}

}



/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if(components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Component Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.comboBox1 = new System.Windows.Forms.ComboBox();

this.SuspendLayout();

//

// comboBox1

//

this.comboBox1.ItemHeight = 13;

this.comboBox1.Name = "comboBox1";

this.comboBox1.Size = new System.Drawing.Size(176, 21);

this.comboBox1.TabIndex = 0;

this.comboBox1.SelectedIndexChanged += new
System.EventHandler(this.comboBox1_SelectedIndexChanged);

//

// StoreSelector

//

this.Controls.AddRange(new System.Windows.Forms.Control[] {

this.comboBox1});

this.Name = "StoreSelector";

this.Size = new System.Drawing.Size(176, 24);

this.Resize += new System.EventHandler(this.StoreSelector_Resize);

this.ResumeLayout(false);

}

#endregion

private void comboBox1_SelectedIndexChanged(object sender,
System.EventArgs e)

{

index = this.comboBox1.SelectedIndex;

}

public void addSelectedEvent(System.EventHandler eh)

{

this.comboBox1.SelectedIndexChanged += new System.EventHandler(eh);


}

private void StoreSelector_Resize(object sender, System.EventArgs e)

{

this.comboBox1.Size = this.Size;

}

public void setSelectedIndex(int i)

{

this.comboBox1.SelectedIndex = i;

index = i;

}

}



}
 
Index does need to be static to get the effect I need

TT (Tom Tempelaere) said:
Another thing I wouldn't do is making that 'index' static. I see no reason
for that.

TT

Jason Winters said:
using System;

using System.Collections;

using System.ComponentModel;

using System.Drawing;

using System.Data;

using System.Windows.Forms;

namespace Biller

{

/// <summary>

/// Summary description for StoreSelector.

/// </summary>

public class StoreSelector : System.Windows.Forms.UserControl

{

private static int index;

public System.Windows.Forms.ComboBox comboBox1;

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

public StoreSelector()

{

// This call is required by the Windows.Forms Form Designer.

InitializeComponent();


// TODO: Add any initialization after the InitForm call

}

public static void setIndex(int i)

{

index = i;

}

public static int getIndex()

{

return index;

}

public static Store getSelectedStore()

{


return StoresCollection.getStoreAtIndex(index);


}

private void storeList()

{

this.comboBox1.Items.Clear();

ArrayList stores = new ArrayList(StoresCollection.getStores());

Object storesNames = new Object[stores.Count];

for(int x=0; x< stores.Count;x++)

{

this.comboBox1.Items.Add(((Store)stores[x]).storeName);

}


if(this.comboBox1.Items.Count > 0)

this.comboBox1.SelectedIndex = index;



}

public void load()

{

try

{

this.storeList();

this.comboBox1.SelectedIndex = index;

}

catch{}

}



/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if(components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Component Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.comboBox1 = new System.Windows.Forms.ComboBox();

this.SuspendLayout();

//

// comboBox1

//

this.comboBox1.ItemHeight = 13;

this.comboBox1.Name = "comboBox1";

this.comboBox1.Size = new System.Drawing.Size(176, 21);

this.comboBox1.TabIndex = 0;

this.comboBox1.SelectedIndexChanged += new
System.EventHandler(this.comboBox1_SelectedIndexChanged);

//

// StoreSelector

//

this.Controls.AddRange(new System.Windows.Forms.Control[] {

this.comboBox1});

this.Name = "StoreSelector";

this.Size = new System.Drawing.Size(176, 24);

this.Resize += new System.EventHandler(this.StoreSelector_Resize);

this.ResumeLayout(false);

}

#endregion

private void comboBox1_SelectedIndexChanged(object sender,
System.EventArgs e)

{

index = this.comboBox1.SelectedIndex;

}

public void addSelectedEvent(System.EventHandler eh)

{

this.comboBox1.SelectedIndexChanged += new System.EventHandler(eh);


}

private void StoreSelector_Resize(object sender, System.EventArgs e)

{

this.comboBox1.Size = this.Size;

}

public void setSelectedIndex(int i)

{

this.comboBox1.SelectedIndex = i;

index = i;

}

}



}
 
Back
Top