A non-static member reference must be relative to a specific object - winforms

I'm trying to get the text in my textbox tb_key to write to my std::string Key Variable by doing this:
std::string Key = TRIPRECOILWARE::LoginForm::tb_key->Text;
I get an error saying :
A non-static member reference must be relative to a specific
object
I tried to search but I couldn't find anything really that fixed it for me.
Minimal Reproducible Example:
LoginForm.h
namespace TRIPRECOILWARE {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
private: System::Void tb_key_TextChanged(System::Object^ sender, System::EventArgs^ e)
{
}
}
LoginForm.cpp
std::string Key = TRIPRECOILWARE::LoginForm::tb_key->Text;
I'm trying to use this in LoginForm.h
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
if (Authenticate(StringToChar(Key), (StringToChar(hwid)))) // Authenticates key & hwid
{
this->Hide();
Software^ soft = gcnew Software();
soft->Show();
}
Basically, I want to get Key from Textbox called tb_key and write
it to my Key variable defined above. Then use that key to
authenticate and perform code

Your real problem is a duplicate of How to turn System::String^ into std::string?
Corrected code:
#include <msclr\marshal.h>
#include <msclr\marshal_cppstd.h>
using namespace System;
using namespace msclr::interop;
void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
std::string Key = marshal_as<std::string>(tb_key->Text);
if (Authenticate(Key.c_str(), hwid.c_str())) // Authenticates key & hwid
{
Hide();
Software^ soft = gcnew Software();
soft->Show();
}
}

Related

Accessing WinForms member properties via delegates

I was somewhat surprised that can't easily access the properties of the WinForms class with functions you add as bublic, years ago with Borland VCL this wasn't an issue. Apparently 'delegates' is said to be the right keyword ... and I've been struggling with this for days now and can't get it to work. Is my approach beyond or is it just that the plug is not plugged in? Via the event handler, the forms property is successfully changed. Now i'm asking for a hint to transfer this capability down to sub1()?
#include "form1.h"
#include <Windows.h>
using namespace System;
using namespace System::Windows::Forms;
void sub1(void);
public delegate void Deleg(String^ st);
[STAThread]
void Main() // array<System::String^> ^args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
App1::Form1 form;
Deleg^ Deleg1;
Application::Run(% form);
// Deleg1 += gcnew Deleg(form,&App1::Form1::newTxt); //?
}
void App1::Form1::button1_Click(System::Object^ s, System::EventArgs^ e)
{
String^ st = "ABC";
App1::Form1::newTxt(st);
Refresh();
sub1();
}
void sub1(void)
{
String^ s = "DEF";
// Deleg1(s);
}
I tried to copy some examples I found on the net, but apparently I don't hit the core.

Create & use a WPF page with C++/CLR

