defining a global ofstream in windows form - winforms

I need to do sth like this:
#include <fstream>
...
ofstream myStream
private: System::Void start_Click(System::Object^ sender, System::EventArgs^ e) {
//open ofstream
}
private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
//write to ofstream
}
is this possible?
Thanks in advance.

The code snippet shows where you wanted to declare it, but not what the declaration looked like. Anyway, the issue is that you can't use unmanaged (C++ native) objects in managed (.NET CLR) code. You could possibly declare an ofstream* variable, but not an ofstream or ofstream&.
If you want to use ofstream rather than System.IO.StreamWriter, you'll have to create a class in a separate containing only hative code, and instantiate that class from within your .NET code. Here is a CodeProject article addressing this.

Related

How to override toolstrip renderer?

I am new to visual studio and c++, I have created a new CLR Empty Project and have started adding some controls. I notice this strange artefact on the right edge of the toolstrip and would like to remove it.
This question Why am I getting a vertical line on toolstrip? provides a solution in c# but I am having trouble trying to make it work in visual c++.
First i made new .cpp file called CustomerRenderer.cpp and the code looks like this:
using namespace System::Windows::Forms;
private ref class CustomRenderer : ToolStripRenderer
{
protected:
virtual void OnRenderToolStripBorder(ToolStripRenderEventArgs^ e) override
{
ToolStripRenderer::OnRenderToolStripBorder(e);
}
};
Then on my forms load I put this:
private: System::Void MainForm_Load(System::Object^ sender, System::EventArgs^ e) {
this->toolStrip1->Renderer = gcnew CustomRenderer();
}
And this just makes the toolstrip disappear completely rather than fix the edge artefact. I like the style of the gradient toolstrip so would rather not change to the system renderer.
How can I override the toolstrip renderer in a visual c++ project and remove the edge border artefact?
Thanks
Ok I solved by changing:
private ref class CustomRenderer : ToolStripRenderer
to
private ref class CustomRenderer : ToolStripProfessionalRenderer

Method binding - What if Method is in another Class/Namespace?

Let's say I got the following ComboBox in my XAML:
<ComboBox x:Name="cmbOinkOink" Loaded="cmbOinkOink_Loaded" />
And I have my cmbOinkOink_Loaded method deep in here:
namespace PiggyWPF.Classes.EventHandler
{
class ComboBoxEventHandler
{
public void cmbOinkOink_Loaded(object sender, RoutedEventArgs e)
{
// Do Stuff...
}
}
}
How am I going to tell XAML that the cmbOinkOink_Loaded is going to be found under PiggyWPF.Classes.EventHandler.ComboBoxEventHandler?
I am not sure there is a straight forward way to achieve this behavior from xaml.
but you can do this easily from code behind.
class Control
{
...
public void cmbOinkOink_Loaded(object sender, RoutedEventArgs e)
{
_handlerObject.DoStuff();
}
You will have to forward the method call one way or another, either via the code behind of that XAML that defines the ComboBox or by using e.g. the ExecuteCommandAction from Interactivity, which requires you to provide a command in you original class instead of just a method, ideally static or otherwise easily accessible so you can use x:Static or something similar in the action's XAML.
(Posted on behalf of the OP).
I guess my only way was to add the following code to my main class:
private void cmbOinkOink_Loaded(object sender, RoutedEventArgs e)
{
using (ComboBoxEventHandler cmbEvent = new ComboBoxEventHandler())
{
cmbEvent.cmbOinkOink_Loaded(ref sender, e);
}
}

Vc++, Use function from form1 in form2, by calling

I have been looking for help everywhere. So far havent found the help i need.
I hope you guys can guide or help me =)
I have a Form1, on form1 i have included in Form1.h the second forms file: Form2.h
So now i have created, which runs when a button is pressen:
| Form1.h |
Form2^ frmProSog = gcnew Form2();
frmProSog->ShowDialog();
Now Form2 is open and i use it to search for a string in a datagrid. When i have selected the result, I will click on a button in Form2 which I want to call a function in Form1 which will add the data in another datagrid which is in Form1.
I have tried to include Form1.h in Form2 but i get an error: cannot be called with the given argument list argument types are: (System::Object ^)
I hope that someone out there is able to help me. Tried many of the following suggestions on stackoverflow which reminds of this question but no luck.
(David Yaw's comment is correct, I write the following on the basis that the OP might need more explicit instructions. This answer is just pointing out usual C++ techniques, not something particularly specific to CLI/C++ or WinForms.)
It seems to me from your description that the body of your button-press event functions must have been in the respective header files. Furthermore, that #include "Form1.h" is in Form2.h, and vice-versa: this causing the circular dependency and associated pain.
The simple fix is to implement the event-handling methods in the Form1.cpp and Form2.cpp source files instead. Once the form designer creates the empty method...
public ref class Form 1 : System::Windows::Forms::Form
{
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
}
}
... then remove the implementation from the header.
public ref class Form 1 : System::Windows::Forms::Form
{
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) ;
}
Copy/Paste into the cpp file, and add the handler body
System::Void Form1::button1_Click(System::Object^ sender, System::EventArgs^ e) {
Form2^ frmProSog = gcnew Form2();
frmProSog->ShowDialog();
}
Remove the respective #include from the .h and put into the .cpp instead.
(If there is a member of Form1 which is a Form2^, forward declare Form2 in Form1.h)
The above will work because, on the basis of your question, there is no visual representation (i.e., in the form designer) of Form1 in Form2, or Form2 in Form1. In this case, you can have as much circular dependency as you like between forms/controls, as long as you forward declare in the *.h, and only "use" (access members, etc) circularly-dependent classes in source files. You can happily take handles (^) to Form2 in Form1.h with forward declaration, including Form2^ as member functions and method signatures.

