I have a WinForms form with a few text boxes on it. I need to change some of them to a custom version of TextBox (I had to override a function). Since the whole new control is just this one function, I just did it all in a few lines of code in same as a contained-class in my form. Now I want to change the instance of the designer-generated text box to an instance of this one in the code. Can this be done somehow?
If it can't, and I have to add a whole new UserControl item to my project, can I at least substitute it in the designer without losing all my property changes and event handler bindings?
Description
Check out the <YourFormName>.Designer.cs file. This is the file the Designer has created. You can change your TextBox to your custom this way.
Sample
#region Windows Form Designer generated code
private void InitializeComponent()
{
// change this
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(0, 0);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 0;
//
// Form1
//
// ...
}
#endregion
// change this
private System.Windows.Forms.TextBox textBox1;
Go to the InitializeComponent() method inside your form class or the form's designer.cs class. In this method, you would find the usage of your textbox controls, now go to the definition of those usages and change the type of the textbox variables to your custom implemented textbox types.
Related
In my winform control I am adding picture box and assigning an image from the Resources. This works great, however I need to change the image based on business logic and this is where the issue begins. I am setting up the Image to the new one but it refuses to get updated. I have also tried to use Refresh, Update or Invalidate on a picturebox again without any success. How to change the picturebox image dynamically ? Am I using the right control?
Below is Designer autogenerated code which works fine:
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Image = global::UnumIDOutlookAddIn.Properties.Resources.MyImage;
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(616, 86);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
this.pictureBox1.TabIndex = 1;
this.pictureBox1.TabStop = false;
//
// WinformComponent
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.pictureBox1);
this.Name = "WinformComponent";
this.Size = new System.Drawing.Size(616, 86);
(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
However, later on I am trying to update the Image, and it never updates. I have tried to differnt order on the last three lines without any success as well.
mycomponent.pictureBox1.Image = Resources.AnotherImage;
mycomponent.pictureBox1.Invalidate(); <-- Tried many configuratons
mycomponent.pictureBox1.Update();
mycomponent.pictureBox1.Refresh();
** I'm completely guessing as to what your problem is. **
Most likely you're doing something like this:
Form1 mycomponent = new Form1(); // Form1 is just an example
mycomponent.pictureBox1.Image = Resources.AnotherImage;
You're making a NEW form and updating the pictureBox on that Form, a Form that is never actually displayed.
If you want to update the Form that is ALREADY VISIBLE on your screen, then you need a reference to that specific form.
Getting a reference to the visible form can be done in various ways. Understanding the CONTEXT of your program will allow us to give you the easiest method to do this.
What forms are involved? Which forms create which ones, in what order? These things matter...
I use this code in my child form
MainMenu f = new MainMenu();
f.tbUserName.Text = "MY TEXT";
so I want to display text in tbUserName but it doesn't show me.
** I don't want to open new window with this code
MainMenu f = new MainMenu();
f.Show();
That code won't work because you're creating a new instance of the object therefore only the new instance will be affected. If you want to affect an open window, make the Label static in the form.designer.cs file, like so:
private TextBox tbUserName;
becomes
public static TextBox tbUserName;
Then you remove remove "this." in front of any mention of "tbUserName".
this.tbUserName.Size = new Size();
becomes
tbUserName.Size = new Size();
And then in order to change the Label's text value, use the below statement.
MainMenu.tbUserName.Text = "MY TEXT";
I am using Metro ui for windows application
public partial class Distributor_Closing : MetroFramework.Forms.MetroForm
{
private object BtnClick(Button button, int index)
{
MetroFramework.Forms.MetroForm childForm = new Distributer_Closing_Info(sub_cat[index], Str, id, Convert.ToInt32(Mtddlstocks.SelectedValue));
childForm.ShowDialog(this);
}
}
I am getting above error when i close Distributer_Closing_Info child form;
public partial class Distributer_Closing_Info : MetroFramework.Forms.MetroForm
{
public Distributer_Closing_Info(int sub_cat,String dte,int stk_mas,int stkid)
{
InitializeComponent();
sub_catid = sub_cat;
StockDate = dte;
Stkmasid = Convert.ToInt32(stk_mas);
stk_id = stkid;
LoadGrid();
}
}
I think it is an issue with the using MetroForm from the MetroFramework;
Your program would work fine if you were using System.Windows.Forms.Form
The workaround as mentioned
childForm.ShowDialog(null);
or this one
childForm.ShowDialog();
In my case the following line triggered this exception (WinForms):
this.splitContainer1.Panel1.Controls.Add(this.splitContainer1);
(The SplitContainer added itself in one of it's own panels.)
In my case, I no longer could open a Form in the WinForms designer within Visual Studio. I received the "A circular control reference has been made" error.
It turns out that I had set the .Name property of the Form to the same name as one of the Controls on the Form (I might have done this by editing the .Designer.cs code indirectly using a ReSharper rename operation...) in the .Designer.cs class module using the same name (but different casing) as a control already declared as private.
For example, the designer code had this snippet for setting Form properties:
//
// MyForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(850, 613);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MinimizeBox = false;
this.Name = "MyCircularName";
While further down in the .Designer.cs, control names are declared:
private MyUserControl myCircularName;
Notice that "MyCircularName" is now used by both Form and MyUserControl instances (the circular-check is not case sensitive). When I try to view the Form in the designer, the designer detects that my Form Name property is the same as a declared-control's Name property.
The solution was to either rename the Form (change "MyCircularName" to, say, "MyCircularNameForm") or to change the declared control name from "myCircularName" to, say, "myCircularNameControl".
I'm a newbie in c# and probably going to ask a very easy question, but I've not been able to find anything on the web to help.
I have a tabControl with a TabPage which is containing a TextBox object; this object, when the event "Text changed" is invoked, will perform the change of the parent tabPage's name.
The textbox where I typed "text changed by me" has a method which is managing changing the name of the tabPage:
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (this.textBox1.Text != "")
this.tabControl2.SelectedTab.Text = this.textBox1.Text;
else
this.tabControl2.SelectedTab.Text = "(no name)";
}
Into the current page menu is contained a control to add a new page, which runs this method when the user click on it:
private void addNewPageToolStripMenuItem_Click(object sender, EventArgs e)
{
int numPagine;
string strPagine;
numPagine = this.tabControl2.TabCount;
strPagine = numPagine.ToString();
this.tabControl2.TabPages.Add("new page" + strPagine);
}
...and here is the output, which is expected since I'm just asking to add a new empty tabPage:
So, my question is: how can I make possible that when the user is clicking on "Add new page", rather than creating an empty new tabPage the program is rather creating a page like the first one (i.e. containing a textbox into the same position which has a method to change the text of the parent tabPage that I have just created?
Here is an example.
//..
// create the new page
TabPage tpNew = new TabPage("new page..");
// add it to the tab
this.tabControl2.TabPages.Add(tpNew);
// create one labe with text and location like label1
Label lbl = new Label();
lbl.Text = label1.Text;
lbl.Location = label1.Location;
// create a new textbox..
TextBox tbx = new TextBox();
tbx.Location = textBox1.Location;
tpNew.Controls.Add(lbl);
tpNew.Controls.Add(tbx);
// add code to the new textbox via lambda code:
tbx.TextChanged += ( (sender2, evArgs) =>
{
if (tbx.Text != "")
this.tabControl2.SelectedTab.Text = tbx.Text;
else
this.tabControl2.SelectedTab.Text = "(no name)";
} );
For more complicated layout you may want to consider creating a user control..
You also may want to create the first page with this code; the, of course with real values for text and positions!
For creating a UserControl you go to the project tag and right click Add-UserControl-UserControl and name it, maybe myTagPageUC. Then you can do layout on it like on a form. A rather good example is right here on MSDN
The problem is that is has no connection to the form, meaning you'll have to code all sorts of references to make it work..
I'm not really sure if you may not be better off writing a complete clonePage method instead. It could work like the code above, but would loop over the Controls of the template page and check on the various types to add the right controls..
It really depends on what is more complicated: the Layout or the ties between the pages and the form and its other controls..
what i want to do is actually more complex than this.
but the principal is this..
i want to insert another control like text box into an existing form, but after some event like a click of a button.
the new text box would be inserted into the same form( main form)
what i have got is. i created as usual windows form application, and then put a button there.
then in the same project i add new form window. and put a text box there.
and in the event of button click, i put
form2.showdialog();
it works but it shows as a dialog box.
but what i want is that the text box shows up on the main form, not in the new form as a dialog box.
thank for the asap reply.
If you need only a single text box sometimes visible, sometimes not, I suggest just to add it in the designer and toggle the TextBox.Visible property in the event.
If you need to dynamically add several controls, I suggest to use the TableLayoutPanel and add controls to it at runtime.
Finally you could just add the control to the main form with something like the following.
Control textBox = new TextBox();
// Set the location, size, and all the other properties.
this.Controls.Add(textBox);
This way you have the largest freedom to build your form, but accept for very simple cases it is non-trivial to get a reasonable layout.
private void button1_Click( object sender, EventArgs e )
{
TextBoxt text = new TextBox( );
// set location and other properties
this.Controls.Add( text );
}
hei.
i am able to solve it by using list (generic list)
in main form, create a private variabel List
and create public method to get the variable.
in main form, create a public method to add by looping through the list.
so in the new class i have created, in one of the method i
put the creation of the form. and into here i pass the listcontrol.
and then put all the control variable into the list control.
and on a click of a button, i call the class method, and then it will automatically draw the form controls that created by the class.
private List<Control> listControl;
public windowForm()
{
InitializeComponent();
listControl = new List<Control>();
}
public List<Control> ListControl {
get { return listControl; }
}
public void addControl() {
if (this.listControl.Count() > 0) {
foreach (Control c in listControl)
{
Console.WriteLine("adding "+c.Name);
this.panel1.Controls.Add(c);
}
}
}
public void removeControl() {
if (this.listControl.Count() > 0)
{
foreach (Control c in listControl)
{
Console.WriteLine("removing " + c.Name);
this.panel1.Controls.Remove(c);
}
}
}
and for the new class i have created, i put
this.groupbox_VectorAddition = new System.Windows.Forms.GroupBox();
this.txtBox_v1a = new System.Windows.Forms.TextBox();
this.txtBox_v1b = new System.Windows.Forms.TextBox();
this.txtBox_v1c = new System.Windows.Forms.TextBox();
this.txtBox_v2c = new System.Windows.Forms.TextBox();
this.txtBox_v2b = new System.Windows.Forms.TextBox();
this.txtBox_v2a = new System.Windows.Forms.TextBox();
this.lbl_Vector1 = new System.Windows.Forms.Label();
this.lbl_Vector2 = new System.Windows.Forms.Label();
this.btn_countAddVector = new System.Windows.Forms.Button();
this.btn_resetVector = new System.Windows.Forms.Button();
//put everything into the panel
form.ListControl.Add(btn_resetVector);
form.ListControl.Add(btn_countAddVector);
form.ListControl.Add(lbl_Vector2);
form.ListControl.Add(lbl_Vector1);
form.ListControl.Add(txtBox_v2a);
form.ListControl.Add(txtBox_v2b);
form.ListControl.Add(txtBox_v2c);
form.ListControl.Add(txtBox_v1c);
form.ListControl.Add(txtBox_v1b);
form.ListControl.Add(txtBox_v1a);
form.ListControl.Add(groupbox_VectorAddition);