I want to use C++/CLR to create a UI for my app. I need C++/CLR because I deal with a lot of native win32 C++ code. So I tried to make my app with the following steps, but they don't work:
Make an empty project
Unicode charset and enable /clr
Add refrences to PresentationCore, PresentationFramework, System, and WindowsBase
Add the App.xaml, App.xaml.h, MainWindow.xaml, MainWindow.xaml.h
Add the InitalizeComponent code to MainWindow.xaml.h (that
s where i'm having trouble.)
This is my mainwindow.xaml.h code
#pragma once
using namespace System::Windows;
using namespace System;
namespace Project2
{
public ref class MainWindow : Window
{
public:
MainWindow()
{
System::Uri ^resourceLocater = gcnew System::Uri("component\MainWindow.xaml", System::UriKind::Relative);
#line 1 "..\..\MainWindow.xaml"
System::Windows::Application::LoadComponent(this, resourceLocater);
}
};
}
My entry point code:
#include <Windows.h>
#include "App.xaml.h"
#include "MainWindow.xaml.h"
using namespace System;
using namespace System::Windows;
[STAThreadAttribute]
int WINAPI WinMain(HINSTANCE a, HINSTANCE b, LPSTR c, int d)
{
auto win = gcnew Project2::MainWindow();
auto app = gcnew Project2::App();
app->Run(win);
return 0;
}
And here's my app.xaml.h code:
#pragma once
using namespace System::Windows;
namespace Project2
{
public ref class App : Application
{
public:
};
}
The MainWindow.xaml and App.xaml all contain valid markup.
I tried chaning the values for the System::Uri resoucelocator but nothings chaning. I just get System.IO.IOException: The mainwindow.xaml was not found
Any ideas?

render pdf from memorystream

I have searched for entire day for a solution, but somehow I couldn't find it.
What I wanted is to get data from database (binary file such as PDF) into MemoryStream (which I succeeded to do so), but problem is when I wish to preview that file into WebBrowser control.
so the code goes:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
namespace MemoryStreamTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
MemoryStream memory = new MemoryStream();
//byte[] temp;
private void Form1_Load(object sender, EventArgs e)
{
memory = GetBlobFile();
}
private MemoryStream GetBlobFile()
{
MemoryStream ms = new MemoryStream();
string SQL = "SELECT [SnapshotPDF] FROM [DBtest].[dbo].[SamplePDF] WHERE id = " + 21;
SqlConnection conn = new SqlConnection("Data Source=database;Initial Catalog=DBtest; User ID=test; Password=test;");
SqlCommand comm = new SqlCommand(SQL, conn);
conn.Open();
byte [] result = (byte[])comm.ExecuteScalar();
conn.Close();
if (result != null)
{
ms.Write(result, 0, result.Length);
ms.Position = 0;
}
return ms;
}
private void button1_Click(object sender, EventArgs e)
{
//webBrowser1.DocumentText = "application/pdf";
webBrowser1.DocumentStream = memory;
}
}
}
Actually webBrowser really output content from memory, but obviously from picture above it's rendered as text...
How can I force to render as PDF?
If it is not possible to use webBrowser control, is there any other controls that I can use in WinForms that would preview/render PDF from memory.
You have to implement an async pluggable protocol, e.g. IClassFactory, IInternetProtocol... Then you use CoInternetGetSession to register your protocol. When IE calls your implementation, you can serve your image data from memory/provide mime type.
It's a bit tedious, but doable. Look at IInternetProtocol and pluggable protocols documentation on MSDN.
Done, I found one little bug which I correct it. this is entire code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using Ghostscript.NET;
using Ghostscript.NET.Viewer;
namespace MemoryStreamTest
{
public partial class Form1 : Form
{
Stream stream;
byte[] result;
private GhostscriptViewer _viewer;
private GhostscriptVersionInfo _gsVersion = GhostscriptVersionInfo.GetLastInstalledVersion();
private Bitmap _pdfPage = null;
public Form1()
{
InitializeComponent();
pictureBox1.Width = 100;
pictureBox1.Height = 100;
_viewer = new GhostscriptViewer();
_viewer.DisplaySize += new GhostscriptViewerViewEventHandler(_viewer_DisplaySize);
_viewer.DisplayUpdate += new GhostscriptViewerViewEventHandler(_viewer_DisplayUpdate);
_viewer.DisplayPage += new GhostscriptViewerViewEventHandler(_viewer_DisplayPage);
}
private void Form1_Load(object sender, EventArgs e)
{
GetBlobFile();
}
private void GetBlobFile()
{
string SQL = "SELECT [SnapshotPDF] FROM [test].[dbo].[InvoiceAdded] WHERE id = " + 21;
SqlConnection conn = new SqlConnection("Data Source=test;Initial Catalog=test; User ID=test; Password=test;");
SqlCommand comm = new SqlCommand(SQL, conn);
conn.Open();
result = (byte[])comm.ExecuteScalar();
conn.Close();
stream = new MemoryStream(result);
}
private void button1_Click(object sender, EventArgs e)
{
ConvertToBitmap();
}
private void ConvertToBitmap()
{
_viewer.Open(stream, _gsVersion, true);
}
//DisplayPage
void _viewer_DisplayPage(object sender, GhostscriptViewerViewEventArgs e)
{
pictureBox1.Invalidate();
pictureBox1.Update();
}
//DisplaySize - dynamically
void _viewer_DisplaySize(object sender, GhostscriptViewerViewEventArgs e)
{
pictureBox1.Image = e.Image;
}
//DisplayUpdate - automatic update picture
void _viewer_DisplayUpdate(object sender, GhostscriptViewerViewEventArgs e)
{
pictureBox1.Invalidate();
pictureBox1.Update();
}
}
}
keep in mind that for this I needed to add .dll from Ghostscript.NET (a nice wrapper made by my patriot from Croatia), also I needed to install Ghostscript interpreter from GhostScript interpreter
And also many thanks to user: sm.abdullah
which actually point me to right direction.
Cheers

fstream and c_str() : c_str is not working

I have been recently working on a program and i tried to use System::String^ to use it for an ofstream file...
I always get an Error...
Note This is a Form so it is a Header File.
This is my Code...(one part)
System::String^ txtPath;
#pragma endregion
private: System::Void btn1_Click(System::Object^ sender, System::EventArgs^ e) {
using namespace std;
ofstream fout (txtPath->c_str());
}
private: System::Void txtBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) {
txtPath=(Convert::ToString(txtBox1->Text))+"\\weapon_deagle.txt";
}
The Error I get is :
Error 1 error C2039: 'c_str' : is not a member of 'System::String' c:\users\a\documents\visual studio 2010\projects\d1s1k formed\d1s1k formed\Form1.h
101

Passing Variables From File-to-File in C#

How would I use a variable declared in Program.cs and access it's value from Form1.cs?
I know how to do this in C, but I'm completely lost in Microsoft's little twist on C.
Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using LuaInterface;
namespace WindowsFormsApplication1
{
static class Program
{
public static Lua lua = null;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
lua = new Lua();
}
}
}
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LuaInterface;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
lua.DoString("print('hi')");
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = "";
}
}
}
Using your examples of Program.cs and Form1.cs and assuming these are the default names and that you have a Program class that instantiates a Form1 class and that you want to pass a parameter to the Form1 class, you can do the following:
Define a constructor for Form1 that takes this parameter and chain to the default constructor:
private Lua lua;
public Form1(Lua lua) : this()
{
this.lua = lua;
}
In your Program class when instantiating Form1, pass the parameter to it:
lua = new Lua();
Application.Run(new Form1(lua));
Note that I am using OOP terminology - objects and classes (not files).
Update:
Since you have declared your lua variable as a public static member of the Program class, you can access it anywhere in your program (assuming the namespaces have been declared appropriately) as follows:
Program.lua;
Though you would want to instantiate the static field before calling Application.Run.
In any way, this makes the object a public shared resource across all threads - making it virtually untestable and difficult to work with if you go multi-threaded.
You can also access the parameter with
Program.lua;
But Oded's way is cleaner.
And in Main, the object must be instantiated before Application.Run:
static void Main()
{
lua = new Lua();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
In Program.cs there is most probably a static class defined like this:
static class Program
{
...
}
If you declare the variable as public or internal, you will be able to access it from your form.
static class Program
{
public static int myVariable;
public static int MyProperty { get; set; }
}
In your form, you access the variable and the property with:
int i = Program.myVariable;
int j = Program.MyProperty;
In object-oriented programming, you would usually keep the variables private and only declare the properties as public.

Resources