Prevent combobox from being closed - c++

How to prevent combobox from being closed (when user will try to select some item, the combobox will not close): https://learn.microsoft.com/en-us/uwp/api/Windows.UI.Xaml.Controls.ComboBox?view=winrt-19041

You can subscribe the DropDownClosed event, when the drop-down list closes, this event will be triggered, you can set the IsDropDownOpen as true to open the drop-down list. In addition, you can declare a property to judge whether the user selects item to cause this event to be triggered. For example:
.xaml:
<ComboBox x:Name="MyComboBox" DropDownClosed="MyComboBox_DropDownClosed">
<ComboBoxItem Tapped="ComboBoxItem_Tapped">123</ComboBoxItem>
</ComboBox>
.h:
private:
bool IsSelected;
.cpp:
void AppCX::MainPage::MyComboBox_DropDownClosed(Platform::Object^ sender, Platform::Object^ e)
{
if (IsSelected == true) {
MyComboBox->IsDropDownOpen = true;
}
IsSelected = false;
}
void AppCX::MainPage::ComboBoxItem_Tapped(Platform::Object^ sender, Windows::UI::Xaml::Input::TappedRoutedEventArgs^ e)
{
IsSelected = true;
}

Related

Combox first row not selectable

How to make first row of combobox not-selectable? (https://learn.microsoft.com/en-us/uwp/api/Windows.UI.Xaml.Controls.ComboBox?view=winrt-19041)
Combox first row not selectable
You could detect DropDownOpened event and find the fist item with ContainerFromIndex then disable it like the following. Because Combobox dropdown is lazy load, so we need add the task delay in DropDownOpened event.
private async void MyCb_DropDownOpened(object sender, object e)
{
await Task.Delay(100);
var item = MyCb.ContainerFromIndex(0) as ComboBoxItem;
if (item != null)
{
item.IsEnabled = false;
}
}

Get certain property of a QLineEdit via eventFilter to clipboard

I'm attempting to create a QLineEdit element whose text will be automatically copied to the clipboard when clicked.
I've created the following eventFilter to capture the click event and installed it on the applicable elements:
bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
if(event->type() == QEvent::MouseButtonPress)
{
qDebug("TEST");
return true;
}
else
{
return false;
}
}
What would be the best way from here to gather the data I need from the object to pass to the clipboard function?
Use the QClipboard class. You can get your application's clipboard using qApp->clipboard() and then set the text from the QLineEdit:
bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
if(event->type() == QEvent::MouseButtonPress)
{
auto watched_as_lineEdit = qobject_cast<QLineEdit*>(watched);
if (watched_as_lineEdit != nullptr) {
qApp->clipboard()->setText(watched_as_lineEdit->text());
return true;
}
}
return QMainWindow::eventFilter(watched, event); // change for actual parent class if different from QMainWindow
}

How do I access the parent item from a custom field in the Sitecore Content Editor?

Normally, when I want to access the current Item in Sitecore, I go through Sitecore.Context.Item. This works well when creating a tool which will be used by the end user, but not for a tool which is consumed by the Sitecore admins. If I want something to show up as a custom field in the Content Editor itself, the Context.Item is a reference to the Content Editor and not to the node which is selected in the editor.
For the most part I can get around this by using the ItemID property, but if I have event dispatchers on the field, those will no longer have access to the ItemID. Eg:
protected override void OnPreRender(EventArgs e)
{
if (IsEvent)
{
// ItemID is defined here!
Button live = FindControl(GetID(LiveButton)) as Button;
if (live != null)
{
live.ServerProperties["Click"] = string.Format("{0}.LiveClicked", ID);
}
}
}
public void LiveClicked()
{
// ItemID is blank here!
DoSomething();
}
How do I gain access to ItemID in my listener (like LiveClicked above)?
The way I solved it was through something called ServerProperties and I called the equivalent to this function in every listener.
private void SyncID()
{
var live = FindControl(GetID(LiveButton)) as Button;
if (live != null)
{
if(string.IsNullOrEmpty(ItemID))
{
ItemID = live.ServerProperties["owner_id"].ToString();
}
live.ServerProperties["owner_id"] = ItemID;
}
}

