I want to combine several winform applications into 1 master application. I created a new winform project/solution and added the forms from two of the solutions. I added TabPages to the first form from which to launch other forms. I also changed the project Output Type to Library as recommended in combine multiple C# projects I am having difficulty getting the 2nd form (classBuilder) to load when I click the Class Builder tab.
I Googled how to add a form to a TabPage and most posts had very similar code that I inserted as shown but nothing happens when I click the tab. As a novice I have no idea if I’m on the right path or placed the subject code in the right place. Need someone way smarter than me to get me on track. A few concerns I have are: 1) Each form has a unique app.config file and if I attempt to rename it the main form doesn’t fill in so I put it back to App.config. I imported the config file for the 2nd form and renamed it ClassBuild.config and the concern is the 2nd form won’t fill in if and when I get it running with some expert help. 2) I don’t understand why I would issue ‘Form frmClassBuilder = new Form();’ when a form by that name already exists. Is this code okay?
namespace VX130
{
public partial class VX130UI : Form
{
public DataTable tblPKIEN;
public DataTable tblsAttributes;
public DataTable tbltAttributes;
public DataSet dsVX130;
SqlDataAdapter da = new SqlDataAdapter();
public VX130UI()
{
InitializeComponent();
WindowState = FormWindowState.Maximized;
//attempt to add form to a tabpage
Form frmClassBuilder = new Form();
frmClassBuilder.TopLevel = false;
tabPage9.Controls.Add(frmClassBuilder);
frmClassBuilder.Parent = this;
frmClassBuilder.WindowState = FormWindowState.Maximized;
frmClassBuilder.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
frmClassBuilder.Dock = DockStyle.Fill;
frmClassBuilder.Show();
//end attempt
// tabControl1
//
this.tabControl1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Controls.Add(this.tabPage8);
this.tabControl1.Controls.Add(this.tabPage9);
this.tabControl1.Controls.Add(this.tabPage10);
this.tabControl1.Controls.Add(this.tabPage11);
this.tabControl1.Controls.Add(this.tabPage12);
this.tabControl1.Location = new System.Drawing.Point(13, 65);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(963, 445);
this.tabControl1.TabIndex = 2;
// tabPage9
//
this.tabPage9.Location = new System.Drawing.Point(4, 22);
this.tabPage9.Name = "tabPage9";
this.tabPage9.Padding = new System.Windows.Forms.Padding(3);
this.tabPage9.Size = new System.Drawing.Size(955, 419);
this.tabPage9.TabIndex = 3;
this.tabPage9.Text = "Class Builder";
this.tabPage9.UseVisualStyleBackColor = true;
this.tabPage9.Controls.Add(ClassBuilderUI.frmClassBuilder()); is a ‘type’ and not valid in given context
I found what was missing piece to create instance of an existing form (exists in solution).
public VX130UI()
{
InitializeComponent();
WindowState = FormWindowState.Maximized;
ClassBuilderUI.frmClassBuilder frmClassBuilder = new ClassBuilderUI.frmClassBuilder(); <== to reference existing form (exists in solution
//Form frmClassBuilder = new Form();
frmClassBuilder.Dock = DockStyle.Fill;
frmClassBuilder.TopLevel = false;
frmClassBuilder.Visible = true;
tabPage9.Controls.Add(frmClassBuilder);
tabPage9.Show();
Show the TabPage, not the form. And don't set things like Parent and WindowState for the form you are adding:
//Form frmClassBuilder = new Form();
ClassBuilderUI.frmClassBuilder frmClassBuilder = new ClassBuilderUI.frmClassBuilder(); <== to reference existing form (exists in solution
frmClassBuilder.Dock = DockStyle.Fill;
frmClassBuilder.TopLevel = false;
frmClassBuilder.Visible = true;
tabPage9.Controls.Add(frmClassBuilder);
tabPage9.Show();
I don’t understand why I would issue ‘Form frmClassBuilder = new
Form();’ when a form by that name already exists.
Exists where? How? You need an instance of the form to add to the Tab page control collection. If you already had an instance, you could add it. Just be sure it doesn't get Close() or Dispose() called on it.
The code in your ClassBuilderTab_MouseClick handler looks useless. I would remove that unless you have some other need for it.
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...
So in my Form1 I have a label called lblView
I also have a class called Timer.cs
Where I have:
public TriageTimer(int ticket_num, int alert_time_in_sec, int error_time_in_sec)
{
this.id = ticket_num;
this.counter.Elapsed += new ElapsedEventHandler(this.count);
this.counter.Interval = 1000;
this.alert_timer.Elapsed += new ElapsedEventHandler(this.alert_sound);
this.alert_timer.Interval = alert_time_in_sec;
this.error_timer.Elapsed += new ElapsedEventHandler(this.error_sound);
this.error_timer.Interval = error_time_in_sec;
this.alert_timer.AutoReset = false;
this.error_timer.AutoReset = false;
this.alert_timer.Enabled = true;
this.counter.Enabled = true;
this.error_timer.Enabled = true;
}
The primary focus is the
this.alert_timer.Elapsed += new ElapsedEventHandler(this.alert_sound);
this.error_timer.Elapsed += new ElapsedEventHandler(this.error_sound);
I tried adding:
lblView.ForeColor = System.Drawing.Color.Green;
into alert_sound but I get the error lblView does not exist in the current context. I have looked up this error and found a few answers on stackoverflow that have solutions like; creating a method in the class to write something in the lbl and just call it from the form.
However my question is specifically trying to get the label fontcolor to change when it hits these Events (alert_sound & error_sound) So is there a way to reference the label that is in Form1 from Timer.cs?
but I get the error lblView does not exist in the current context.
YES, since the label control instance is present in your FORM, you will have to access it through a reference of that form. You can pass a FORM reference while calling that class method and using that form instance access the label control. Also, the FORM must be opened.
I'm trying to add a form to a SplitContainer from a child form. I can do it from the forms parent using this.
Lockdown.MainForm form = new Lockdown.MainForm(true);
form.MdiParent = this;
form.TopLevel = false;
form.Dock = DockStyle.Fill;
this.splitContainer.Panel2.Controls.Add(form);
form.Show();
But I can't figure out how to do it from a child of the parent form.
Any help is appreciated.
Here's how I solved the problem. I passed a reference to the child form.
MessageBoxRegister register = new MessageBoxRegister(this);
register.ShowDialog();
I then saved the reference in a global variable.
Launcher launcher;
public MessageBoxRegister(Launcher launcher)
{
InitializeComponent();
this.launcher = launcher;
}
Then I could open the form into the splitContainer like this.
Lockdown.MainForm form = new Lockdown.MainForm(true);
form.MdiParent = launcher;
form.TopLevel = false;
form.Dock = DockStyle.Fill;
launcher.splitContainer.Panel2.Controls.Add(form);
form.Show();
i'm developping a winforms application and i'm putting a mdi child form in splitcontainer.panel1.
when i want to close current mdi child to open another one i can't get the child form.
i'm using this code to open e new child but i want get the current child to close it :
Accueil accueil = new Accueil();
accueil.MdiParent = this;
accueil.TopLevel = false;
this.splitContainer1.Panel1.Controls.Add(accueil);
accueil.WindowState = FormWindowState.Maximized;
accueil.Size = this.splitContainer1.Panel1.ClientSize;
accueil.MinimizeBox = false;
accueil.MaximizeBox = false;
accueil.ControlBox = false;
accueil.Width = this.splitContainer1.Panel1.Width;
accueil.Height = this.splitContainer1.Panel1.Height;
accueil.Show();
Putting an MDI child window into a split container doesn't make any sense. You are turning the form into a plain control by setting its TopLevel property to false. Best not to lose the reference. But you will probably be ahead with:
while (splitContainer1.Panel1.Controls.Count > 0)
splitContainer1.Panel1.Controls[0].Dispose();
var accueil = new Accueil();
accueil.TopLevel = false;
accueil.Dock = DockStyle.Fill;
accueil.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
accueil.Visible = true;
this.splitContainer1.Panel1.Controls.Add(accueil);
Do consider using a UserControl instead, it is the sane approach with the least likely long-term confuzzlement.
I was developing a WinForm application for a class and I ran into a bug that I can't seem to find the root of. When I run the application everything works except for an error label that was supposed to come up with incorrect user input. At first I thought I had written the event handler for it wrong, so I stopped hiding it at startup but the label is still missing. I'm not sure if I'm missing something in some back-end file or if I'm just missing something really obvious.
This is the function that creates the label.
private void InitializeErrorLabel()
{
int width = 200, height = 13,
anchorY = this.Label.Location.Y - this.Label.Size.Height - 3;
// Initialize Component
this.ErrorLabel.AutoSize = true;
this.ErrorLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.ErrorLabel.ForeColor = System.Drawing.Color.Red;
this.ErrorLabel.Location = new System.Drawing.Point((XSize - width) / 2, (anchorY - height));
this.ErrorLabel.Anchor = System.Windows.Forms.AnchorStyles.Top;
this.ErrorLabel.Name = "ErrorLabel";
this.ErrorLabel.Size = new System.Drawing.Size(width, height);
this.ErrorLabel.Text = "Invalid User ID. Please try again!";
return;
}
and this is the function that initializes my controls:
private void InitializeComponent()
{
this.UserInput = new System.Windows.Forms.TextBox();
this.SwitchMajor = new System.Windows.Forms.RadioButton();
this.SwitchToCS = new System.Windows.Forms.CheckBox();
this.SwitchToCE = new System.Windows.Forms.CheckBox();
this.KeepMajor = new System.Windows.Forms.RadioButton();
this.AcceptValues = new System.Windows.Forms.Button();
this.Label = new System.Windows.Forms.Label();
this.ErrorLabel = new System.Windows.Forms.Label();
this.SuspendLayout();
// Initialize Components
this.InitializeLabel();
this.InitializeMainWindow();
this.InitializeUserInput();
this.InitializeSwitchMajorBtn();
this.InitializeChangeToCSBtn();
this.InitializeChangeToCEBtn();
this.InitializeAcceptValuesBtn();
this.InitializeErrorLabel();
this.ResumeLayout();
this.PerformLayout();
return;
}
Again, not really sure what I'm doing wrong here. Any help would be appreciated.
Thanks,
In what control are you adding your errorlabel ?
A normal label initialization should look like
private System.Windows.Forms.Label ErrorLabel;
this.ErrorLabel = new System.Windows.Forms.Label();
this.groupBox2.Controls.Add(this.ErrorLabel);
this.ErrorLabel.AutoSize = true;
this.ErrorLabel.Location = new System.Drawing.Point(8, 59);
this.ErrorLabel.Name = "ErrorLabel";
this.ErrorLabel.Size = new System.Drawing.Size(55, 13);
this.ErrorLabel.TabIndex = 69;
this.ErrorLabel.Text = "Address 2";
this.ErrorLabel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
Important
The label must be added to a control like my line three. Your control can be a form in your case. Its a groupbox in my case and the group box itself must be added to myform and myform must be visible .
this.groupBox2.Controls.Add(this.ErrorLabel);
I don't see a place where you're adding the label to the form's collection of controls:
this.Controls.Add(this.ErrorLabel);
If it's not a member of the Controls collection, you won't see it.
Related to that, I wouldn't be surprised if the other buttons, checkboxes, etc you're defining are not showing up on the form either, for the same reason.
Normally this would be taken care of by the Designer.cs file automatically.
This might be an obvious answer, but it totally went over my head. I verified that my code had the this.groupBox2.Controls.Add(this.ErrorLabel); like others have mentioned here. I couldn't figure out why I wasn't seeing my label. My label was an asterisk *, and in the designer window, I size it pretty small, but I was able to see the * in the designer window. Turns out I just had to increase it's area in the designer window since it was to small when I actually rendered the App.