Using widgets in MonoDevelop and GTKSharp - c#

I switched from using VirtualStudio Express 2010 and I am trying to work with MonoDevelop and GTKSharp. Now. I am trying to switch to new editor software but it seems much different than VisualStudio.
What I am trying to do is basically to use widgets in this editor. For example when I am creating a button in VisualStudio and then double click the item then I am automatically getting the piece of code representing the item in the form. And here is the problem, how do I create events for buttons and comboboxes in MonoDevelop? I was looking through Internet examples for two days now and I can not figure out how to do it. The examples are not clear enough.
What I am trying to create? First I am trying to figure out how to use ComboBox and button which will allow me to choose one of 3 options in ComboBox and then under button event I want to fire 1 of 3 separate windows depending on which item has been picked.
Please provide me some easy examples how to work with MonoDevelop or else I will need to switch back to Windows OS :(
Please help!
// edit //
Lets say I got time on my hands and I am really interested in it. So if GTK# allow me so far:
public MainWindow () : base(Gtk.WindowType.Toplevel)
{
Build ();
button1.Clicked += button1_Click;
combobox1.SelectionGet += comboBox1_Selection; << is this correct?
}
private void button1_Click(object s, EventArgs e)
{
}
private void comboBox1_Selection (object s, EventArgs e)
{
switch (combobox1.SelectedIndex)
{
case 0:
window1.Show();
break;
case 1:
window2.Show();
break;
case 2:
window3.Show();
break;
}
}
But I feel like I am more lost than I was earlier.

Create a new C# GTK project.
Open up the "MainWindow.cs" and at the bottom right of the window click the "Designer" button to go into designer mode.
Next open up the hidden toolBox window at the right of the MonoDev window. Drag out a "Fixed" Container on the main windows canvas. This is required to put Buttons and stuff on your window.
Now drag a button on the Fixed container. To move the button around click on the little white box above the button when selected.
Now go to your SourceCode again. In the constructor write::
button1.Clicked += button1_Click;
Then make the new Click method.
private void button1_Click(object s, EventArgs e)
{
}

You don't need to add the handlers manually...
In Gtk the concept names changes a little, when a widget which is the same as a control in windows forms, just different name, does something it emits a "signal" then what you have to do is to "handle" that signal which would be the equivalent to catch the event in windows forms.
You can just select your widget in monodevelop and then go to the properties pane of that widget, there you will see a tab called "signals", this has the list of the signals that the widget specifically emits, thus allowing you to code actions when the widget does something like being clicked. Once there just double click in the signal you want to handle, for example, for a button, double click on the "Released" signal which is the signal emitted by the button when you click on it and release it.
I'm attaching a screenshot so you can get the picture. Hope it helps!!

Related

C# 2015 Public Class

My problem is whenever I double click on an object (e.g picturebox), it enters another window where I can do my coding (which is normal) however, it starts with:
private void pictureBox1_Click(object sender, EventArgs e)
which I don't understand :(
How can I make it into Public Class so I can change the event to "mouse enter" or "mouse leave"?
Thank you in advance.
If all you want to do is switch from design view to code view then use the F7 key. In older versions of VS, F7 would switch back again too but in later versions you use Shift+F7 to switch from code view to design view.
When in design view, you can select the form or a control/component, open the Properties window, click the Events button and then create or select a handler for any event and/or jump to it in code view. When in code view, you can use the drop-down lists at the top to select the form or a control/component or any field declared WithEvents and to create and/or jump to the handler for that event.
Already resolved. I was able to do it by creating another project and choosing the windows form application as visual basic, not c#.

What is the event which fires every time when i click outside for my wpf page?

i am developing windows application using wpf. I want to do some functionality when i click outside of my control created. for example, if i have Message-box open in my window, i want to do some function if i click outside of my Message-box window.
I tried,
private void OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
.....
}
but its not working.. please any one tell me, what is the event fire when i click outside of my control?
you have two supposed solutions:
one of them is to get Mouse.X, Mouse.Y from System not from application, this article will help
http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C
second which is better is not using Dialog, but use PopUp window and this article will help
how to close a WPF Dialog Window when the user clicks outside it

Making a new form window appear in c#

I'm trying to make a card game using Windows Application Forms.
The thing is that I don't know how to do the following- for example if i'd have 3 buttons- one of them named, for example, "Play", if i'd click on it, it would open the actual game, but in the same window, it would only make the buttons dissapear, and when i'd click back, it would open the window with buttons again. I don't really know how to explain my problem better, hopefully someone can tell me how to do that.
You don't have to hide / show the buttons. What you can do instead is to make a new form with the cards on it. That Form will pop up after you click the play button.
private void PlayButton_Click(object sender, EventArgs e)
{
// You other functionality goes here
GameForm GF = new GameForm();
GF.Show();
//Or - try this and see the difference
GF.ShowDialog();
}
Good Luck!
In addition to Leez's answer, in your situation, you should think about using container controls rather than handling the visible states of individual controls.
You could put related controls in a Panel, GroupBox or TabControl and set the visible properties of those containers instead.
you can use Visible property of button to do that as follows.
private void button1_Click(object sender, EventArgs e)
{
// You other functionality goes here
button1.Visible = false;
}

