Drawing in Form_Load - forms

So, I have:
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
DrawBackground();
}
private: System::Void DrawBackground(){
Graphics^ g1=this->CreateGraphics();
SolidBrush^ p1 = gcnew SolidBrush(System::Drawing::Color::Gray);
g1->FillRectangle(p1,90,150,600,150);
}
This should load this rectangle when form loaded, but it doesn't.
Why ?? How to you write this correctly ?

You cannot draw in the Load event, the form isn't visible yet. Nor is it ever correct to use CreateGraphics(). Whatever you draw won't survive until the next redraw. Override the OnPaintBackground method instead. Like this:
protected:
virtual void OnPaintBackground(PaintEventArgs^ e) override {
__super::OnPaintBackground(e);
SolidBrush brush(System::Drawing::Color::Gray);
e->Graphics->FillRectangle(%brush,90,150,600,150);
}
Note the use of the stack semantics for brush (no hat), that ensures the brush is automatically disposed.

Related

Calling C# click event without actually clicking the button not working

I have below code written inside C# Form application where I am trying to get x,y and z co-ordinates from Leap Motion device.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//controller.EventContext = WindowsFormsSynchronizationContext.Current;
button1.Click += new EventHandler(button1_Click);
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button1_Click(object sender, EventArgs e)
{
// Initialize the Controller object which connects to the Leap motion service
// and captures the hand tracking data
Controller controller = new Controller();
//Get the most recent tracking data using the Frame object
Frame frame = controller.Frame();
for (int h = 0; h < frame.Hands.Count; h++)
{
// Initialize the Hand in the given frame
Hand leapHand = frame.Hands[h];
// Get the "Pointer" finger of current hand which refers to where a person is pointing
Finger leapFinger = leapHand.Fingers[1];
// Prepare a vector which will store the co-ordinate values of the tip of the pointer
Vector currentPosition = leapFinger.StabilizedTipPosition;
textBox1.Text = Convert.ToString(currentPosition.x);
textBox2.Text = Convert.ToString(currentPosition.y);
textBox3.Text = Convert.ToString(currentPosition.z);
}
}
}
However, I need to explicitly click the button1 to display.
Any idea what's wrong ?
Here is an overly simple example of one way to do it. I'm not saying it's the best way, but you should be able to modify this sample code with your code.
In this example, there is a simple "HelloWorld" method that is called when the form is initialized as well as whenever you press the button. Let me know if this gets you close to what you're after.
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.Click += new EventHandler(button1_Click);
HelloWorldTest();
}
private void button1_Click(object sender, EventArgs e)
{
HelloWorldTest();
}
private void HelloWorldTest()
{
MessageBox.Show("Hello World!");
}
}
}
For what it's worth, I didn't use your code because I don't have the Leap libraries installed and if there was a typo, I wouldn't be much help to resolve it. Nonetheless, hopefully it gets you going down the right path.

ILImageSCPlot doesn't draw image correct

In a Plotting Form I want draw an ILImageSCPlot:
private void ilPanel1_Load(object sender, EventArgs e)
{ var data = ILSpecialData.waterfallf(25, 100);
ilPanel1.Scene.Add(new ILPlotCube {
new ILImageSCPlot(data );
});
}
But the drawing is not complete. The upper blue/green range is missing.
Sometimes at refresh (resize form) I can see the blue/green for a short moment.

how to refresh a ILPanel by clicking a button...?

i am a Student and just startet programming and try out ILNumerics.
Here is my question:
How can i refresh or replot a IlPanal after changing colormap or userdata?
I want to refresh the Surfacegraph by clicking on a button. I guess the answer is very easy, but i tried a lot the last two days... i'm a noob ;)
For example:
private void ilPanel_3D_Load(object sender, EventArgs e)
{
var scene = new ILScene();
var pc = scene.Add(new ILPlotCube(twoDMode: false));
var sf = pc.Add(new ILSurface(Z));
ilPanel_3D.Scene = scene;
}
private void button1_Click(object sender, EventArgs e)
{
// dont know what to put in here...
}
thanks
For such interactive actions you will have to access the 'synchronized copy' which every driver internally maintains of the global scene. Since a scene may get displayed by many drivers at the same time, this internal copy only reflects all changes due to user interaction / modifications.
private void button1_Click(object sender, EventArgs e) {
ilPanel_3D.GetCurrentScene().First<ILSurface>().Colormap = Colormaps.Summer;
ilPanel_3D.Refresh();
}
PS: no need to wait for two days the next time ... ! ;)
#Edit2: Actually, the update works just fine on the global scene also. But in order to broadcast the change to the ilPanel1 driver, you will have to add a call to Configure() at the end of your modifications. Configure can be called once for all changed nodes on one of their common parent nodes. So, you might simply call Configure() on the scene:
private void button1_Click(object sender, EventArgs e) {
ilPanel_3D.Scene.First<ILSurface>().Colormap = Colormaps.Summer;
ilPanel_3D.Scene.Configure();
ilPanel_3D.Refresh();
}

