I have created class in which i have defined methods which i need to call in forms .I need to call this methods from class Test in many forms.Below is my code how i try, but without success.I dont see here where i'm wrong.
//class Test.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
namespace Restaurant
{
public class Test : Form
{
public void MouseMove(object sender, MouseEventArgs e)
{
TextBox txt = sender as TextBox;
foreach (TextBox text in this.Controls.OfType<TextBox>())
{
if (e.Button == MouseButtons.Left)
{
if (txt.Name == text.Name)
{
txt.Left = e.X + txt.Left - MouseDownLocation.X;
txt.Top = e.Y + txt.Top - MouseDownLocation.Y;
}
}
}
}
public void MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
}
}
}
// form in which i need to call methods from Test class
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
namespace Restaurant
{
public partial class Form1 : Test
{
public Form1()
{
InitializeComponent();
}
private void textBox_MouseMove(object sender, MouseEventArgs e)
{
Test b = new Test();
b.MouseMove1(sender,e);
}
private void textBox_MouseDown(object sender, MouseEventArgs e)
{
Test b = new Test();
b.MouseDown(sender,e);
}
}
}
Since Form1 inherits from Test, you should be able to change Form1 as follows:
private void textBox_MouseMove(object sender, MouseEventArgs e)
{
MouseMove1(sender,e);
}
This way you are calling the method on the form you are on, rather than a new form that you are instantiating and acting against in your code sample.
Related
I build a method that hold BackColor for a panel, if i call the method in a new method the color will not show
this is my code
''''''''''''''''''''''''''''''''''''''''''''''''''''
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace WindowsFormsApplication24
{
class theme
{
public static void dark()
{
Form1 f = new Form1();
Color c= f.panel1.BackColor = Color.Black;
}
}
}
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
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;
namespace WindowsFormsApplication24
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
theme.dark();
}
}
}
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
The new Form1() you instantiated is an instance of the Form1 class, but many Form1 instances can exist simultaneously. You want to pass the instance of Form1 you are trying to change. Try:
private void Form1_Load(object sender, EventArgs e)
{
theme.dark(this);
}
And:
public static void dark(Form1 f)
{
f.panel1.BackColor = Color.Black;
}
what I'm trying to do is to change the text of the "last" lost focussed TxtBox by using a button. I know about the LostFocus method, but what are the actual paramaters that we have to give to that fuction?
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.Net;
using System.IO;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// this.ActiveControl = textBox2;
}
private Control _focusedControl;
private void button1_Click(object sender, EventArgs e)
{
SubmitData();
}
private void SubmitData()
{
if (_focusedControl != null)
{
_focusedControl.Text = "hi";
}
}
private void TextBox2_GotFocus(object sender, EventArgs e)
{
MessageBox.Show("Got focus.");
_focusedControl = (Control)sender;
}
}
}
So, what is happening is, no messagebox is popping and the _focusedCOntrol varable doesn't contain the last focus.
I'm trying to create program which display video from IP Camera.
This is my 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 Emgu.CV; //
using Emgu.CV.CvEnum; // usual Emgu Cv imports
using Emgu.CV.Structure; //
using Emgu.CV.UI;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Runtime.InteropServices;
using Emgu.Util;
using System.Net;
namespace WindowsFormsApplication1
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
public Capture _capture;
public Mat imgOriginal;
private void imageBox2_Click(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
public void button1_Click(object sender, EventArgs e)
{
_capture = new Capture("http://192.168.1.148:8080/video");
_capture.ImageGrabbed += ProcessFrame;
_capture.Start();
}
public void ProcessFrame(object sender, EventArgs arg)
{
imgOriginal= _capture.QueryFrame();
ibOriginal.Image = imgOriginal;
}
}
}
It's getting stuck on this step (without expectation):
imgOriginal= _capture.QueryFrame();
Maybe i should you invoke method but i don't know how.
Im using Emgu 3.1.0 Link to Doc
I managed to troubleshoot this . I made some canonical and syntax mistakes.
I provide working code for community:
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 Emgu.CV; //
using Emgu.CV.CvEnum; // usual Emgu Cv imports
using Emgu.CV.Structure; //
using Emgu.CV.UI;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Runtime.InteropServices;
using Emgu.Util;
using System.Net;
namespace WindowsFormsApplication1
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
Run();
}
public Capture _capture;
public Mat imgOriginal;
private void imageBox2_Click(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
void Run()
{
try
{
_capture = new Capture("http://192.168.1.148:8080/video");
}
catch (Exception e)
{
MessageBox.Show(e.Message);
return;
}
Application.Idle += ProcessFrame;
}
void ProcessFrame(object sender, EventArgs e)
{
Mat frame = _capture.QueryFrame();
ibOriginal.Image = frame;
}
public void button1_Click(object sender, EventArgs e)
{
}
}
}
I am building a simple drag and drop game using windows forms. Its like a jigsaw puzzle. I want to drag pictures from one form to another to complete the puzzle. Is there a way to do this with a PictureBox. It seems to be the only property without the AllowDrop function. Source code below.
I want to drag images from the form on the left to the form on the right.
I was following this tutorial but I cannot get my head around it.
C# Drag and Drop from one Picture box into Another
Form Right
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;
namespace DragDropGame
{
public partial class ChildFormTwo : Form
{
public ChildFormTwo()
{
InitializeComponent();
pictureBox3.DragEnter += pictureBox3_DragEnter;
pictureBox3.DragDrop += pictureBox3_DragDrop;
}
private void pictureBox3_DragDrop(object sender, DragEventArgs e)
{
var bmp = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
pictureBox2.Image = bmp;
}
private void pictureBox3_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
e.Effect = DragDropEffects.Move;
}
}
}
Form Left
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;
namespace DragDropGame
{
public partial class ChildFormTwo : Form
{
public ChildFormTwo()
{
InitializeComponent();
pictureBox3.DragEnter += pictureBox3_DragEnter;
pictureBox3.DragDrop += pictureBox3_DragDrop;
}
private void pictureBox3_DragDrop(object sender, DragEventArgs e)
{
var bmp = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
pictureBox2.Image = bmp;
}
private void pictureBox3_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
e.Effect = DragDropEffects.Move;
}
}
}
The web page is:
Google Tennis Results
What i'm trying to get as text is the tennis results in the top of the page.
Tried using webBrowser now but not getting it at all.
Not sure if it's also java script in the page or not but i can't parse it.
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.IO;
using System.Net;
using System.Threading;
using System.Runtime.InteropServices;
namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
test1();
}
private void test1()
{
this.webBrowser1.ObjectForScripting = new MyScript();
}
[ComVisible(true)]
public class MyScript
{
public void CallServerSideCode()
{
var doc = ((Form1)Application.OpenForms[0]).webBrowser1.Document;
var renderedHtml = doc.GetElementsByTagName("HTML")[0].OuterHtml;
}
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("https://www.google.co.il/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=australian%20open%20atp");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Navigate("javascript: window.external.CallServerSideCode();");
}
}
}