Using a Picture Box as a button, how do I ignore/disable double-click events?

I'm having some difficulty using picture boxes as buttons. I'd like to have custom button graphics rather than using the plain-old windows forms Button control.
It works pretty well, except the buttons seem to lag when a single button is pressed rapidly. I'd like the OnClick method to be called immediately every time the Picture Box is clicked, but it seems to be registering a double-click event, even though I am not using that event.
The short version is I'd like to:
(Ideal) Disable the double-click event, since it appears this is what the Control is waiting for on every alternating click of the Picture Box and I never use it. Or,
(Acceptable) Write a generic DoubleClick event I can use for all Picture Boxes I plan to use as buttons.
I did write up a test program which shows that this is, in fact, what's happening. I wrote a MouseUp event that increments a counter by 1, and also added a OnClick even that increments a different counter by 1. When rapidly clicking the Picture Box linked to these events, the MouseUp counter is exactly double that of the OnClick counter.
I then added a DoubleClick event that calls the OnClick event. This, although not ideal, solves the problem perfectly well. If there is a way to write a generic DoubleClick event that all buttons could use, I think this would be acceptable.
Right now my DoubleClick event is just
private void pb_DoubleClick(object sender, EventArgs e)
{
pbOK_Click(sender, e); //This needs to be generic
}
I'd like to at least replace the DoubleClick line with something like
private void pb_DoubleClick(object sender, EventArgs e)
{
(Sender.ToString)_Click(sender, e); // Doesn't work quite like this.
}
I could write a special DoubleClick event for each Picture Box individually, but that is far from ideal and seems quite cumbersome, so I would prefer not to do that if it isn't necessary.
This is my first posted question, so thanks to anyone who takes the time to read this. Any advice would be greatly appreciated.
EDIT: This program is being developed for Windows CE, so the properties and events options for the objects is quite limited. I still may be missing something, but as far as I can tell the Properties list for buttons goes straight from GenerateMember to Location, with none of the Image options available. I apologize for not stating the target platform in the original post.
It is done with the Control.SetStyle() method, like this:
using System;
using System.Windows.Forms;
class MyButton : PictureBox {
public MyButton() {
this.SetStyle(ControlStyles.StandardDoubleClick, false);
}
}
Okay, my original answer was trash. My update:
Here's a link on how to get a controls Method name:
Link
In the link it has a Control c. To get your control (sender) use the following:
PictureBox picbox = (PictureBox) sender;
Next, call a method using a string(your class.Method.Name): Link
Now that should work. Hope it does.
Why don't you create your own control? This way, you don't have to do anything to get the behavior you want.
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public class ButtonPictureBox : PictureBox
{
protected override void OnDoubleClick(System.EventArgs e)
{
OnClick(e);
}
}
}
I don't know if this is possible on Windows CE though.
If you also want to hide the DoubleClick event in the form designer, you can add this to the class:
[Browsable(false)]
public new event EventHandler DoubleClick;

Control.Enter event doesn't fire when switching tasks. Is there an alternative that does?

