Extracting property value of image object - c#

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.

Related

How does tag and sender work in c# cant find a fix to my code

my code below gets an error that says 'object' doesnt contain a definition for 'Tag'. How would i define tag to get this to work.
Im also getting another error saying there is no arguments given that correspond to formal parameter 'sender' and im not sure what this means or how to fix it an have tried googling it but got no where. Any help would be apprieciated.
private void AddNewAppointment(object sender, EventArgs e)
{
frmManage frmManage = new frmManage();
frmManage.dtpDate.Value = new DateTime(currentDate.Year, currentDate.Month,(FlowLayoutPanel)sender.Tag);
frmManage.ShowDialog();
DisplayCurrentDate();
}
You need to cast the sender to the expected object type like a label or a texbox or a panel...
From your code it seems there are missing parenthesis:
((FlowLayoutPanel)sender).Tag
Also you need to cast the Tag that is object:
(int)((FlowLayoutPanel)sender).Tag
Also you should write a thing like that to have a code more robust:
var panel = sender as FlowLayoutPanel;
if ( panel == null ) throw new Exception("...");
frmManage.dtpDate.Value = new DateTime(currentDate.Year, currentDate.Month, (int)panel.Tag);

Access Gtk# (GtkSharp) Label child of a Button

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];
}
}

Getting specific data from an object C#

I'm busy with a UWP app that uses a gridview as a category page. I've put a OnClick listener for when one of the the grid items is clicked, and it works. The result is an object which contains the imageURL and imageText.
This is where the problem starts, I need to access the object and retrieve the imagetext, I've tried converting it using various methods but nothing seems to want to work.
Here is the output of the clicked item, which is correct.
This is currently my latest attempt, which simply passes "an object of this type cannot be converted"
private void CategoryItem_Click(object sender, ItemClickEventArgs e)
{
object output = e.ClickedItem;
//string[] arr = ((IEnumerable)e.ClickedItem).Cast<object>()
//.Select(x => x.ToString())
//.ToArray();
}
You can use the as operator to cast the object to a Wiin.MainPage.MyImage:
private void CategoryItem_Click(object sender, ItemClickEventArgs e)
{
MyImage output = e.ClickedItem as MyImage;
if (output == null) return; // the clicked item was not of the expected type
string text = output.ImageText;
string url = output.ImageUrl;
}
Your debugger shows that the object in e.ClickedItem is a MyImage instance, so the cast should work.
In case the MyImage type is a value type (a struct), the as operator won't work. You'd need to cast directly then:
MyImage output = (MyImage)e.ClickedItem;
But this cast will throw an InvalidCastException if e.ClickedItem contains an instance of a different type. You might check the type before casting using is:
if (!e.ClickedItem is MyImage) return;

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

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