C# WinForm takes times for Initializecomponent() - c#

I am working on C# application where i am using "Telerik" controls to design the form. I have 5 tabs in Ribbon control having around 15 controls in each tab. The main problem arise when i run the code. It takes some time to load all the controls, it shows grey screen until controls loaded completely. The problem is with InitializeComponent() which is taking time. So is there any solution(s) which i can use so that my designer can load fast and i can avoid slow load of designer. I can't upload full code. But i can give some idea how i have designed.
RadRibbonBar myRibbon;//ribbon control
this.myRibbon.CommandTabs.AddRange(new Telerik.WinControls.RadItem[] {
this.rbnTab1,
this.rbnTab2,
this.rbnTab3,
this.rbnTab4,
this.rbnTab5});//add tabs
this.rbnTab1.Items.AddRange(new Telerik.WinControls.RadItem[] {
this.rbngroup1,
this.rbngroup2,
this.rbngroup3,
this.rbngroup4});//add groups in tab
this.rbngroup1.Items.AddRange(new Telerik.WinControls.RadItem[] {
this.rbnBtn1,
this.rbnBtn2,
this.rbnBtn3,
this.rbnBtn4});//add button in each group
myRibbon.ThemeName = "Office2013Light";
Each tab with 5 groups, each group with 4 button
Apart from this 15 buttons in StartMenu, around 10 controls in QuickAccessToolBar
NOTE: I am adding ribbon control in SplitContainer, and when i add ribbon from designer it takes entire screen to load slow whereas if i load ribbon at the end of window_load event than first it shows remaining controls and than its usual time to show ribbon.

Related

How can I add a button to the layout? - C# [duplicate]

This question already has answers here:
How to add buttons dynamically to my form?
(8 answers)
Closed 2 years ago.
I cannot find any methods to add the button to the layout.
I am trying to add the child (button) to the layout, but I can't find any methods to do so.
Source Code:
using System;
using System.Windows.Forms;
namespace WinForms
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
private static void Main()
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
Button button = new Button {Height = 100, Width = 100, Text = "Test"};
}
}
}
You open your project in Visual Studio 2019 (not Visual Studio Code, not JetBrains Rider) - free versions for which exist if your context qualifies for the license. If you don't qualify for a free license, you[r workplace] can easily afford a license of some form
You double click Form1 in the solution explorer and then you see something that looks like what the form will look like when you run the program, and you open the controls tool panel and drag a button out of it and drop it onto the form...
But if you want to get into hand-writing the volumes of boring repetitive code to build a UI then you add controls to the Controls collection of other controls, viz:
Form1 f = new Form1();
Button button = new Button {Height = 100, Width = 100, Text = "Test"};
f.Controls.Add(button);
Application.Run(f);
Every Control has a Controls collection to which other Controls can be added (not just things you think of as "things that have child controls, like Panel or GroupBox" - some controls are collections of other controls, like a NumericUpDown is a textbox and a couple of buttons)
For an example of how much code you'll need to write, lay out a reasonable looking UI in the design view and then open the Form1.Designer.cs - you'll see why we do it with the aid of a design tool! :)
Wouldn't it be faster to learn if I didn't use the Designer tool?
IMO, no. That's like saying "wouldn't it be faster to learn if I hand code an SVG in notepad rather than using Inkscape/Gimp to draw the image visually.. or create a PNG by typing the bytes out in a hex editor"
Getting so close to the raw low level means you end up "not being able to see the wood for the trees" and it hinders your learning. For a lengthy discourse on abstractions and why we use them/how they apply to every daily process including learning and operating in life, see the comment trail
You need to add the button to your Form, not to the Main method!
The issue with your code is that it's in the wrong place - actually, it only runs after the form is closed, because Application.Run will run your form in a message loop, allowing UI events to fire.
You can either use the Visual Studio WinForms Designer (if using Visual Studio), or manually add the code after the InitializeComponent() method - so, either right in the constructor, or in any of the Form startup events, such as Load or Shown.
It's likewise very important to add the Button (or any dynamically instantiated control, for that matter) to the Controls collection of the Form - otherwise, your Button won't be displayed:
Button button = new Button {Height = 100, Width = 100, Text = "Test"};
this.Controls.Add(button);
There are certainly use cases for dynamic generation of controls; however, it's very unusual to build out your controls manually before the form even runs - in most cases of dynamic control generation, the form is up and running - the dynamic generation is in response to some user action. I recommend using the designer for general UI layout.

Infragistics UltraTabbedMdiManager overlapping tab forms and panel shifts down