Rep steps:
create example .NET form application
put a TextBox on the form
wire a function up to the TextBox's Enter event
When you run this application, the Control.Enter event fires when focus first goes to the TextBox. However, if you click away into another application and then click back into the test application, the event will not fire again.
So moving between applications does not trigger Enter/Leave.
Is there another alternative Control-level event that I can use, which will fire in this scenario?
Ordinarily, I would use Form.Activated. Unfortunately, that is troublesome here because my component is hosted by a docking system that can undock my component into a new Form without notifying me.
What are you trying to do in the Enter event?
I can't find another control-level event that fires in your example program but when my test app does regain focus, the control that last had focus still has it.
Interesting question but it needs a little more context.
If I try your example and click outside the control on another window, desktop, etc, I can get the Got and Lost Focus events to fire, but if you're only trying to click within a form or a control with only 1 control, these event will never be fired because it is the only thing to focus on. Neither will Entered or left, unless you change the dynamics or overload the controls, you cannot get this to happen
In your example, I think you need another control. The reason being is that the first control (tabIndex 0) is the one with focus. With no other control to switch focus to, this control will always be focused, and therefore can never be entered. Switching to another application or form will not change the focus or active control in this form so when you return you will still not get the event fired.
With added controls control.entered should work fine. If this is your only control, why not call the event on formLoad, or TextChanged, when the form gets focus?
Thanks, I'll give some background.
My control is a UserControl that contains a grid and a toolbar. A user will typically launch several of these controls to view different slices of the system's data.
There are several keyboards shortcuts that can launch actions from the selected row in the current grid. However, it is a requirement that these keyboard shortcuts should apply not only to the currently focused grid. If the user is currently focused on one of the many other areas of the application, then this keyboard shortcut should still work, and it should be routed to the last focused grid.
So I wired a function to the Control.Enter event of my UserControl to basically say LastFocusedGrid = this.
And it would work, except for the docking and undocking...
See, these controls are hosted inside an application with docking features, somewhat similar to visual studio.
By default, the control launches as a tab within the main working area of the application, similar to the way a source file opens in visual studio.
However, the user can "rip out" a tab by grabbing the tab header and dragging it out of the main application. At this point, the application creates a new "float form" to host the control. Switching between the main application and this float form is the same as switching between apps, for the purposes of the Control.Enter and Form.Activated events.
At that point we have the "one control within a form" scenario simulated with the example application described in the original post.
Now, there are some ways around this. I could leverage the Form.Activated event, which DOES fire when switching between forms. If you add an event in the test application to the Form's Activated event, you will see that it works great.
The problem is that my UserControl's relationship with its parent Form is fluid, making the solution somewhat complicated. I tried wiring up to "this.ParentForm.Activated" which worked okay. The problem is when do you call this? What happens when you are undocked/redocked? I ended up with a nasty bunch of code with things like "previousParentForm" so that I could unhook from the old form, and then I was still facing the problem that the docking system doesn't notify me when my parent Form is being changed, so I was going to have to make a bunch of changes there, too.
These problems are not unsolvable, but if there is a simpler control-level "parent form was activated" event, then that would be a lot more elegant.
That's rather long, but I hope it clarifies the situation.
So when creating your grid, can you not set the KeyPressed, or KeyUp, etc. event? If so, all the grids can make use of the same event handler. Just make sure that when you get into the event handler to do something like:
Grid currentGrid = (Grid)sender;
Then you should be able to apply that block of code to any grid that gets sent in without having to worry about keeping track.
Since all the event handler really is, it's location is a mute point really as long as everything you need to execute it is accessible.
Frye, the problem is that the keyboard shortcuts should work no matter where the user is in the application. They are gloabl commands, handled at the top level, and then routed to the "last focused grid."
So handling the keystrokes at the grid level will not help.
To be more specific, assume user launches grids A, B, and C. But he also launches other controls X, Y, and Z that have nothing to do with my code.
User clicks on A, then on C. Then he clicks on Y, then on Z. With focus on Z, he hits my keyboard shortcut. In this case, grid C should respond since it was the last grid the user was focused in.
It sounds like the issue that you're having is not directly related to the Enter event and more to the point, if you have controls "that have nothing to do with your code" then you really aren't looking at a control level event.
Guess I wasn't clear.
My control lives in a container application. So do other unrelated controls by other teams. Think of it like visual studio -- my control is the code editing tab, but there is also the pending changes list and the properties window, which cohabitate with the source files but aren't directly related.
The keyboard shortcut is handled by the container application. Then it should be routed to the last one of my controls that the user was focused on.
Maintaing this "LastFocusedGrid" reference is what I do in the Enter event.
If you want to see similar functionality at work in visual studio, try this:
open a few source files
navigate to the "Start Page" tab.
Hit Ctrl-F and search "current document" for some string
Notice that the serach feature auto-navigates to the LAST FOCUSED source file to perform the search.
So even though you weren't focused in the source file, the ctrl-F command was processed by visual studio and routed to the last focused source file tab.
Now try the same thing with Ctrl-G. It doesn't work unless you are focused directly in the source file.
My keyboard commands need to work like Ctrl-F here, not like Ctrl-G. That is why I don't just capture the keyboard events directly in my control.
Does that clarify or make things worse?
Have you tried just a simple Control.GotFocus?
in this example if you toggle between clicking the textboxes neither the enter or got focus will do as expected, however if you click the child forms instead both will behave as expected.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace EnterBrokenExample
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form Form1 = new Form();
Form c1 = new Form();
Form c2 = new Form();
Form1.IsMdiContainer = true;
c1.MdiParent = Form1;
c2.MdiParent = Form1;
c1.Show();
c2.Show();
TextBox tb1 = new TextBox();
c1.Controls.Add(tb1);
tb1.Enter += ontbenter;
tb1.Text = "Some Text";
tb1.GotFocus += ongotfocus;
TextBox tb2 = new TextBox();
c2.Controls.Add(tb2);
tb2.Enter += ontbenter;
tb2.Text = "some other text";
tb2.GotFocus += ongotfocus;
Application.Run(Form1);
}
static void ontbenter(object sender, EventArgs args)
{
if (!(sender is TextBox))
return;
TextBox s = (TextBox)sender;
s.SelectAll();
}
static void ongotfocus(object sender, EventArgs args)
{
if (!(sender is TextBox))
return;
TextBox s = (TextBox)sender;
s.SelectAll();
}
}
}

Categories