Click event for static text - visual-c++

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.

Related

How to get the Content of a XAML Button?

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".

Visual c++ (CLR) Acces richtextbox from a source file

I have created a Form using c++ (CLR).
I have added a richtextbox and I need to make it visible in my source file in order to change the text from there.
So to sum it up:
I have a richtextbox in MyForm.h.I need to change the text of this richtextbox from Source.cpp.How do I do that?
Managed to fix it by doing so:
Creating a new function in the .cpp source file like this:
void TEST(System::Windows::Forms::RichTextBox ^ changin)
{
changin->Text = "TEST";
}
Than in the .h file
void TEST(System::Windows::Forms::RichTextBox ^ changin);
Here is when a button is pushed:
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
TEST(richTextBox4);
}
Where richTextBox4 is the one that should be modified from the .cpp file.
Pass your text through a public function in your form and there update the text.
In such way you save object encapsulation.
In your form add a method in the following manner:
In the form you have a private field:
private RichTextBox rtb;
rtb is reference to a user control containing the RichTextBox which expose the RichTextBox text property by overriding it:
public override string Text
{
get
{
return rtb.Text;
}
set
{
rtb.Text = value;
}
}
then by a public method in your form you can access the RichTextBox.
public void SetText()
{
rtb.Text = "test_text";
}
you can have a look on the same idea here:
http://www.codeproject.com/Articles/18178/A-Padded-Text-Box-Control
Update : have a look on the following example it emphasize my explanation above : http://www.codeproject.com/Articles/4544/Insert-Plain-Text-and-Images-into-RichTextBox-at-R
I hope it is clear enough.

How to close user control used in radgrid filtering?

I have a radgrid, for date column i have created a custom user control for filtering. I need to create a close button to close the user control. There are no close events which i can call. I don't want to make visibility collapsed. I started with something below:
public partial class DateFilterControl : UserControl, IFilteringControl
{
public event CloseEventHandler Close;
public delegate void CloseEventHandler();
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
It is throwing nullreference exception which is obvious to come. What code do i need to put to close the user control?
use Messaging Service. With this you can close the window in ViewModel so no need to give close function in Backend. Add Command property to Cancel button
<Button Content="Cancel" Command="{Binding CancelCommand}"/>
Now in the ViewModel add the RelayCommand property in that add
Messenger.Default.Send<bool>(true, typeof(XViewModel));
Now in the BackEnd of this userControl adds following in the constructor.
Messenger.Default.Register<bool>(this, typeof(ScheduleViewModel), (b) =>
{
if (b == true)
{
this.DialogResult = true;
}
});
Now u can close the Window...
This will surely helps u...

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.

MFC menu item checkbox behavior

I'm trying to add a menu item such that it acts like a check mark where the user can check/uncheck, and the other classes can see that menu item's check mark status. I received a suggestion of creating a class for the menu option (with a popup option), however, I can't create a class for the menu option when I'm in the resource layout editor in Visual Studio 2005. It would be great to hear suggestions on the easiest way to create menu items that can do what I have described.
You should use the CCmdUI::SetCheck function to add a checkbox to a menu item, via an ON_UPDATE_COMMAND_UI handler function, and the ON_COMMAND handler to change the state of the checkbox. This method works for both for your application's main menu and for any popup menus you might create.
Assuming you have an MDI or SDI MFC application, you should first decide where you want to add the handler functions, for example in the application, main frame, document, or view class. This depends on what the flag will be used for: if it controls application-wide behaviour, put it in the application class; if it controls view-specific behaviour, put it in your view class, etc.
(Also, I'd recommend leaving the menu item's Checked property in the resource editor set to False.)
Here's an example using a view class to control the checkbox state of the ID_MY_COMMAND menu item:
// MyView.h
class CMyView : public CView
{
private:
BOOL m_Flag;
afx_msg void OnMyCommand();
afx_msg void OnUpdateMyCommand(CCmdUI* pCmdUI);
DECLARE_MESSAGE_MAP()
};
// MyView.cpp
BEGIN_MESSAGE_MAP(CMyView, CView)
ON_COMMAND(ID_MY_COMMAND, OnMyCommand)
ON_UPDATE_COMMAND_UI(ID_MY_COMMAND, OnUpdateMyCommand)
END_MESSAGE_MAP()
void CMyView::OnMyCommand()
{
m_Flag = !m_Flag; // Toggle the flag
// Use the new flag value.
}
void CMyView::OnUpdateMyCommand(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck(m_Flag);
}
You should ensure the m_Flag member variable is initialised, for example, in the CMyView constructor or OnInitialUpdate function.
I hope this helps!
#ChrisN's approach doesn't quite work for MFC Dialog applications (the pCmdUI->SetCheck(m_Flag); has no effect). Here is a solution for Dialog apps:
// MyView.h
class CMyView : public CView
{
private:
BOOL m_Flag;
CMenu * m_menu;
virtual BOOL OnInitDialog();
afx_msg void OnMyCommand();
DECLARE_MESSAGE_MAP()
};
// MyView.cpp
BEGIN_MESSAGE_MAP(CMyView, CView)
ON_COMMAND(ID_MY_COMMAND, OnMyCommand)
END_MESSAGE_MAP()
BOOL CMyView::OnInitDialog()
{
m_menu = GetMenu();
}
void CMyView::OnMyCommand()
{
m_Flag = !m_Flag; // Toggle the flag
if (m_flag) {
m_menu->CheckMenuItem(ID_MENUITEM, MF_CHECKED | MF_BYCOMMAND);
} else {
m_menu->CheckMenuItem(ID_MENUITEM, MF_UNCHECKED | MF_BYCOMMAND);
}
}
References:
http://www.codeguru.com/forum/showthread.php?t=322261
I ended up retrieving the menu from the mainframe using GetMenu() method, and then used that menu object and ID numbers to call CheckMenuItem() with the right flags, as well as GetMenuState() function.

Resources