Access Gtk# (GtkSharp) Label child of a Button - c#

I have a C# project in MonoDevelop.
Long story short is I found I wasnt able to apply styling/pango/markup/images if I used the Button with Label in the Stetic GUI.
The documentation i read and some code I see said to make a Label or Image and pack it into the Button.
I did that for a Label and it worked successfully to style it.
A sample of the object Build method:
private void Build()
{
box = new HBox(false, 0);
box.SetSizeRequest(40, 40);
box.BorderWidth = 2;
button = new Button();
button.Clicked += cyclepoint;
lblpoints = new Label();
lblpoints.Text = "200";
lblpoints.ModifyFg(Gtk.StateType.Normal, new Gdk.Color(237, 52, 112));
button.Add(lblpoints);
button.ShowAll();
box.Add(this.button);
box.ShowAll();
this.Add(box);
}
So that is fine. Now I'm trying to attach the Signal so that whenever the Button is click it would change the Label Text for the particularly clicked button (as there will be multiple of the form.
Code I have for the signal:
private void cyclepoint(object sender, EventArgs args)
{
Console.WriteLine("Button Pressed!");
Console.WriteLine((Label)(((Button)sender).Child).Text);
}
Actual Output When I build the GUI and click the button
Button Pressed!
GtkLabel
Yet when I try to below to do a change the MonoDevelop IDe wont compile with error:
Error CS1061: 'Widget' does not contain a definition for 'Text' and no accessible extension method 'Text' accepting a first argument of type 'Widget' could be found (are you missing a using directive or an assembly reference?) (CS1061) (shump)
So I dont seem to be able to access the Text property of the Gtk.Label to change it's display Text. What is the correct way to go about doing this?

So in my regard. I dont believe I can access the "Child" element of the Gtk.Button object.
However, I've since discovered that I can access and edit the GTK.Label.Text property of the child Gtk.Label object directly from within the class. This will automatically update the Label with no necessary calls to Show() functions so will reflect the update immediately to boot! Furthermore, it also keeps the pango styling properties I had applied.
The end goal was to have a stylized countdown type of Button object that reduces the number as clicked and goal was accomplished.
private void cyclepoint(object sender, EventArgs args)
{
// Points is a different array that holds the increments of values
if (this.lblpoints.Text == points[10])
{
lblpoints.Text = points[0];
}
else
{
int nextval;
nextval = (Array.IndexOf(points, this.lblpoints.Text) + 1);
this.lblpoints.Text = points[nextval];
}
}

Related

How do I navigate to another page from another c# class

I have a main page which has a play, options and exit button, both the play and exit button work as I followed a tutorial on it however for the options button I do not know how to navigate it. What I am planning to do is to create another class called optionsMenu.cs and have like a credits screen or a how to play guide on it.
These are the codes that I have for my options button.
var optionsGameButton = new Button(buttonTexture, buttonFont)
{
Position = new Vector2(300, 250),
Text = "Options",
};
optionsGameButton.Click += OptionsGameButton_Click;
_components = new List<Component>()
{
titleGameButton,
playGameButton,
optionsGameButton,
exitGameButton,
};
Finally I know I have to write a code in this area to be able to get the options button working but I don't know which kind of code to put.
private void OptionsGameButton_Click(object sender, EventArgs e)
{
Console.WriteLine("Credits");
}
If you're set on using click events, then you'll need to create a new instance of your options page and set it as the content of your control object within OptionsGameButton_Click. On the options page you'll need to set the xmlns to your optionsMenu.cs file.
I think you could also set the content of your control object by passing it as a parameter within the method itself, but I've only done this using the ICommand interface.

Returning more than one variable in a C# 'get' statement

