Updating a textbox in main GUI from a thread (created on button click) in cli - c++-cli

I am very new with C++/CLI, which is based on c#. I have a main GUI form.
public ref class MyForm : public System::Windows::Forms::Form
On clicking a button in the from I am creating a thread using CreateThread. Code as following:
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e)
{
HANDLE h1;
h1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread1,0, 0, &threadID1);
}
Now my problem is I need to update a TextBox in Myform from the thread. Can anybody please tell me how to do it in cli?

It is safe to use .NET Thread^ in this scenario. Inside your thread1 method use Control::BeginInvoke or Control::Invoke like #AlexF mentioned. Here you have an example from C#.

Related

Helix Toolkit How to use MouseWheelEventHandler to run specific methods in other classes

I would like to run particular methods of a custom Camera class whenever the user zooms in or out in the helix toolkit view that my program is running inside of.
A key feature of this functionality is getting the mouseargs from the event so I can adjust the camera in a way that is proportional to the number of scroll ticks.
I began to try this:
public event PropertyChangedEventHandler PropertyChanged;
public virtual void onMouseWheeled(MouseDevice Mouse, int time,
MouseWheelEventArgs e) {
MouseWheel?.Invoke(this, new MouseWheelEventArgs(Mouse, time,
e.Delta)); }
//This next line goes in a MainWindow_Loaded method that gets called in the
//MainWindowConstructor
void MainWindow_Loaded(object sender, RoutedEventArgs e) {
view1.MouseWheel += new MouseWheelEventHandler(onMouseWheeled(Cursor,
Time.simTime, view1.MouseWheeledEventArgs)); }
but was having a lot of trouble figuring out how to pass a MouseWheelEventArgs object into the onMouseWheeled method when I'm trying to add the onMouseWheeled method to the MouseWheelEventHandler. Assuming nothing is fundamentally wrong with that sentence, which is nothing more than wishful thinking, The last thing I am trying to figure out is how to get mouse wheel event args so that I can pass it into a method.
Then I tried this:
public event MouseWheelEventHandler MouseWheel;
public virtual void onMouseWheeled(object sender, MouseWheelEventArgs e)
{
Console.WriteLine(e.Delta);
}
//In Main Window Loaded method...
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
view1.MouseWheel += onMouseWheeled;
}
But I get no output when i scroll the wheel. I assumed this might actually work because view1 is the helix window I'm attaching all of my objects to, as children of view1.
Basically my main questions are:
What does invoke actually do? I only have this running to try to see if its working because onPropertyChanged methods I always use run the Invoke command like so. I'm actually not sure where I'm going with this.
How does the handler work?
How do the event args get called out so that I can use them and pass them as objects to other methods?
Thank you for your time. And Thank you twice for any and all pointers and advice you may give me.
Try to use preview mouse wheel event

Enable - Disable a button while threading vsto for ms word

I'am very new to threading and quite unclear as to why this is happening in my code, when I click on a button that verifies hyperlinks in my document, I start a new thread that does the verification once it starts I want to disable the ribbon button and enable it again after thread finished but this is not happening and I dont know what is the mistake .Here is what I have tried so far:
public class Alpha :Ribbon1
{
// This method that will be called when the thread is started
public void Beta()
{
foreach() { //do something } after this loop ,enable the button again
button.enable=true //not applying
} }
private void button_Click(object sender, RibbonControlEventArgs e)
{
Alpha oAlpha = new Alpha();
// Create the thread object, passing in the Alpha.Beta method
Thread oThread = new Thread(new ThreadStart(oAlpha.Beta));
// MessageBox.Show("Please wait till the document is checked for invalid links");
// Start the thread
oThread.Start();
button7.Label = "Pls wait";
button7.Enabled = false;
}
Ribbon needs to be rendered again after enable/disable for change to take effect, you can do this by calling IRibbonUI.Invalidate()

Invoking Message dialog from Settings Flyout causes Message Dialog to flicker

I'm trying to invoke messagedialog from setting flyout for my Windows 8 Metro app but it's causing the message dialog to flicker. Below is the code.
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
SettingsPane.GetForCurrentView().CommandsRequested+=settings_CommandsRequested;
}
private void Settings_CommandsRequested(SettingsPane sender, SetttingsPaneCommandsRequestedEventArgs args)
{
SettingsCommand cmd = new SettingsCommand("test","test1232",new UICommandInvokedHandler(CreateDialog));
args.Request.ApplicationCommands.Add(cmd);
}
private void CreateDialog(IUICommand command)
{
if (ReferenceEquals(command.Id, "cmd"))
{
MessageDialog md = new MessageDialog("Hi");
md.ShowAsync();
}
}
I've contacted official microsoft dev-support and their response was:
"MessageDialog is not recommended within the SettingsFlyout".
So in case you want to implement something simillar to support user's decision from the SettingsPane, you should either:
1) Enable toggling feature in the Flyout.
2) Desiging the SettingsFlyout so it lets the user make decision (for example in Logout cases, add Yes/no buttons inside the settingsFlyout) - Thats the option I chose.

