(VB.NET / C#) How to create Vista-like style MenuStrip? [duplicate] - c#

I noticed that adding a MenuStrip (from the Toolbox) to my form design doesn't yield a menu bar like the one seen in many native Windows applications. Instead I get a menu bar like Visual Studio's own. None of the style settings for MenuStrip appear to mimic the much more common native menu bar.
Is there a way to add a menu bar to my Windows Forms application that looks the same as the one you see in Notepad, Task Manager and others? (Preferably with the designer, but I wouldn't mind adding it programmatically either.)
Screenshot for illustration:

Go to your Toolbox, right click anywhere inside and select "Choose Items".
When the dialog loads and appears, scroll down til you see MainMenu. Add that to the toolbox, and you've got yourself a native menu bar!

You can do this by setting your form's Menu property, like this:
private void Form1_Load(object sender, EventArgs e)
{
this.Menu = new MainMenu();
MenuItem item = new MenuItem("File");
this.Menu.MenuItems.Add(item);
item.MenuItems.Add("Save", new EventHandler(Save_Click));
item.MenuItems.Add("Open", new EventHandler(Open_Click));
item = new MenuItem("Edit");
this.Menu.MenuItems.Add(item);
item.MenuItems.Add("Copy", new EventHandler(Copy_Click));
item.MenuItems.Add("Paste", new EventHandler(Paste_Click));
// etc ...
}
private void Save_Click(object sender, EventArgs e)
{
// save
}
These menus will look like "normal" system menus.
I couldn't find any designer support for this, though. In my defense, I didn't try real hard.

Instead of using a the MainMenu component you can create your own renderer for the MenuStrip component. The advantage here is being able to add images to MenuStripItem objects. Here is the pastebin for the custom renderer:
NativeRenderer
There are different themes that can be applied in the constructor of the renderer. Try them all to see the native themes. To use this renderer simply set the instance to the MenuStrip Renderer property:
menuStrip.Renderer = new NativeRenderer([theme]);

I normally set the MenuStrip's RenderMode to System which gives a minimalist, single colour menu (no gradients or anything decadent like that).
If that does not go far enough, then you'll likely have to jump through some low-level hoops to get what you want.

Related

How to create tab pages content dynamically in windows forms?

I am using windows form to build an app that draws the form controls based on the connected device dynamically. So I have a tab control and when the user select tab3 for instance the tab page content will be drawing based on connected device for example add two text boxes and a button. How can I do this. I would like also to know how to position those controls after they are created.
private void tabPage3_Click(object sender, EventArgs e)
{
TextBox text = new TextBox();
this.tabPage3.Controls.Add(text);
}
As you just stated, you create your Controls like in your example. Positioning is achieved by the Left and Top Properties of your freshly created control. BUT, my advise is, it will be easier to use predefined UserControls and add them dynamically, because I think you don't have nearly unlimited types of devices.
If you are curious how Visual Studio Designer is creating those code, just look up Designer.cs in InitializeComponent()

Set the Button Text according to Resource File in Visual Studio 2013

I am an Amateur Visual C# Programmer.
I want to implement the concept of I18N in my project.
(Similar to strings.xml in Android)
I have added 2 String Resources in my Resource File.
And I have added 2 Buttons in my form.
Now, I want the text of the Button to be the string value from the Resource file.
Please help.
Adhish
If you are using windows form, you can use this procedure.
Open the properties menu of the form in the design. Select the option "Language" and choose the language that you want.
Selecting a different language will create automatically a new file resource with the label for the language selected.
The file .resx will the same name of the form plus the initial of the language used.
It really depends on your UI framework. The following solution is for WinForms, but you can implement it (without MVVM) in WPF as well.
Create a button in the designer.
Register to the Load event of the form by double clicking it. (Right click on the form => properties => click the events button => search for the Load event)
In the load event, initialize the text of the button with the string from the resources.
Your code should look something like this:
private void Form1_Load(object sender, EventArgs e)
{
button1.Text = MyResources.ButtonText;
}

Excel buttons to call WPF windows

I have a CSharp (.NET) application that has created an add-in with a ribbon in Excel. I have buttons in the ribbon. I want to be able to click on the buttons, and open WPF windows.
The code looks like
private void OnNewButtonAction(object sender, RibbonControlEventArgs e)
{
var window = new View.MyWindow()
{
DataContext = new ViewModel.MyViewModel(),
};
window.Show();
}
Where MyWindow is a class of type System.Windows.Window. MyWindow has its own xaml file, which has radio buttons, text fields etc. When I try to run this - and click on the button, I get an XML parse exception as - "'Provide value on 'System.Windows.StaticResourceExtension' threw an exception".
Is it possible to invoke wpf windows from excel add-ins? What am I doing wrong?
Edit: I have already looked at
Using WPF Controls in Office Solutions and it doesn't work. And it adds a separate pane to excel, which is not what I am looking at.
You should use Excel-DNA. Its a really useful piece of software designed just for things like this, it helps to implement Excel with WPF, and you shouldn't have any problems again.
you can get it Here
If however you don't want that there is a step by step tutorial here on how to do it.
You may try to define a WPF window as a custom control and add it to the custom pane of the word. Have a look at the following link, pleases:
Using WPF Controls in Office Solutions
You may have a look at this link as well:
Office 2007 Excel Addin - WPF ComboBox Collapses when Expanded
The common way we add WPF control to custompane is:
Create an Excel add-in project
Add user control (WPF) name UserControl1 and add reference to System.xaml
Code the WPF control and Build the project successfully
Add User Control from Window Form collection, named the control as UserControl2
Drag and drop a UserControl1 to UserControl2, assign the position as you like
Code the ThisAddIn.cs in this way:
UserControl1 myWPF;
UserControl2 winformControl;
Microsoft.Office.Tools.CustomTaskPane pane;
System.Windows.Forms.Integration.ElementHost myHost;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
myWPF = new UserControl1();
winformControl = new UserControl2();
pane = CustomTaskPanes.Add(winformControl, "WPFControl");
pane.Visible = true;
pane.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionRight;
}
We can use the button to control the pane's visible property.

What is the most efficient way to create a Win form with multiple pages?

My goal
I am working on a project in C# using Visual Studio 2013. The project is one that I intend to contain a lot of pages. These pages are all linked together using buttons. My problem is that I cannot come up with an efficient and elegant solution for this.
My attempts
So far I have came up with two potenial solutions to my problem. First I added extra forms and then on button press I hid the current form and displayed the new form, like so:
Form2 frm = new Form2();
frm.Show();`
Form1.Hide();
While this does work, I have two problems with it.
My project will end up with hundreds of forms
The transition between forms looks sloppy. I am aiming for a browser like transition by where all navigation occurs on one window, without opening and closing others.
The second potential solution I tried incorporated the use of Panels. So I essentially created each page on a different Panel. Then the appropriate panel was shown upon a button press and the rest were hidden. Like this:
private void button1_Click(object sender, EventArgs e)
{
mainMenuPanel.Hide();
submenuPanel1.Show();
submenuPanel2.Hide();
submenuPanel3.Hide();
submenuPanel4.Hide();
}
This is exactly what I was looking for however my issue with it is that managing the vast amount of panels quickly became a nightmare. Editing the controls on a Panel that was hidden behind 9 other Panels and as the number of panels in my project was only going to grow - this does not seem like the ideal solution in its current form.
In my head I thought there maybe an option in Visual Studio 2013 that allows me to 'hide' the Panels I am not using on the form, or drag them off the form temporarily. Is that an option in Visual Studio.
If not do any of you know a more efficient and manageable way of achieving this?
Thanks in advance.
If you are stuck using WinForms, your best bet is probably using UserControls. you can actually extend the UserControl class out to be a "page" ie: UserControlPage. This makes the form much simpler in function, but you will need to do some finicky work with handling events /passing data if the controls need to talk to each other.
if you aren't nailed into using Winforms, WPF supports all of this natively, and has wonderful tools for building all the pages you would need, and storing/populating your data, and propagating events.
If you want to have single form with changing content, and you don't want to mess up with panels in one form, then solution is user controls. You will be able to create them dynamically and add to form controls. Also there is no mess, because your form will be very simple - you can have single 'placeholder' control which will be used to dock user control which is currently displayed (e.g. panel control):
private void ShowContent(Control content)
{
placeHolderPanel.Controls.Clear(); // clear current content
placeHolderPanel.Controls.Add(content); // add new
content.Dock = DockStyle.Fill; // fill placeholder area
}
Usage:
private void button1_Click(object sender, EventArgs e)
{
ShowContent(new FooUserControl());
}
You could subclass the Panel class and create as many of those custom panels as needed, then they would be inserted on your Main Form, and managed as you described.
The advantage is that you would be able to individually edit them as a separate user control.
The drawback is that you lose direct event handling of controls on those panels from the main form. You can still define your own events on those panels and delegate the individual control events.
There's always a trade-off somewhere.
Cheers

Multiple XAML layouts in WPF

App I am trying to create in WPF/C# has quite a few buttons in a layout with a "TV screen" type panel above (its actually an FMS emulator for commercial aircraft). Many of the buttons change the layout, which are numerous TEXTBOXs on the tv screen. My question is: is there a provision to encapsulate the layouts in different classes/files and load them into the "tv screen" at the selection of the various buttons? In other words, user hits the Flight Plan button and the layout of the 355x355 box (screen) above loads the XAML "flight_plan" layout/file/class. Each layout has different TEXTBOX sizes & locations and there are in excess of 30 different "pages", which would make encapsulating them desirable.
I am very new to WPF and c#, but have written win apps in c++ all the way back to Turbo C & OWL. I also may be trying to do something that isn't possible due to working lately in Android/Java and am confusing capabilities.
Thanks in advance.
Edit
Thanks to #adityaswami89 and everyone else who got me on the right track, I have found the solution. I added the pages via a new "WPF Page" in VS2012. Then changed the "screen" to a navigation frame and it was truly simple from there. Below is the simple project I created to test it.
public partial class MainWindow : Window
{
NavRad navrad = new NavRad();
FPlan fplan = new FPlan();
public MainWindow() {..}
private void Frame_Navigated_1(object sender, NavigationEventArgs e) {..}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Screen_Frame.Navigate(fplan);
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
Screen_Frame.Navigate(navrad);
}
You can also use the concept of Frames for the intended functionality , if that can be an option you are looking.
You can refer the below link for the same.
http://msdn.microsoft.com/en-us/library/ms750478.aspx#Frame_in_Standalone_Applications
You can abstract the different UI Layout Sets within different User Controls and load them according your UI logic. One way to do this is using an MVVM framework, for example, Caliburn Micro makes this a pretty simple task as doing:
ActivateItem(UILayoutViewModel);
And this call can be called from any method.
See more of Caliburn Screens and Composition at official source.

Categories

Resources