Static and non-static method or global class object

I'm making a Windows Forms Application in VS2012 C++.
Situation just for example, real project is more complicated:
I have a Form that contains TextBox, Button and Timer.
Button just triggers the timer. Timer just calls function that increments some variable.
I need to display the function's variable that is incremented, in TextBox.
In Form1.h I add code:
public: void Timer_Function(); //function activated by timer Tick
void Set_Text(String ^str); //function to set TextBox Text
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
if (timer1->Enabled == false) timer1->Enabled = true;
else timer1->Enabled = false;
}
private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e)
{
Timer_Function();
}
In My_app.cpp code like this:
#include "stdafx.h"
#include "Form1.h"
#include "resource.h"
using namespace test_staticfunc;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Application::Run(gcnew Form1());
return 0;
}
void Form1::Timer_Function()
{
Timer_Func();
}
void Form1::Set_Text(String ^str)
{
textBox1->Text = str;
}
void Timer_Func()
{
static int I=0;
I++;
Form1::Set_Text(I.ToString());
}
Function Timer_Func() is specified in "resource.h" like this:
void Timer_Func();
I.e. I'm trying to display the current state of inner variable I of Timer_Func() by passing it to a Form1 public method Set_Text().
So. The error here is that Set_Text() is not a static method.
I tried to make it static, but got an error "ะก2227: The operand to the left of "->Text" is not a pointer to a class, structure, or union." How to get it right? In that case a static method is trying to implement a non-static method, right?
Or another way: to make an instance of Form1 - instead of
Application::Run(gcnew Form1());
insert code
Form1 ^My_form = gcnew Form1();
Application::Run(My_form);
And use Set_Text as non-static method for class instance My_form.
But My_form is available only in main()! I couldn't make My_form anywhere else. Is there way to make it global or something?
May be there are other ways to solve this problem?
Help, please! I've already searched several forums for answer but didn't find the answer. More precisely non of them suited.
P.S. Sorry for my bad english! ^_^

DragDrop event not raised

This is kind of a stupid question... I'm trying to drag and drop a picturebox onto a panel. I followed some exemples, but it doesn't work. The DragDrop event of the panel is never raised. I searched thi site for a solution andfound two topics over a year old, but their solutions did not work. I created a new project, with only this code :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
panel1.DragDrop +=new DragEventHandler(panel1_DragDrop);
panel1.DragOver +=new DragEventHandler(panel1_DragOver);
}
private void panel1_DragOver(object sender, DragEventArgs e)
{
Console.WriteLine("DragOver");
}
private void panel1_DragDrop(object sender, DragEventArgs e)
{
Console.WriteLine("DragDrop");
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
Console.WriteLine("Mouse");
pictureBox1.DoDragDrop(pictureBox1.Text, DragDropEffects.All);
}
}
I also set the AllowDrop of the panel and the form to true. DragOver and MouseDown are raised. Also, when I drag the picturebox, the cursor become a barred circled, like it was an operation that wasn't allowed. Is there a way that the cursor becomes the image in the picture box? I don't want the picturebox to move, only to add an item to the panel.
The problem is easy to solve.
You must just set in DragEnter appropriate Effect:
private void Form1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
After that DragDrop event is fired correctly.
Richard, the problem is that drag and drop is not as simple operation as you have coded here. Here you haven't started the drag movement which should start with code and you can read more about it here... http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dodragdrop(v=VS.90).aspx
If you just want to move the PictureBox... dragging picturebox inside winform on runtime
And finally Drag and Drop between Instances of the same Windows Forms Application
Hope this helps.