In my Excel Add-In, I have created two task panes - with each ones visibility being from two different values, requiring both to be in a return statement, however it will only allow me to return one of the values. These are 'taskPaneValue' and 'taskPaneValue2'.
How do I go about having both returned in the 'get' statement.
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
taskPaneControl2 = new FileChooser();
taskPaneValue2 = this.CustomTaskPanes.Add(taskPaneControl2, "File Chooser");
taskPaneValue2.VisibleChanged += new EventHandler(taskPaneValue_VisibleChanged);
taskPaneValue2.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionFloating;
taskPaneValue2.Height = 600;
taskPaneValue2.Width = 600;
taskPaneValue2.DockPositionRestrict = Office.MsoCTPDockPositionRestrict.msoCTPDockPositionRestrictNoChange;
//These three lines of code start by initiating the TaskPane control (namely aLaCarteMenu())
//It then goes on to set the name of the menu "A La Carte Menu" which appears on the top left of the window before stating its visibility.
taskPaneControl1 = new aLaCarteMenu();
taskPaneValue = this.CustomTaskPanes.Add(taskPaneControl1, "A La Carte Menu");
taskPaneValue.VisibleChanged +=
//The following four lines of code are used to display the visiblity of the AddIn.
//The docking position is set to float, with a resolution of 980x1920. This is designed for a 1080p screen, however still working on changing it to fit screens dynamically.
new EventHandler(taskPaneValue_VisibleChanged);
taskPaneValue.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionFloating;
taskPaneValue.Height = 980;
taskPaneValue.Width = 1920;
//This line of code sets the position to be restricted to what has been set above (floating). This allows for the pane to be moved around the screen, as well as to be resized.
//This stops the pane from locking on to the right, left, top or bottom sections of the Excel Window.
taskPaneValue.DockPositionRestrict = Office.MsoCTPDockPositionRestrict.msoCTPDockPositionRestrictNoChange;
}
private void taskPaneValue_VisibleChanged(object sender, System.EventArgs e)
{
Globals.Ribbons.ManageTaskPaneRibbon.toggleButton1.Checked = taskPaneValue.Visible;
Globals.Ribbons.ManageTaskPaneRibbon.toggleButton2.Checked = taskPaneValue2.Visible;
}
public Microsoft.Office.Tools.CustomTaskPane TaskPane
{
get
{
return taskPaneValue2;
}
}
The final 'get' statement is the one I wish to return both variables.
Use a Tuple or create a class that has all the properties you are looking for and make that the return type of the function.
You can also create a method with out parameter modifier, if it'still possible to you. Please, check the following link: https://msdn.microsoft.com/en-us/library/ee332485.aspx

ObjectListView editing doesn't work

