How to get the Content of a XAML Button? - winrt-xaml

How do I get the Content of a XAML Button?
Visual Studio Community 15.9.4, with the C++/WinRT extensions installed.
I can set the Button's Content from the click handler, but cannot get the current Content.
Button definition from MainPage.xaml:
<Button x:Name="myButton" Click="ClickHandler">Click Me</Button>
Click handler definition from MainPage.cpp:
void MainPage::ClickHandler(IInspectable const& sender, RoutedEventArgs const& args)
{
myButton().Content(box_value(L"Clicked"));
}
I have also found that this code works to set the Content:
void MainPage::ClickHandler(IInspectable const& sender, RoutedEventArgs const& args)
{
Button sendButton = winrt::unbox_value<Button>(sender);
sendButton.Content(box_value(L"Clicked"));
}
Code I have tried to get the Content just does not compile.

In posting the original question, a thought was triggered, which I tried and which worked. The following was added to the MainPage.cpp click handler:
IInspectable sendButtonContent = sendButton.Content();
hstring sendButtonString = unbox_value<hstring>(sendButtonContent);
When the code is paused, after the Button is clicked, the value of sendButtonString is "Clicked".

Related

close a ContentDialog after LostFocus on it and click on the MainPage Interface

I have used a ContentDialog to create a PopUp ContentDialog like the one in the Groove App:
the problem is that,I can't close it when I click on any part of the MainPage Interface and loose focus,I have tried like this,but it doesn't work :(,it closes only after I send data from the ContentDialog
MainPage.xaml:
private void MenuButton_Click(object sender, RoutedEventArgs e)
{
AddFavoris formTask = new AddFavoris();
t = formTask.ShowAsync();
string f = formTask.Opgave;
formTask.LostFocus += new RoutedEventHandler(formTask_LostFocus);
}
private void formTask_LostFocus(object sender, RoutedEventArgs e)
{
t.Cancel();
}
so,is there any solutions can I use to have a ContrentDialog that will be closed after a Click on the MainPage interface and lost focus on it
thanks for help
Use Flyout instead of using a ContentDialog. Flyouts can be dismissed by tapping or clicking somewhere outside the flyout.
For more information: Guidelines for flyouts, Guidelines for dialog controls

FolderBrowserDialog with 'Default' button C#

I have tried this solution and it works beautifully FolderBrowserDialogEx
but now I have to add another button which will be placed next to editbox and upon clicking will show a particular folder always. I have tried this so far.
In the constructor:
btnDefault = new Button();
btnDefault.Text = "Default";
btnDefault.Enabled = true;
btnDefault.Click +=new EventHandler(btnDefault_Click);
on click
private void btnDefault_Click(object sender, EventArgs e)
{
_selectedPath = #"C:\ProgramData";
}
How do I add new Button? Full code is available HERE
Thank you for any help.

How to implement double click to a button in C#

how to implement double click event for a button in C# window application
strong text
Add an event handler
btn.doubleClick += btn_doubleClick;
Then, define
private void btn_doubleClick ( object sender, EventArgs e )
{
// do stuff
}

Click event for static text

I have a static text named IDC_STATIC for which I made a click event, but it is not getting called when it's clicked.
ON_BN_CLICKED(IDC_STATIC, ontest) //(begin message map)
void test::ontest(my method)
{
MessageBox("success");
}
afx_msg void test(); //(header file)
What is wrong with the code?
The solution is to go to the static text control's properties and enable Notify.

Fire parent click event when child is clicked in windows form user control

I have a user control
public partial class ButtonControl : UserControl
which has two controls of label and picturebox
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.text = new System.Windows.Forms.Label();
I have used this control in a windows form
this.appointmentButton = new DentalSoft.UI.Controls.ButtonControl();
created an event
this.appointmentButton.Click += new System.EventHandler(this.appointmentButton_Click);
but the problem is, if i click on the image or the label inside of the control, the click event doesn't fire.
I want to fire this click event no matter where the user clicks inside of the control. Is it possible?
Yes it is a simple matter. When you click on the child controls they receive the click event and the user control does not. So you can subscribe to the child control click events and when they occur simply raise the usercontrol click event and it will appear to click no matter where the mouse is positioned.
Just double click on the picturebox and the label to create the click event handlers then add a line of code to call the parent usercontrol OnClick method.
private void text_Click(object sender, EventArgs e)
{
this.OnClick(new EventArgs());
}
private void pictureBox1_Click(object sender, EventArgs e)
{
this.OnClick(new EventArgs());
}
I solved this problem the following way. I wanted to changes the background images of the parent PictureBox when the child Label was clicked. I played around some and found I could call the event handler in the following way.
private void Label_Click(object sender, EventArgs e)
{
Label label = (Label)sender;
Box_Click(label.Parent, e);
}
private void Box_Click(object sender, EventArgs e)
{
//
PictureBox box = sender as PictureBox;
//
if (isMine[int.Parse(box.Name)])
{
box.Image = Image.FromFile(#"..\..\images\BoxRedX.jpg");
MessageBox.Show("Game Over");
}
else
{
box.Image = Image.FromFile(#"..\..\images\BoxGray.jpg");
}
}
As you can see I grab the Child and using it to reference its parent and send it to the parents click event. This is the code I used to work in my project. The important method is the first on listed. The second is there for clarity to see how it passed and worked.

Resources