I am working in monodevelop and learning c#....
I am trying to get a message box to appear but I can't get it to function correctly ...
Here is my code:
using System;
using Gtk;
using GtkSharp;
public partial class MainWindow : Gtk.Window
{
public MainWindow () : base(Gtk.WindowType.Toplevel)
{
Build ();
}
protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
Application.Quit ();
a.RetVal = true;
}
protected virtual void OnButton11ResizeChecked (object sender, System.EventArgs e)
{
System.Windows.Forms.MessageBox.Show("Hello World Again!!");
}
}
What am i missing?
You cannot mix the GTK# and System.Windows.Forms UI toolkits. You need to use a GTK dialog, something like this:
void ShowMessage (Window parent, string title, string message)
{
Dialog dialog = null;
try {
dialog = new Dialog (title, parent,
DialogFlags.DestroyWithParent | DialogFlags.Modal,
ResponseType.Ok);
dialog.VBox.Add (new Label (message));
dialog.ShowAll ();
dialog.Run ();
} finally {
if (dialog != null)
dialog.Destroy ();
}
}
See also this question.
You are referencing GTK which is the bundled graphical toolkit in mono but are trying to use Windows.Forms which, although included in mono too, is a different toolkit:
System.Windows.Forms: This is the toolkit used in windows, the implementation on mono "emulates" how this controls are drawn and behave under platforms that mono runs on.
Gtk: This is a toolkit used in many OpenSource applications (Firefox, Pidgin, etc) and GTKSharp is simply the implementation of this same library but exposed to the .Net languages available on mono although you could use it directly with Visual Studio or a Microsoft compiler too.
So summarizing, as Mike said, you cannot use them both, you have to choose either one. If you are just learning .Net I would greatly advice to learn GTK instead of Windows Forms. Windows forms is kind of poor and basic toolkit, and soon you'll find that you'll need to learn a new API from a third party to do stuff that windows forms cant do (DevExpress, Infragistics) and Gtk can be easily extended and adjusted to your needs.
Related
I'm converting a C/GTK+ GUI application to C# using GTKSharp in VS2017. I've installed this package https://www.nuget.org/packages/GtkSharp/3.1.3 via NuGet.
Here's how I load up the CSS (the application uses a Glade file to define the interface):
static void Main(string[] args)
{
new Program(args);
}
public Program(string[] args)
{
Application.Init();
builder = new Builder();
Gtk.CssProvider provider = new CssProvider();
builder.AddFromFile("interface.glade");
provider.LoadFromPath("style.css");
builder.Autoconnect(this);
Gtk.Window window = (Gtk.Window)builder.GetObject("start");
Gtk.StyleContext.AddProviderForScreen(Gdk.Screen.Default, provider, 800); // couldn't find the equivalent to GTK_STYLE_PROVIDER_PRIORITY_USER so I set the priority to a random number
window.Show();
Application.Run();
}
The selector names seem to be different from GTK+. For example,
window {
...
}
works in C/GTK+ but not in C#, whereas
GtkWindow {
...
}
works in C# but not in C/GTK+. Then there are a few widgets I can't seem to style at all. For example,
button {
...
}
works in GTK+ but
GtkButton {
...
}
does not work in C#. I couldn't find any documentation regarding how GTK# handles CSS styling so I thought it'd be the same as GTK+. Any pointers?
The GTKSharp seems more like an expected GTK3 behavior.
Here is the reference manual from developer.gnome.org
Espcially useful should be the Table 1. Selector syntax section.
In short the elements are named after the GTK class name: GtkButton, GtkLabel and so on.
For a class list of default GTK3 wigets check out the docs talbe of content..
The GTK button is a container wiget that doesn't render background so without seeing the actual CSS properties you try to apply using that selector I can't tell you why it doesn't work but so you might need to style it content separately. Eg.
GtkButton GtkLabel {
color: lime;
}
The selector itself GtkButton should be correct.
It was a version issue.
Version 3.22 of GTK# detects the GtkButton selector correctly. I was using GTK 3.14's native libraries. There is one unlisted NuGet package that provides win32 libraries for the 3.22 version. Strangely enough, that version detects the old "button", "window"... instead of the "GtkButton", "GtkWindow"... tags.
I am new to c# and visual studio and have run into some trouble.
I have created a project with references to "Windows" and ".Net" in visual studio because I want to test a little with smart cards.
The code:
using System;
using Windows.Devices.Enumeration;
using Windows.Devices.SmartCards;
class HandleSmartCard
{
public async void checkNumberOfSmartCards()
{
string selector = SmartCardReader.GetDeviceSelector();
DeviceInformationCollection devices = await DeviceInformation.FindAllAsync(selector);
// return "2";
}
}
So far it looks fine. However I also want the project to be able to use
System.Windows.Forms; which I have used in a previos test.
I add reference to System.Windows.Forms; and try to create a form. However in that form when I try this:
Form prompt = new Form();
System.Windows.Forms.Button confirmation = new System.Windows.Forms.Button() { Dock = DockStyle.Bottom };
confirmation.Click += (sender, e) => { prompt.Close(); };
I get a red line under "Close" with the message:
Reference to type component claims it is defined in system but could
not be found.
System is referenced at top of file, but I am guessing it is the wrong type of system right?
Can I somehow use "both Systems" in one project so to speak?
I hope someone understands what I mean and can help me understand this.
You're most likely working on a UWP app. The API for UWP apps is a very small subset of the full .NET framework. You can find more information here
https://msdn.microsoft.com/en-us/library/windows/apps/mt185501.aspx
You're attempting to reference System.Windows.Forms which is not allowed in UWP applications.
Looks like you're trying to create a popup to ask the user something. For this, use the MessageDialog class.
https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.popups.messagedialog.aspx
I’m trying to use an embedded browser into my WinForms application. I used CefSharp ChromiumWebBrowser, it work perfectly. However, CEFSharp requires the Visual C++ 2013, or the Visual C++ 2008….I think it is a complicated dependency. I'm creating a portable application without the dependency envirenment.
I have found another library, similar CefSharp, also use Chrominum, called ChromiumFX (visit: https://bitbucket.org/chromiumfx/chromiumfx ). The library uses the .Net Framework 3.5 (Default available in Windows 7) , no need to install Visual C++. And it is suitable for my application.
But I don’t find any way to create multi tabs application with this library. Here is what I tried to do:
private void LifeSpanHandler_OnBeforePopup(object sender, CfxOnBeforePopupEventArgs e)
{
e.SetReturnValue(true);
tabPage2.InvokeOnUiThreadIfRequired(() =>
{
ChromiumWebBrowser b = new ChromiumWebBrowser(e.TargetUrl);
b.Dock = DockStyle.Fill;
tabPage2.Controls.Add(b);
});
}
InvokeOnUiThreadIfRequired is an extension method:
public static void InvokeOnUiThreadIfRequired(this Control control, Action action)
{
if (control.InvokeRequired)
{
control.BeginInvoke(action);
}
else
{
action.Invoke();
}
}
But the application error and the program has crashed.
Another thing, when I created a new instance of ChromiumWebBrowser, I want it isolated with another instance, it means do not use the same cookies, sessions with any instance (private browser). In CefSharp, just create a new RequestContext for new instance:
webbrowser.RequestContext = new RequestContext();
But in ChromiumFX, I dont find any way to do that!
Somebody help me? Thank for you help!
I'm currently following a tutorial from the MSDN which isn't very clear on somethings the issue that i am having is that the method that they are suggesting that i use is apparently not available to that class
Here is the link to the tutorial : http://msdn.microsoft.com/en-us/library/windows/apps/dn495655.aspx
Here is the code that i am using
In my App.Xaml.cs not my Main page i have an event handler
public App()
{
this.InitializeComponent();
Window.Current.SizeChanged += Current_SizeChanged;
this.Suspending += OnSuspending;
}
Underneath this the stub method
void Current_SizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
{
// Get the new view state but its not allowing me to use getforcurrent state
// almost like it doesn't exist
string CurrentViewState = ApplicationView.GetForCurrentView().Orientation.ToString();
// Trigger the Visual State Manager
VisualStateManager.GoToState(this, CurrentViewState, true);
}
If anyone else has followed this tutorial can they tell me what is going wrong ?
Have i put the code in the wrong page
Am i missing a library
I am following this microsoft tutorial word for word and it giving me the error which is the title of my post i have done research and i am using the latest version of visual studio and it's still not letting me use this method because it do not exist apparently
Try using Windows.UI.ViewManagement; at the top of the program.
The minimum supported version of this API is Windows 8.1. So you can't use it with the Windows 8 API.
MSDN reference
I've been experiemnting with the community version of ILNumerics 3.2.1.0 with .Net 4.0 in Visual Studio 2010 pro on Windows 7, and going through the documentation I succesfully get a windows form project to display a chart, using the code below.
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void ilPanel1_Load(object sender, EventArgs e)
{
ILSurface mySurface = new ILSurface(ILSpecialData.sincf(100, 200));
ILPlotCube myCube = new ILPlotCube(twoDMode: false);
myCube.Add(mySurface);
ilPanel1.Scene.Add(myCube);
}
}
If I try exactly the same code but from inside a VSTO Excel 2010 application all that is displayed in the form is the designer view of the ILPanel, blue circle on white background. I don't get any error messages. Am I missing something obvious? or does anyone have a solution of how to get the chart to display in VSTO?
Update
Thanks to Philliproso for pointing out the IsDesignMode() method. As pointed out in various places, including this question, Detecting design mode from a Control's constructor , the following method is not ideal, but for me is has provided a quick fix to allow me to evaluate ILNumerics.
public static bool IsDesignMode() {
if (System.Windows.Forms.Application.ExecutablePath.IndexOf("devenv.exe", StringComparison.OrdinalIgnoreCase) > -1)
{
return true;
}
return false;
}
This is the same issue as here:
Ilnumerics Ilpanel not activating when compiled in a winform into a dll when loaded into matlab
in-a-winform-into-a-dll-when-loa
Using VSTO as host for ILNumerics lets the panels assume, it was loaded in a designer. We are currently collecting possible workarounds and solutions. One solution might be to introduce a flag in the Settings of ILNumerics:
Hosted [default: false]
Your situation would require the flag to be enabled. In hosted mode, a blacklist of common designers could be checked at runtime and compared to the current entry assembly. Any other suggestions?