Is there a PowerPoint equivalent of Word's ToggleRibbon? Or another way to accomplish the same thing?

[In Office 2007+, you can cause the ribbon to be minimized so that only the tab names are shown, which makes it look a bit like a menu bar. The full ribbon is then only shown when you click on a tab. This ribbon state is what I'm trying to control.]
In the Word 2007+ object model, there's a ToggleRibbon method on the Window object, that minimizes (or not) the ribbon for that window.
I'm looking for an equivalent method in PowerPoint 2007+, and I can't find one. Is there such a thing, and if not is there another way to achieve it? Apart from using SendKeys, that is - don't go there.
i have implemented in c# with word addins .
you can take help from below link:
http://msdn.microsoft.com/en-us/library/bb608590.aspx
I have added a button in the ribbon(visual).
in the button click, i have added following code.
private void button1_Click(object sender, RibbonControlEventArgs e)
{
Globals.ThisAddIn.Toogle();
}
i have added these code in the ThisAddIn.cs:
public void Toogle()
{
Word.Window obj = Application.ActiveWindow;
obj.ToggleRibbon();
}
sorry, Window.ToggleRibbon() is present only in word. even it is not present in excel or work. so achieve this, do the following code :
private void button1_Click(object sender, RibbonControlEventArgs e)
{
SendKeys.SendWait("^{F1}");
}
Hope , this will work for you.

How to execute a binding for a metro control

I want to write the contents of a per occasion active TextBox back to the bound property of the ViewModel when the user presses the key combination for save (Ctrl-S).
My Problem with it is, that I'm not able to trigger the execution of the binding so that the bound Text-Property reflects the contents of the TextBox.
-There seems to be no GetBinding-method. Therefore I can not get the Binding and execute it manualy.
-There is no Validate-method such as in WinForms which executes the Binding
-Giving focus to another control from within KeyDown seems not to work, the binding does not execute
How can I achieve this?
Take a look at Aaron's discussion about this in his WiredPrarie blog post : http://www.wiredprairie.us/blog/index.php/archives/1701
I think I understand your question better now. One way around this would be to use a sub-classed textbox with a new property like this from here:
public class BindableTextBox : TextBox
{
public string BindableText
{
get { return (string)GetValue(BindableTextProperty); }
set { SetValue(BindableTextProperty, value); }
}
// Using a DependencyProperty as the backing store for BindableText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty BindableTextProperty =
DependencyProperty.Register("BindableText", typeof(string), typeof(BindableTextBox), new PropertyMetadata("", OnBindableTextChanged));
private static void OnBindableTextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs eventArgs)
{
((BindableTextBox)sender).OnBindableTextChanged((string)eventArgs.OldValue, (string)eventArgs.NewValue);
}
public BindableTextBox()
{
TextChanged += BindableTextBox_TextChanged;
}
private void OnBindableTextChanged(string oldValue, string newValue)
{
Text = newValue ? ? string.Empty; // null is not allowed as value!
}
private void BindableTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
BindableText = Text;
}
}
Then bind to the BindableText property.
Solution for command-instances
Here a solution I have found which is relatively leightweight, but also a bit "hackish":
btn.Focus(Windows.UI.Xaml.FocusState.Programmatic);
Dispatcher.ProcessEvent(CoreProcessEventsOption.ProcessAllIfPresent);
btn.Command.Execute(null);
First I give the focus to another control (In my case the button which has the bound command). Then I give the system time to execute the bindings and in the end I raise the command which is bound to the button.
Solution without bound commands
Give the Focus to another control and call the Dispatcher.ProcessEvent(...).
anotherControl.Focus(Windows.UI.Xaml.FocusState.Programmatic);
Dispatcher.ProcessEvent(CoreProcessEventsOption.ProcessAllIfPresent);
// Do your action here, the bound Text-property (or every other bound property) is now ready, binding has been executed
Please see also the solution of BStateham.
It's another way to solve the problem