How to refresh item in a popup menu?

i have a popup menu (that comes out when the user uses right click on specified elements), wich items are readed from a list.
I want that when an item is selected, that item is disabled in the popupMenu (then if some action happen it will return enabled).
I have implemented the popupMenu, but i cannot implement this enable/disable JMenuItem element. Anyone can help me? Thanks
class PopupTriggerListener extends MouseAdapter {
public void mousePressed(MouseEvent ev) {
if (ev.isPopupTrigger()) {
menu.show(ev.getComponent(), ev.getX(), ev.getY());
x = ev.getX();
y = ev.getY();
}
}
public void mouseReleased(MouseEvent ev) {
if (ev.isPopupTrigger()) {
menu.show(ev.getComponent(), ev.getX(), ev.getY());
x = ev.getX();
y = ev.getY();
}
}
public void mouseClicked(MouseEvent ev) {
}
}
}
JLabel label = new MyLabel("right-click");
public Test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuItem item = new JMenuItem("Test1");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Menu item Test1");
JLabel newLabel = new JLabel("test");
label.add(newLabel);
newLabel.setBounds(x, y, 40, 10);
}
});
menu.add(item);
item = new JMenuItem("Test2");
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Menu item Test2");
}
});
menu.add(item);
getContentPane().add(label);
pack();
setSize(300, 100);
}
public static void main(String[] args) {
new Test().setVisible(true);
}
The way this is mostly done is using Actions. Actions are extensions of the ActionListener interface. You can set the Action of, for example, a JMenuItem and in the Action you can set enabled to false. This will automatically disable the JMenuItem. Alternately you can enable it by setting enabled to true on the Action.
Here is the Action API #Oracle: Action API JAVA
And here is a discourse on how to use Actions: How to use Actions JAVA

Toggle a button's text from "On" to "Off" when clicked (Visual C++)

I was given a small task to simply make a button's text toggle "On" and "Off" when you clicked on it - it already starts with the text "Off" as it hasn't been pressed yet, but when you click it changes to "On". On each alternating click afterwards, the button's text would ideally keep changing from "On" to "Off". In my case I thought a simple boolean variable would be the solution as On and Off can be treated as True or False, but not to be...
Anyway here's the code for the button's handler I've got so far:
private: System::Void toggleButtonText_Click(System::Object^ sender, System::EventArgs^ e)
{
static bool isOn = true;
if(isOn == false)
{
toggleButtonText->Text = "Off";
}
else
{
toggleButtonText->Text = "On";
}
}
As you can see, the name of the button is "toggleButtonText". In the InitializeComponent(void) method, this line enables the default text to "Off":
this->toggleButtonText->Text = L"On";
Looking at the rest of my tasks, getting this right will give me enough clues to attempt them on my own instead of spending ages on endless Google searches.
You need t toggle the flag each time you click the button. You can also greatly reduce the size of your code if you use the ?: operator:
static bool isOn = true;
toggleButtonText->Text = isOn ? "On" : "Off";
isOn = !isOn;
You have to update your variable state after toggling the text
static bool isOn = true;
if(isOn == false)
{
toggleButtonText->Text = "Off";
}
else
{
toggleButtonText->Text = "On";
}
isOn = !isOn; //toggle the flag too
Using static variable here is an extremely crappy solution. For example, if you change button's state programmatically the static variable will be out of sync with the actual press state.
Why not get toggle state from the button itself? Here's anwser how to add actual toggle button in the Windows Forms. After that you can modify the method the following way:
System::Void toggleButtonText_Click(System::Object^ sender, System::EventArgs^ e)
{
CheckBox^ button = (CheckBox^)sender;
if (button->Checked)
{
button->Text = "On";
}
else
{
button->Text = "Off";
}
}