I'm working on a Agenda using windows forms C#, I'm trying to create a colored picture box for each appointment object in the project. Using this code that is used in a loop for each object in my appointment list im creating each picturebox on the right location on the form1 screen.
PictureBox Point = new PictureBox();
this.Controls.Add(Point);
Point.Location = new Point(obj.Location.X, 45 + obj.Location.Y);
Point.BackColor = color;
Point.Size = new Size(96, 25);
Point.Enabled = false;
Point.Tag = "Point";
Point.TabIndex = 100;
Point.Visible = true;
When I'm calling this method from input on the same form, for example a button click. It will work just fine and create all the picture boxxes as needed. But when I'm calling it from the form2.closed event it wont work. Form 2 is my appointment planner form, when clicking on save on this form it will add a new object to the list, so a new picturebox should be created. I have checked the debug using breakpoints, and strange enough it will go through the create code, but no matter what I do it wont render the pictureboxxes.
I personally think it has to do with the form1 not Initializing when called from form2.closed event. But even when using InitializeComponent(); end Refresh(); inside my code it still doesnt work.
Am I using the wrong event or is there a specific call I need to make to generate the pictureboxxes?
Sorry if my post is lacking code or info, I'm not used to posting on stackoverflow, feel free to ask for more information if needed.
if you run your code from Form2 and you want to add control to Form1 you cannot use "this". Form1 has to be accessible from Form2.
PictureBox Point = new PictureBox();
Point.Location = new Point(obj.Location.X, 45 + obj.Location.Y);
Point.BackColor = color;
Point.Size = new Size(96, 25);
Point.Enabled = false;
Point.Tag = "Point";
Point.TabIndex = 100;
Point.Visible = true;
Form1.Controls.Add(Point);
I have tried/researched for an entire day to set the location of Form2 called by a button click on Form1.
I allow the user to resize the forms and want Form2 to open at the same size and location of Form1 (wherever it is when they click the button).
This is what I've tried so far:`
app.hnow = this.Height;// size of form displayed now
app.wnow = this.Width;
app.module = 2; //indicates look up mode
lookprmtby lookby = new lookprmtby(this); instantiate Form2
app.hprev = lookby.ClientSize.Height; // gets original size of form being called
app.wprev = lookby.ClientSize.Width;
lookby.ClientSize = new Size(app.wnow, app.hnow);// set new size before show
lookby.WindowState = FormWindowState.Normal;
lookby.StartPosition = FormStartPosition.Manual;
// set lookby's location to same location as current form - i.e. left = 0 top = 174
lookby.Location = this.Location;// this does not work / lookby.location is 0,0 after this statement
lookby.Left = this.Location.X; // left and top do not work either-they evaluate to 0
lookby.Top = this.Location.Y;
lookby.Show();`
FYI, ClientSize is equivilent to the Size property. I'm using BinaryMission's ModernChromeWindow style as the basis for the majority of my forms.
Can anyone see the reason why the lookby.location property is evaluating to 0,0?
(VS 2019, c#)
I have my main form and a dialogbox which is called from main. In my main form I have a label and a button that which properties I can't change. I'm using Visual Studio 2015, not sure if there is a bug regarding this. I also made sure my label and button are set to public to modify.
Code: (this is from the dialog box, this has a list box the function is triggered at selectindexchange)
else if ((short)lbDiscountTypes.SelectedValue == 2) //Senior
{
frm_Main main = new frm_Main();
main.VAT = false;
main.labelStatus.Text = "NON-VAT (SENIOR)";
main.labelStatus.BackColor = System.Drawing.Color.IndianRed;
main.labelStatus.ForeColor = System.Drawing.Color.WhiteSmoke;
main.btnNonVat.Enabled = false;
main.btnNonVat.BackColor = System.Drawing.Color.SlateGray;
main.btnNonVat.ForeColor = System.Drawing.Color.Navy;
main.labelVatAmount.Text = 0.00m.ToString();
main.Dispose();
//INQUIRE DISCOUNT TYPES
var Discount = GC.CSHR_DiscountTypes.Where(Filter => Filter.DiscountCode == (short)lbDiscountTypes.SelectedValue);
decimal DP = 0.00m;
foreach (var item in Discount)
{
DP = item.DiscountPercentage;
}
foreach (var item in GC.CSHR_SORepo
.Where(Filter => Filter.Machine == MACHINE
&& Filter.SalesOrderNum == SALESORDERNUM
&& Filter.First_SRP == Filter.IMFSRP))
{
item.DiscountAmount = (item.SoldSRP * DP) / 100;
item.TotalAmount = (item.Quantity * item.SoldSRP) - item.DiscountAmount;
item.VATableSalesOnTotalAmount = (item.Quantity * item.SoldSRP) - item.DiscountAmount;
item.VATRate = 0.00m;
GC.SaveChanges();
}
Close();
}
The code below //INQUIRE DISCOUNT TYPES works well but not the one on top.
I've used debug mode to check if the lines are not being skipped over and they aren't.
You should pay attention to:
You are creating a new instance of your main form that you don't need (while it is open behind the dialog), so you need to get it not create a new instance
You are disposing the main form you created. main.Dispose();
In fact you are creating a new instance of main form and assigning values to those controls and then dispose it. While and instance of yor main form that you expect to see changes on it, is open and untouched behind your dialog.
To set value of those controls you can do one of these ways:
Option 1
Make your labelStatus and btnNonVat public. Open your main form in designer and select labelStatus and btnNonVat and in property grid, set Modifier to public. Then write this code:
//var main = Application.OpenForms.OfType<frm_Main>().FirstOrDefault();
var main = (frm_Main)Application.OpenForms["frm_Main"];
main.labelStatus.Text = "NON-VAT (SENIOR)";
main.labelStatus.BackColor = System.Drawing.Color.IndianRed;
main.labelStatus.ForeColor = System.Drawing.Color.WhiteSmoke;
main.btnNonVat.Enabled = false;
main.btnNonVat.BackColor = System.Drawing.Color.SlateGray;
main.btnNonVat.ForeColor = System.Drawing.Color.Navy;
main.labelVatAmount.Text = 0.00m.ToString();
Option 2
Pass an instance of your frm_Main to your dialog and work with it.
Option 3
After closing the dialog, use values from dialog and set values of your main form
Looks like you are trying to create new form using frm_Main main = new frm_Main(); syntax. All you need to do is get the instance of your current form.
var _currentMainForm= Application.OpenForms[0];
or if you have given name to your form
var _currentMainForm = Application.OpenForms["MainFormName"];
Once you get the reference you can perform all your label updates.
The code on top creates a new form, changes the labels and then disposes the form.
I think you should change the labels of the existing form.
Like in the other answer said you are setting properties of controls into a new Form object and not in the form where you come from.
You should pass the form object into the parameters of the dialog, something like:
void myDialog(frm_Main callingForm)
{
callingForm.Textbox1.Text = "abc";
}
And call it from you main form like this
...
myDialog(this);
I have written an WinForms Control as ActiveX by COM-Interop and so far it works well in MS Access.
But the Problem is in Access the Form on the Display View hasn't the same Size as the Form in the Design View.
I have tried to set the Initialize Size by getting the ContainerControl. But i doesn't return the right Value.
public DummyCtrl()
{
this.Dock = DockStyle.Fill;
this.AutoSize = true;
var axC = (Control)this.GetContainerControl();
this.Width = axC.Width;
this.Height = axC.Height;
InitializeComponent();
}
GetContainerControl() contains a "ControlAxSourcingSite[WFControl.DummyCtrl]" Object.
I'm not sure how to get the right Value befor Initialize the Element. Any Ideas?
So i found it by myself. ;)
The right way to do this is:
public DummyCtrl()
{
this.Size = PreferredSize;
InitializeComponent();
}
I suggest that this works probably in every Forms Control.
Have a nice Day...
I am trying to implement the following behaviour:
On a form there is a tabcontrol. On that tabcontrol there is a treeview. To prevent scrollbars appearing, I would like the form to change its size according to the contents of the treeview when displayed for the first time.
If the treeview has too many nodes to be displayed on the default size of the form, the form should change it's size so that there is no vertical scrollbar on the treeview (up to a maximum size allowed by the size of the screen).
What I need to know is, if it is possible to achieve this behaviour through the properties of the controls. I'm sure this can be achieved by calculating and settings the sizes of the elements programmatically, but I would like to know if there is a way to achieve this by settings like AutoSizeMode etc.
[UPDATE]
It's the first dialog a user of my application sees: It's a dialog to select the database to work with. It's a list of databases, with a tabcontrol, buttens etc. If the list is too long, scrollbars appear and a colleague of mine wants them to disappear.
Use the AutoSize and AutoSizeMode properties.
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.autosize.aspx
An example:
private void Form1_Load(object sender, EventArgs e)
{
// no smaller than design time size
this.MinimumSize = new System.Drawing.Size(this.Width, this.Height);
// no larger than screen size
this.MaximumSize = new System.Drawing.Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, (int)System.Windows.SystemParameters.PrimaryScreenHeight);
this.AutoSize = true;
this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
// rest of your code here...
}
By using the various sizing properties (Dock, Anchor) or container controls (Panel, TableLayoutPanel, FlowLayoutPanel, etc.) you can only dictate the size from the outer control down to the inner controls. But there is nothing (working) within the .Net framework that allows to dictate the size of a container through the size of the child control. I also missed this a few times and tried the AutoSize property, but it never worked.
So all you can do is trying to get this stuff done manually, sorry.
From MSDN:
To maximize productivity, the Windows Forms Designer shadows the
AutoSize property for the Form class. At design time, the form
behaves as though the AutoSize property is set to false,
regardless of its actual setting. At runtime, no special
accommodation is made, and the AutoSize property is applied as
specified by the property setting.
This might be useful.
It resizes a new form to a user control, and then anchors the user control to the new form:
Form f = new Form();
MyUserControl muc = new MyUserControl();
f.ClientSize = muc.Size;
f.Controls.Add(muc);
muc.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
f.ShowDialog();
You could calculate the required height of the TreeView, by figuring out the height of a node, multiplying it by the number of nodes, then setting the form's MinimumSize property accordingly.
// assuming the treeview is populated!
nodeHeight = treeview1.Nodes[0].Bounds.Height;
this.MaximumSize = new Size(someMaximumWidth, someMaximumHeight);
int requiredFormHeight = (treeView1.GetNodeCount(true) * nodeHeight);
this.MinimumSize = new Size(this.Width, requiredFormHeight);
NB. This assumes though that the treeview1 is the only control on the form. When setting the requiredFormHeight variable you'll need to allow for other controls and height requirements surrounding the treeview, such as the tabcontrol you mentioned.
(I would however agree with #jgauffin and assess the rationale behind the requirement to resize a form everytime it loads without the user's consent - maybe let the user position and size the form and remember that instead??)
This technique solved my problem:
In parent form:
frmEmployee frm = new frmEmployee();
frm.MdiParent = this;
frm.Dock = DockStyle.Fill;
frm.Show();
In the child form (Load event):
this.WindowState = FormWindowState.Maximized;
If you trying to fit the content according to the forms than the following will help. It helps me while I was trying to fit the content on the form to fit when ever the forms were resized.
this.contents.Size = new Size(this.ClientRectangle.Width,
this.ClientRectangle.Height);
I User this Code in my project, Useful for me.
private void Form1_Resize(object sender, EventArgs e)
{
int w = MainPanel.Width; // you can use form.width when you don't use panels
w = (w - 120)/4; // 120 because set 15px for each side of panels
// and put panels in FlowLayoutPanel
// 4 because i have 4 panel boxes
panel1.Width = w;
panel2.Width = w;
panel3.Width = w;
panel4.Width = w;
}
I used this code and it works just fine
const int margin = 5;
Rectangle rect = new Rectangle(
Screen.PrimaryScreen.WorkingArea.X + margin,
Screen.PrimaryScreen.WorkingArea.Y + margin,
Screen.PrimaryScreen.WorkingArea.Width - 2 * margin,
Screen.PrimaryScreen.WorkingArea.Height - 2 * (margin - 7));
this.Bounds = rect;