I have added a new window to the project and the name of the window is Actwindow. How can I open this window?
I tried using Actwindow=new Window(); but it does not work.
The window code is:
using System;
namespace myproject
{
public partial class Actwindow : Gtk.Window
{
public Actwindow () :
base (Gtk.WindowType.Toplevel)
{
this.Build ();
}
}
}
What am I doing wrong?
Actwindow myWindow = new Actwindow();
myWindow.Show(); // or similar
Related
I have the following window using Windows App SDK 1.1.5 and WinUI 3, I want to keep it always at the bottom, just above the desktop, m_AppWindow.MoveInZOrderAtBottom(); moves it to the very bottom as I intended, but once it is pressed it comes back to the foreground.
How can I prevent it from coming to the foreground once clicked? I suppose it may be related to the HWND handle.
using Microsoft.UI;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using System;
using WinRT.Interop;
namespace Widgets {
public sealed partial class MainWindow : Window {
private AppWindow m_AppWindow;
public MainWindow() {
this.InitializeComponent();
m_AppWindow = GetAppWindowForCurrentWindow();
m_AppWindow.MoveInZOrderAtBottom();
}
private AppWindow GetAppWindowForCurrentWindow() {
IntPtr hWnd = WindowNative.GetWindowHandle(this);
WindowId wndId = Win32Interop.GetWindowIdFromWindow(hWnd);
return AppWindow.GetFromWindowId(wndId);
}
}
}
This is just a slightly modified MainWindow.cs from the visual studio Blank App, Packaged (WinUI 3 in Desktop) C# template.
Thanks in advance.
You can try it this way.
public sealed partial class MainWindow : Window
{
private AppWindow m_AppWindow;
public MainWindow()
{
this.InitializeComponent();
m_AppWindow = GetAppWindowForCurrentWindow();
m_AppWindow.Changed += M_AppWindow_Changed;
}
private void M_AppWindow_Changed(AppWindow sender, AppWindowChangedEventArgs args)
{
if (args.DidZOrderChange is true)
{
m_AppWindow.MoveInZOrderAtBottom();
}
}
private AppWindow GetAppWindowForCurrentWindow()
{
IntPtr hWnd = WindowNative.GetWindowHandle(this);
WindowId wndId = Win32Interop.GetWindowIdFromWindow(hWnd);
return AppWindow.GetFromWindowId(wndId);
}
}
I have a textbox (TextBoxMoeda) made by me, but, even when the textbox property readonly is set true. nothing happens. Anyone knows how to implement the code to resolve this problem?. Please help me?
public class TextBoxMoeda : TextBox
{
private double dp;
private string fmt = string.Empty;
private int _CasasDecimais = 0;
[Category("TextBoxMoeda")]
public virtual bool SomenteLeitura
{
get => base.ReadOnly;
set
{
base.ReadOnly = value;
Invalidate();
}
}
//Code Continues .......
}
WPF
You want the IsReadOnly property, not the ReadOnly property.
For example:
Create a new WPF App (.NET Framework) project called DeleteMe
In MainWindow.xaml, delete the Grid
In MainWindow.xaml.cs, use this code:
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
namespace DeleteMe
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var tm = new TextBoxMoeda();
this.AddChild(tm);
tm.SomenteLeitura = true;
}
public class TextBoxMoeda : TextBox
{
[Category("TextBoxMoeda")]
public virtual bool SomenteLeitura
{
get => base.IsReadOnly;
set
{
base.IsReadOnly = value;
}
}
}
}
}
The textbox will be readonly (i.e. I can't type anything into the textbox).
WinForms
The code already appears to work in WinForms. These steps will produce a working project:
Create a new Windows Forms App project called DeleteMeAgain
Overwrite all of the Form1.cs code with this:
using System.ComponentModel;
using System.Windows.Forms;
namespace DeleteMeAgain
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var tm = new TextBoxMoeda();
this.Controls.Add(tm);
tm.SomenteLeitura = true;
}
public class TextBoxMoeda : TextBox
{
[Category("TextBoxMoeda")]
public virtual bool SomenteLeitura
{
get => base.ReadOnly;
set
{
base.ReadOnly = value;
Invalidate();
}
}
}
}
}
The textbox will be readonly (i.e. I can't type anything into the textbox).
Windows Form: User Control
Do this:
Create a new Windows Forms App called DeleteMe3
Set Target Framework to .NET Core 3.1 (Long-term support)
In your project, create a new UserControl with the filename TextBoxStack.cs
Overwrite all of the code for that user control with this:
using System.ComponentModel;
using System.Windows.Forms;
namespace DeleteMe3
{
public partial class TextBoxStack : TextBox
{
[Category("TextBoxStack")]
public virtual bool SomenteLeitura
{
get => base.ReadOnly;
set
{
base.ReadOnly = value;
Invalidate();
}
}
}
}
An error will appear. Go to the source of the error and delete that line.
Build the solution
Add your new user control to Form1
Select the new user control on the form
Change the SomenteLeitura property to True
Run the project. The textbox should be grey and readonly.
I have a WPF application and here is the application structure:
Views
MainWindow.xaml, ABC.xaml (all with .cs files)
ViewModels
MainWindowVM.cs, ABCVM.cs
I have a Button in MainWindow.xaml (bound to MainWindowVM.cs) that calls a function, SampleFunction() in MainWindowVM.cs when being clicked and the SampleFunction() then creates a new instance of ABC.xaml (bound to ABCVM.cs) and open a new window of ABC.xaml using Show() function.
How can I make sure that clicking the Button in MainWindow would not open another new window of ABC.xaml when the old window is still there, or not create another new instance of ABC.xaml?
MainWindow.xaml.cs
public partial class MainWindow : Window
{
/*...Some other codes...*/
private MainWindowVM _VM = new MainWindowVM();
public MainWindowVM MainWindowVM
{
get { return _VM; }
set { _VM= value; }
}
public MainWindow()
{
InitializeComponent();
this.DataContext = MainWindowVM;
}
private void SomeControl_MouseLeftButtonUp(object sender,MouseButtonEventArgs e)
{
MainWindowVM.SampleFunction();
}
}
MainWindowVM.cs
public class MainWindowVM
{
/*...Some other codes...*/
public void SampleFunction()
{
ABC abc= new ABC();
abc.Show();
}
}
ABC.xaml.cs
public partial class ABC: Window
{
/*...Some other codes...*/
private static ABCVM _abcVM= new ABCVM();
public ABCVM ABCVM { get { return _abcVM; } set { _abcVM = value; } }
public ABC()
{
InitializeComponent();
this.DataContext = ABCVM;
}
}
Use ShowDialog() instead of Show().
Then you have to close the ABC.xaml first, before you can make something on the MainWindow. So you can't open a second ABC.xaml Window.
You can write code to check whether a window type object exists or not.
for each(Window win in Application.Current.Windows)
{
string windowType = win.GetType().ToString();
if (!windowType.Equals(nameSpace + "." + ABC))
{
ABC abc= new ABC();
abc.Show();
}
}
I am trying to create a C# Application with Chrome-Style Tabs using EasyTabs in WinForms, but I am getting the following error code:
The designer must create an instance of type 'EasyTabs.TitleBarTabs' but it cannot because the type is declared as abstract.
I have followed the Youtube-tutorial beneath as a guideline.
https://www.youtube.com/watch?v=WVFjegJK8EY
Code:
using System;
using EasyTabs;
namespace WindowsFormsApp1
{
public partial class AppContainer : TitleBarTabs
{
public AppContainer()
{
InitializeComponent();
AeroPeekEnabled = true;
TabRenderer = new ChromeTabRenderer(this);
}
public override TitleBarTab CreateTab()
{
return new TitleBarTab(this)
{
Content = Form1
{
Text = "New Tab"
}
};
}
private void AppContainer_Load(object sender, EventArgs e)
{
}
}
}
It is normal to see this error when you try to view AppContainer.cs directly.
The designer must create an instance of type 'EasyTabs.TitleBarTabs' but it cannot because the type is declared as abstract.
Let that not deter you from achieving what you wanted to achieve. Ignore the error and simply right click on AppContainer.cs and select View Code
using EasyTabs;
namespace WindowsFormsApp1
{
public partial class AppContainer : TitleBarTabs
{
public AppTabs()
{
InitializeComponent();
AeroPeekEnabled = true;
TabRenderer = new ChromeTabRenderer(this);
Icon = mBible.Properties.Resources.appico;
}
public override TitleBarTab CreateTab()
{
return new TitleBarTab(this)
{
Content = new Form1
{
Text = "New Tab"
}
};
}
}
}
That should be enough to generate new tabs for you.
Now to create tabs easily lets assume you have a form 'MainForm' and the tab you want to be generating is 'Form1' here is what we do
using System;
using System.Windows.Forms;
using EasyTabs;
namespace WindowsFormsApp1
{
public partial class MainForm : Form
{
public static AppContainer tabbedApp = new AppContainer();
public MainForm()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
tabbedApp.Tabs.Add(new TitleBarTab(tabbedApp)
{
Content = new Form1
{
Text = "New Tab"
}
});
tabbedApp.SelectedTabIndex = 0;
TitleBarTabsApplicationContext applicationContext = new TitleBarTabsApplicationContext();
applicationContext.Start(tabbedApp);
this.Hide();
}
}
}
You can always add to your tabs a new tab easily by calling this code on another form
AppContainer.tabbedApp.Tabs.Add(new TitleBarTab(AppContainer.tabbedApp)
{
Content = new Form2 { Text = "Another Tab" }
});
AppContainer.tabbedApp.SelectedTabIndex = 0;
I hope that helps and anyone looking for help on similar issues.
Hello I have a simple WPF window and I want to load and display a logo image on specific "Image" item. The following code works perfectly and I can display the logo.
namespace WPFexample
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var uri = new Uri("pack://application:,,,/Assets/Logo.png");
LogoImage.Source = new BitmapImage(uri);
}
}
}
Now I want to create another class which can access the GUI and display from there the logo:
namespace WPFexample
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
class MyClass
{
private void myCustomDisplay()
{
var uri = new Uri("pack://application:,,,/Assets/Logo.png");
// How can I call the LogoImage.Source from this place??
}
}
}
Initially I thought that I could do this but nothing happened:
class MyClass
{
private void myCustomDisplay()
{
MainWindow MainWindowClassRef = new MainWindow();
var uri = new Uri("pack://application:,,,/Assets/Logo.png");
MainWindowClassRef.LogoImage.Source = new BitmapImage(uri);
}
}
Any suggestion why I cannot display it?
This does not work either:
namespace WPFexample
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public void DisplayInGUI()
{
var uri = new Uri("pack://application:,,,/Assets/ITLLogo.png");
LogoImage.Source = new BitmapImage(uri);
}
}
class MyClass:MainWindow
{
public void myCustomDisplay()
{
DisplayInGUI()
}
}
}
To access a member (e.g. property or method) of the MainWindow instance, you can cast the MainWindow property of the current Application instance to your MainWindow class.
So e.g. call the MainWindow's DisplayInGUI() method by
((MainWindow)Application.Current.MainWindow).DisplayInGUI();
You probably want to pass the actual logo image as parameter to a method like
public partial class MainWindow : Window
{
...
public void SetLogoImage(ImageSource image)
{
LogoImage.Source = image;
}
}
and call it like this:
((MainWindow)Application.Current.MainWindow).SetLogoImage(
new BitmapImage(new Uri("pack://application:,,,/Assets/ITLLogo.png")));
EDIT: Call the method from another thread like this:
var mainWindow = (MainWindow)Application.Current.MainWindow;
mainWindow.Dispatcher.Invoke(() => mainWindow.DisplayInGUI());
MyClass needs to have a property or field called LogoImage. The "partial" keyword indicates that MainWindow has part of its class definition somewhere else - probably in a class called MainWindow.xaml. I would try looking at that file and moving the relevant portions of it to MyClass.
It might also help for MyClass to derive indirectly from UIElement. If you ask your IDE to create it as a UserControl in a new file, it will probably generate some placeholder xaml for you as well.