I'm trying to create a simple listbox with ObjectListView (WinForm, C#). The goal is to have a single value (a double) and a check box.
I want to be able to edit the double value by Single Click, so here are the relevant lines of code from my MyWindow.Designer.cs file (i've left out the default values for efficiency):
this.olvDepths = new BrightIdeasSoftware.ObjectListView();
this.olvColumn1 = ((BrightIdeasSoftware.OLVColumn)(new BrightIdeasSoftware.OLVColumn()));
...
this.olvDepths.CellEditActivation = BrightIdeasSoftware.ObjectListView.CellEditActivateMode.SingleClick;
this.olvDepths.CheckBoxes = true;
this.olvDepths.CheckedAspectName = "IsDefault";
this.olvDepths.FullRowSelect = true;
//
// olvColumn1
//
this.olvColumn1.AspectName = "Depth";
this.olvColumn1.Text = "";
this.olvColumn1.IsEditable = true;
I then create a list of my class (ShieldingEntry) and use the olvDepths.SetObjects() with the list. My ShieldingEntry class looks like this:
public class ShieldingEntry
{
public double Depth { get; set; }
public bool IsDefault { get; set; }
}
However, when I click the field, it doesn't go into edit mode. I've also tried the DoubleClick, SingleClickAlways, and F2Only modes and they don't work either.
The Checkbox works fine.
************** I have additional information *********************
I've pulled and build the ObjectListView source, so I could step through it.
I put a breakpoint in the OLV StartCellEdit method and it gets called and appears to setup and select the control appropriately. It just never appears...
As I noted in the comments on the answer below, I've got this control on a tabbed dialog, and if I switch to another tab, then back, the control works fine.
What am I missing?
I've used ObjectListView before, and here is what I had to do:
Handle the CellEditStarting event. This event is raised when the cell goes into edit mode. Since OLV doesn't really have built-in editors, you have to make your own. Then handle the CellEditFinishing event to validate the data before putting it back into your model.
So first, handling the CellEditStarting event:
private void objlv_CellEditStarting(object sender, CellEditEventArgs e)
{
//e.Column.AspectName gives the model column name of the editing column
if (e.Column.AspectName == "DoubleValue")
{
NumericUpDown nud = new NumericUpDown();
nud.MinValue = 0.0;
nud.MaxValue = 1000.0;
nud.Value = (double)e.Value;
e.Control = nud;
}
}
This creates your editing control. If you want to make sure the size is right, you can set the size of the control (in this case a NumericUpDown) to the cell bounds using e.CellBounds from the event object.
This will show the editor when you click in the cell. Then you can handle the editor finished event to validate the data:
private void objlv_CellEditFinishing(object sender, CellEditEventArgs e)
{
if (e.Column.AspectName == "DoubleValue")
{
//Here you can verify data, if the data is wrong, call
if ((double)e.NewValue > 10000.0)
e.Cancel = true;
}
}
I don't think handling it is required, but its good practice to validate data from the user.
The editing control in the CellEditStarting event can be any control, even a user defined one. I've used a lot of user defined controls (like textboxes with browse buttons) in the cell editor.
[Edit]
I uploaded an example here dropbox link that seems to work. Might not be in the exact view as needed, but seems to do the job.
For anyone else with this problem. I had it specifically when trying to edit a 'null' value in a decimal? on the OLV on a tab page. Solution for me was to set UseCustomSelectionColors to 'False'. I didn't look elsewhere to see if it was reported as a bug. Seems like a bug.

Extracting property value of image object

I have an image that I want to add a dropshadow effect dynamically when ever the mouse is over the image.
`<Image Name="image1" Stretch="Fill" Source="/Start;component/Images/100px-The_alliance_logo.jpg" MouseEnter="MouseOver" />`
I want to extract the value of Name whenever mouse is hovered over the image. I have coded the following in C#.
private void MouseOver(object sender, MouseEventArgs e)
{
object ObjectName = new object();
ObjectName = Convert.ToString(sender.GetType().GetProperty("Name").GetValue(sender, null));
String Obj = (String)ObjectName;
Obj.Effect = shade(Obj);
}
I am getting an error in Obj.Effect = shade(Obj);, that says : "'string' does not contain a definition for 'Effect' and no extension method 'Effect' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)"
How can I solve it ?
If you insist on doing code-behind, which is not how WPF is supposed to be programmed, then you will need to do this:
private void MouseOver(object sender, MouseEventArgs e)
{
var image = sender as Image;
if(image != null)
{
image.Effect = yourShadowEffect;
}
}
However, there is a good example of how to do it the WPF way here, I strongly suggest you take a look, because the code behind approach will lead you nowhere.
Edit:
In this example, yourShadowEffect is supposed to be a variable of type System.Windows.Media.Effects.Effect, in your case a DropShadowEffect. You can create a new one by calling the constructor:
yourShadowEffect = new DropShadowEffect();
You should not do this in your MouseEventHandler, but rather have this as a class variable, to save resources and not create a new one every time the user hovers it's mouse over the image.

How to change icon and text to a ApplicationBarIconButton?

I want to make my click event change an ApplicationBarIconButton. My ApplicationBarIconButton looks like this:
<shell:ApplicationBarIconButton x:Name="driveAction" Click="drive_click" IconUri="/img/car.png" Text="kör" />
I want the IconUri to change from /img/car.png to ex. /img/car-stop.png and the text value from kör to passagera. I tried the function below, but it only causes my app to shut down.
private void drive_click(object sender, EventArgs e)
{
this.driveAction.Text = "passagera";
this.driveAction.Source = "/img/car-stop.png";
}
What is wrong? Why doesn't this work?
The default ApplicationBar requires you to access the buttons through the ApplicationBar object. To accomplish this you must know the index of the button that you want to change
private const int DriveButtonIndex = 0;
private void drive_click(object sender, EventArgs e)
{
var button = (ApplicationBarIconButton)ApplicationBar.Buttons[DriveButtonIndex];
button.IconUri = new Uri("/img/car-stop.png", UriKind.Relative);
button.Text = "passagera";
}
There are a few custom ApplicationBars that allow you to name your buttons. But I've found that the above solution always works for me.
Get it directly from the application bar list -
ApplicationBar.Buttons[0].Text = "passagera";
ApplicationBar.Buttons[0].Source = "/img/car-stop.png";
You could also query the list of buttons for a specific icon as a more tenable long-term solution, but if you only have one button and that's not going to change, this works.
Because the ApplicationBarIconButton is actually a native control and not a true XAML object you cannot refer to it by name.
You can refer to it by index if create in XAML. Alternatively you could create it in code and then you can maintain a named reference you can use.

Categories

Resources