I am using a UltraTabbedMdiManager for a C# winform application. Each tab is added one at a time and displays a unique form. The tab panel is nested within its parent form. I am experiencing two issues related to new tab instantiation.
Issue #1: On adding a new tab the entire tab plane shifts down exposing a strip at the top of the parent form (Mdi container), just below its title bar. When clicking on another tab or elsewhere on the parent form, the tab plane reverts to its original position. This strip only appears on creating a new tab and disappears as soon as the focus is lost (by clicking elsewhere). It also occurs only every second tab that is generated which suggests an activation focus issue on new tabs. The problem does not occur when clicking between already generated tabs or when removing tabs.
I have found a work around for this issue: After the creation of the tab and its form, iterate through all the tabs in the mdi manager and disable them. This iteration is then immediately repeated with enabling all the tabs. The new tab is then reactived. These steps force a loss and then regain of focus and then activation of the tab (N.B. without activation the previous tab will be active). The following is a (verbose) piece of my code which does this operation:
var activeTab = MdiTabManager.ActiveTab;
foreach (var tabgroup in MdiTabManager.TabGroups)
{
foreach (var tab in tabgroup.Tabs)
{
tab.Form.Enabled = false;
}
}
foreach (var tabgroup in MdiTabManager.TabGroups)
{
foreach (var tab in tabgroup.Tabs)
{
tab.Form.Enabled = true;
}
}
activeTab.Activate();
Issue #2: On the generation of some new tabs. The form shifts down and the controls at the top of the previous tab are displayed (but are inactive) on the new tab. It is as if they were superimposed onto the new tab. A similar work around like before resolves this issue; I had read this on another infragistics forum post. However this time the tab's form ShowInTaskbar property is set to false and then true (after iterating through all the mdi managers tabs).
However, the workaround(s) have side-effects if relying on the Application.OpenForms collection. Calling ShowInTaskbar will effectively remove the open forms from this collection (a known Windows form bug as described here Application.OpenForms.Count = 0 always). Unfortunately my application relies on this collection. So my workaround is not applicable to my application. Though, it could be useful to other designs not relying on the OpernForms collection.
It is possible that the two issues are related. Perhaps solved in later version of infragistics. Infragistics does suggest upgrading. Before doing this has anyone experience such problems before?
Just for further information about our application. The form, that contains the mdi manager, is launched from our application's main form. This main form also uses a UltraTabbedMdiManager but does have these issues. Therefore are there known problems with having more than one mdi manager in the same project or solution ?
Here are my environment details:
Infragistics v14.1, .Net v4.7, Visual Studio 2017 Enterprise, C# 7.0, WinForms

Multiple forms one windows C# possible or only panels/usercontrol?

So here's my Question, I'm new to C#(teaching my self at that) Here's the thing, I'm working on a basic sim game, nothing to complex but I've got the design and basic functions done.
However In order to implement it, I'm currently using multiple Forms(Visual Studio 2013)
I have my "main" form which has the "action" buttons to it
So when i want to go to a user Profile page I have
Btn_profileview Click(object sender, EventArgs e){
Form profile = new Form();
profile.Show();
}
The User would then implement the changes(for instance change name) which is written to a text file, for use in other areas of the program.
However It opens a new windows, I've tried modal and nonmodal windows and while the benefit of Modal so they have to actual close the window solves the issue, i'd rather have it just overwrite the preexisting Form, and then on close go back to the "main" screen without actually using multiple windows.
Now I was told UserControl and/or Panel would solve the issue, but it would cause a complete redesign moving from the multiple forms to the multiple panel screens and figuring out how to get those to work(Visible and Invisible), i'm assuming it wouldn't be extremely difficult something along the lines of Panel"name".show(); and panel"name".close();
But would it be possible to actually add a line of code to the pre-existing code(so as not to cause a complete reesign) or are Panels and UserControl the only real way to implement within 1 continuous windows?
paqogomez is right: There are many ways to do it.
Here is one that combines a lot of the pros:
You create an invisible Tab on your window with as many pages as you need. Place a Panel on each tab and create all your controls on of them. This does not mean that you have to do it all over - you can move and drop the controls you already have without much hassle. Of course you need to turn off docking and maybe anchors, but other than that this is a simple process.
If you have controls on the 2nd form with the same name, these names should be changed to something unique though. I hope all Controls have proper names already, but especially Labels get neglected, at least here.. (With a little luck you can even use cut and paste to get Controls from another form to panel2!)
The big pro of this trick is that you can do all work in the designer of the same form. The Tab control serves only as a container where you keep your panels without adding to the UI and without giving the user control of what is shown.
Next you create one Panel variable in your main form:
Panel currentPanel;
On Load you assign the first 'real' Panel to it like this:
currentPanel = panel1;
this.Controls.Add(currentPanel);
Later, each time you want to switch, you re-assign the panels you need like this:
this.Controls.Remove(currentPanel);
currentPanel = panel2; // or whichever panel you want to show..
this.Controls.Add(currentPanel );
If your real panels are docked to fill the tabpage, as they should, the currentPanel will fill the form. You still have access to each panel and to each control by their names at any time but you see no overhead of tabs and your form never changes, except for the full content.