It is possible to create "new MouseButtonEventArgs" in Silverlight?

I'm newbie in Silverlight and I need to activate MouseRightButtonDown(object sender, MouseButtonEventArgs e) from an another event handler in my application.
I have found, that in WPF it is possible to do somthing like this:
public void OnContextMenuOpened(object sender, RoutedEventArgs e) {
MouseButtonEventArgs args = new MouseButtonEventArgs(
InputManager.Current.PrimaryMouseDevice,
0,
MouseButton.Right);
MouseRightButtonDown(sender, args);
}
But I have in Silverlight neither InputManager-Class nor MouseButton-Class... It is generally possible to realise something like that?
I want to do it, because I try to select an DataGridRow(within an custom control) with help of right-mouse-button. Without context menu it is easily possible, but when I switch context menu on, then context menu opens and event will not fired...
My code snippet:
public override void OnApplyTemplate() {
DataGrid = (DataGrid)GetTemplateChild("DataGrid");
DataGrid.MouseRightButtonDown += DataGridMouseRightButtonDown;
ContextMenu = (ContextMenu)GetTemplateChild("ContextMenu");
ContextMenu.Opened += OnContextMenuOpened;
}
private void DataGridMouseRightButtonDown(object sender, MouseButtonEventArgs e) {
//My code to select an DataGridRow
}
public void OnContextMenuOpened(object sender, RoutedEventArgs e) {
//This event-handler now will be always activated if I do
//right-mouse-button-click
}
Thanks a lot for help!
The Results of my research has shown, that it is impossible in silverlight -.-

Hide cursor wpf WebBrowser control?

neither
<WebBrowser x:Name="wbMain" Cursor="None"></WebBrowser>
nor
*{cursor:none}
is working for me. I can't find any resources online telling me how to accomplish this. The use-case for this is an application that runs full screen meant for viewing only after the setup takes place.
Edit: I forgot to add that the css works as expected when viewing the website in the IE9 browser.
I don't know if this is a good or bad practice but you can add System.Windows.Forms reference
then
private void MouseEnter(object sender, MouseEventArgs e)
{
System.Windows.Forms.Cursor.Hide();
}
private void MouseLeave(object sender, MouseEventArgs e)
{
System.Windows.Forms.Cursor.Show();
}
use this code on mouseEnter form example in web-browser control
For those who dont like to add WinForms reference, try
[DllImport("user32.dll")]
static extern int ShowCursor(bool bShow);
and call ShowCursor(false) when needed.
In app.cs
protected override void OnStartup(StartupEventArgs e)
{
System.Windows.Forms.Cursor.Hide();
}

Resources