How to show multiple form pages in single windows form application?

I have got a requirement to design a windows forms application using Visual Studio 2010.
According to the design I have to develop the application which contains a menu bar. On selecting of the menus from the Menu bar relevant forms should open. Now as per my requirement these menu forms should be displayed in the same parent Windows Form. Means everything should be in the single form Application.Nothing should be out of that.
The problem that I'm facing is that I don't know how to proceed with this. This is the first time I'm working on Windows Form Application leaving Web.
You are looking for developing an MDI application. MSDN article to guide you - http://msdn.microsoft.com/en-us/library/xyhh2e7e(v=vs.100).aspx
You could build a grid area on your form using a combination of split containers (horizontal and vertical).
Then in separate panels design each of your menu forms, with each panel visibility set to false.
When choosing a menu you would need to assign a parent (split container panel) to the menu form and set it to visible.
Step 1
Create a form and give the name it to "mdiMain" and set the property IsMdiContainer to true.
Add a MenuStrip control from the ToolBar and add some menues.
Step 2
Create another form and give the name it to "frmChild"
Step 3
Write some code in menu click event to display frmChild form in MDI Parent.
Dim frm As New frmChild()
frm.MdiParent = Me
frm.Show()
Now your application is ready. You can place your code and control in frmChild window form.
As an option in some cases you can use WebBrowser control and manipulate html code inside depending on the menu bar selection.
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser%28v=vs.110%29.aspx

What is the standard way to add controls to a TabPage at Design Time?

I am creating my first Windows Forms application, to be deployed on Windows Mobile and I am having some trouble designing a Tabbed Interface.
I had assumed that I could Create a TabControl, then Add some TabPages and then drag Controls on to each Tab Page in turn. This does not appear to be possible and most of the information I see on the web seems to suggest that the controls should be added dynamically at run-time.
Am I missing something obvious here or is this indeed correct?
If you do have to add the controls at runtime then how do people generally manage the design process. Do they create a Custom UserControl for each tab and then add that at runtime?
Design environment (C# Visual Studio 2005, .net 2.0)
Runtime environment (Windows Mobile 6.1)
Update 1
The actual steps taken within visual studio were as follows :-
Select New Project -> SmartDevice -> Windows Mobile 6 Professional -> Device Application
Added a TabControl to Form1. This automatically adds tabPage1 and tabPage2
Update 2
The solution to this is embarrassingly noobish. The TabControl puts the tabs at the bottom of the page, the first thing I was doing was resizing the tab control to a single line which was then hiding the TabPage control.
Currently i don't use Windows Mobile, but i think it works quite the same.
After adding a TabControl to your form you should take a look into the properties and search for TabPages. Here you can add and delete new TabPages to your Control and design it as you like in the designer.
To your question about using UserControls on each TabPage i would definitely say Yes. It makes easier to separate between each page and what will happen on each one.
Also at a last step i am going to move the needed code out of the Designer.cs into my own function (e.g. var tabControl = CreateTabControl() where all of my properties are set. Then i put all my UserControls into an
private IEnumerable<Type> GetAllTypes()
{
yield return typeof(MyFirstControl);
yield return typeof(MySecondControl);
}
and make an
private void CreateTabPages(TabControl tabControl, IEnumerable<Type> types)
{
foreach(var type in types)
{
var control = Activator.CreateInstance(type);
var tabPage = new TabPage();
tabPage.Controls.Add(control);
tabControl.TabPages.Add(tabPage);
}
}
this will then be called by
CreateTabPages(tabControl, GetAllTypes());
With this approach i can easily add another Tab Page with a single line of code and design it in its own scope.
I just opened vs2008 and created a tabcontrol, then I added controls inside using drag and drop in the designer and I didn't found any problem.
The way I use to do it is to create a usercontrol for each tab, But I add the usercontrol to the tab in the designer. (note that the usercontrol will not appear in the toolbox until you generate your solution).
I didn't know why your method are not working. Did you stop your application before try to add the controls?
Good Luck.

Categories