|
You should be using the Activated event to tell when a specific form is brought forward and activated. That's the intended way to accomplish what you're trying to do.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
0. suggest you put a Public Property in the UserControl code to track whether the Control is Visible and Active:
public bool HiddenOrLeft { set; get; }
Update that variable using the various EventHandlers described here:
1. use the 'VisibleChanged Event of the UserControl to detect when it is hidden.
2. use the 'Leave Event of the UserControl to detect it loses Focus on the Form it is sited in.
3. use the 'Enter Event of the UserControl to detect it becomes active/has focus.
4. use the Form 'Leave Event to also set the flag that indicates the UserControl is not active.
You want to go "whole hog:" also handle the /Form.Deactivated Event and the Form 'VisibleChanged Event
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
I am new to C# and I am planing to design my own keypad but I don't know how/where to start, I have 4 textBoxes the keypad buttons.
The first problem came into my mind was: how can I detect the cursor location (which textBox is the cursor in?).
So for example if I had only one textbox then it is easy I could write inside button1 : textBox1.text = "1" and inside button2 : textBox1.text = "2" and inside button_A : textBox1.text = "A".... and so on but I have 4 textBoxes and it is confusing.
Can you please provide me with an idea or what to write inside each button to print its value in the textbox which the cursor is in.
Thank you professionals.
|
|
|
|
|
edit #1
All you need to do to keep the Focus on the TextBox Control when a Button is clicked is to set the TextBox 'Capture property to 'true in the Click EventHandler. Example:
private void button1_Click(object sender, EventArgs e)
{
CurrentTextBox.Text += "A";
CurrentTextBox.Capture = true;
} See below for how to keep the 'CurrentTextBox field up to date as the user changes which TextBox they use.
end edit #1
The word "keypad" suggests to me a bunch of Buttons that you click on, and things happen.
I am not clear what you are doing with the TextBoxes here; please clarify.
In WinForms the 'ActiveControl property of the ContainerControl Class will always return which Control has Focus in the ContainerControl (which could be Form, a Panel, etc.) [^].
Control activeFormControl = this.ActiveControl;
Control activePanelControl = panel1.ActiveControl;
A way I often use to keep track of which of several TextBoxes has Focus currently, or has had Focus most recently is: wire all of them up to the same 'Enter EventHandler, and:
TextBox CurrentTextBox;
private void TextBoxes1To4_Enter(object sender, EventArgs e)
{
CurrentTextBox = sender as TextBox;
if(CurrentTextBox == null)
{
throw new ArgumentNullException("Illegal use TextBox Enter");
}
switch (CurrentTextBox.Name)
{
case "textBox1":
textBox1.Text = "Entered";
break;
case "textBox2":
break;
case "textBox3":
break;
case "textBox4":
break;
}
}
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
modified 14-Nov-15 3:56am.
|
|
|
|
|
Clicking the button will cause the cursor to leave any textbox it is in.
You can determine which textbox the cursor is in (or "was" in) by hooking up the "Leave" event for each textbox and storing the "sender" (i.e. textbox) for each event in a common field / property. (Note that all the textboxes can use the same event handler). e.g.
private TextBox _tb = null;
this.textBox1.Leave += new System.EventHandler(this.textBox_Leave);
this.textBox2.Leave += new System.EventHandler(this.textBox_Leave);
this.textBox3.Leave += new System.EventHandler(this.textBox_Leave);
private void textBox_Leave( object sender, EventArgs e ) {
_tb = sender as TextBox;
}
private void button1_Click( object sender, EventArgs e ) {
if ( _tb != null ) {
MessageBox.Show( "The last TextBox was: " + _tb.Name );
}
}
(The "Enter" event can be used to record where the cursor currently is as long as there is no other UI activity that will move the cursor; i.e. a button click).
|
|
|
|
|
Hi
If You are new to C# it wont be easy.
First of all You can not use buttons, because when You click a button that button will steal focus, so your key stroke will have no notion where to go.
The only (winforms)control that not steals focus is a label, but a label has not got the right properties to act as a key.
The solution is to create a custom control derived from label control.
to do so You must add this class to your project:
namespace YOURPROJECTNAMESPACE
{
public sealed class MyControl : Label
{
public MyControl()
{
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
BackColor = Color.Transparent;
}
public Image ControlImage { get; set; }
protected override void OnPaint(PaintEventArgs e)
{
if (ControlImage != null)
{
e.Graphics.DrawImage(ControlImage, 4, 4, Width - 8, Height - 8);
}
var rectangle = new RectangleF(2, 0, Width, Height + 5);
var format = new StringFormat
{
LineAlignment = StringAlignment.Center,
Alignment = StringAlignment.Center
};
e.Graphics.DrawString(Text, Font, Brushes.Black, rectangle, format);
}
}
}
After you have done this You will find in the designer toolbox MyControl.
Then drag a flowLayouPanel from the toolbox. Populate this panel by dragging as many MyControls as you need. in properties give them a nice picture of a key and a character as text.
One more thing to do, In the designer doubleclick each key,and in the click event this:
private void myControl1_Click(object sender, EventArgs e)
{
SendKeys.Send("{1}");
}
private void myControlEnter_Click(object sender, EventArgs e)
{
SendKeys.Send("{ENTER}");
}
Etc.
If You have any more questions just ask,
Groover
0200 A9 23
0202 8D 01 80
0205 00
modified 13-Nov-15 21:47pm.
|
|
|
|
|
Well, it can be easy: all you need to do to keep the Focus on the TextBox Control when a Button is clicked is to set the TextBox 'Capture to 'true in the Click EventHandler.
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
hello people i have studied a network sniffer on this site <a href="http://www.codeproject.com/Articles/17031/A-Network-Sniffer-in-C"> </a>[<a href="http://www.codeproject.com/Articles/17031/A-Network-Sniffer-in-C" target="_blank" title="New Window">^</a>] so how can i do the reverse which is creating the packet in c# or vb.net pcap seems not to work
thank you
|
|
|
|
|
1. I suggest you post on the article itself
2. You may have pasted the link wrongly, since we can't click it
Best,
John
-- LogWizard Meet the Log Viewer that makes monitoring log files a joy!
|
|
|
|
|
Crafting packets from scratch can only be done by administrators. I you're trying to do this as a normal user, it's never going to work. Why? Security reasons.
|
|
|
|
|
i working on a NAT project of mine .i can receive packtes and get the info such as source ip and dest ip but i need to do the reverse by creating a packet and im an administartor by the way
|
|
|
|
|
|
I have object with members of different types like this:
public class Node
{
public int itemID = -1;
public int counter = 1;
public Node parent = null;
public List<Node> childs = new List<Node>();
public Node nodeLink = null;
}
And I have class tree
public class tree
{
public List<int> headerList = null;
public Dictionary<int, Node> mapItemNodes = new Dictionary<int, Node>();
public Dictionary<int, Node> mapItemLastNode = new Dictionary<int, Node>();
public Node root = new Node();
}
And I need your help to serialize and deserialize an object of the class tree
|
|
|
|
|
Rather than using fields, you're going to have to use properties if you want to use a default serializer - and don't forget the Serializable attribute on your class.
|
|
|
|
|
Note: I have two classes where I use XmlSerializer, and they have fields -which are correctly serialized.
Best,
John
-- LogWizard Meet the Log Viewer that makes monitoring log files a joy!
|
|
|
|
|
So long as the fields are public, yes that will work. If there are fields that are private, they must be wrapped in public properties in order to work.
|
|
|
|
|
did your classes contain dictionaries? or would you please give me an example
modified 12-Nov-15 9:14am.
|
|
|
|
|
|
Your class "tree" contains Dictionaries - and that causes a problem: it won't be serializable by the XMLSerializer. That means: you havt to write your own serialization / deserialization code.
|
|
|
|
|
fyi: I have written a Tree structure very much like this, and had no problem serializing/de-serializing Dictionaries using WCF.
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
|
|
|
|
|
You can use WCF (DataContract and DataMember) to serialize structures like this; in fact, I've written a very similar tree-structure, with internal Dictionaries, and it serializes/de-serializes okay. Compressing the XML output of the WCF serializer with GZip results in file sizes often less than 50% of the umcompressed file, and often with file sizes as low as 25% of the uncompressed XML. Those file sizes are comparable to the size of the structure(s) saved with binary serialization.
I gave an example of using WCF on a recent answer to a QA question here: ^].
Do read the documentation on using WCF; and study the various fields of the DataContract and DataMember Attributes that are optional; they are very useful !
Also, note that if your Class inherits from some Generic Collection, like this:
[DataMember]
public class MyClass : List<SomeOtherClass>
{
[DataMember]
public int SomeIntProp { get; set; }
} This will not serialize, even if 'SomeOtherClass is also properly adorned with WCF Attributes. I've never found an explanation of this.
So, code the generic Collection as a Property in another Class.
Suggestion: use Properties, not Fields.
Another idea: Here you'll find an implementation of a Generic approach to using WCF ... the code was inspired by the comments and ideas of Pete O'Hanlon on that thread, but if the code doesn't work for you, that's my fault
[^]
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.
modified 12-Nov-15 17:15pm.
|
|
|
|
|
Dear all,
thanks a lot for your replies, but actually I am confused because I am a new user to c#, and now I am using ConsoleApplication in my application. my aim from asking you is to write the content of the tree to a file and retreiving it a gain to the same object regardless the used method. I hope that you help me in my problem.
|
|
|
|
|
Hi,
I have a little method which registeres an extension (edit: like .ext) into the Windows registry. Does anyone have an idea how I can tell Windows to open a file with the respective extension only in a new program instance, if currently none is running? If the program is already running, a new tab in my program should be created (I have a event void opentab(object sender, EventArgs e) to open a new tab). But how can I tell windows not to open a new instance but to call the running program and let it run the event?
I think one can somehow do it with a subnode "ddeexec" in the subnode "open", but I'm not sure and it didn't work for me.
Best wishes
robin
void DefineAssociations(string extension, string IconFile)
{
string keyName = extension;
RegistryKey key = Registry.ClassesRoot.OpenSubKey(keyName);
if (key != null)
{
string apppath = "";
apppath = Application.ExecutablePath.ToLower() + " \"%1\"";
key.SetValue("", "MyProg");
RegistryKey iconkey;
iconkey = key.CreateSubKey("DefaultIcon");
iconkey.SetValue("", Environment.CurrentDirectory + "\\resources\\" + IconFile);
key = key.CreateSubKey("shell");
key = key.CreateSubKey("open");
key = key.CreateSubKey("command");
key.SetValue("", apppath);
}
}
|
|
|
|
|
I don't think it's possible. Anyway, long story short - that's not the way to do it.
It's your job to find out if another instance is running, and if so, to notify it. It's not an easy task though. There's quite a bit of code required to do this. Namely, first find out if another instance of your app is running, and if so, you need to notify it and send it the new file to open.
More or less, something like this:
[StructLayout(LayoutKind.Sequential)]
public struct COPYDATASTRUCT {
public IntPtr dwData;
public int cbData;
[MarshalAs(UnmanagedType.LPStr)] public string lpData;
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, ref COPYDATASTRUCT lParam);
public const int WM_COPYDATA = 0x004A;
...
var running_already = Process.GetProcessesByName("Yourapp").ToDictionary(x => x.Id, x => x);
running_already.Remove(Process.GetCurrentProcess().Id);
if (running_already.Count > 0) {
if (args.Length == 0)
return;
string open = args[0];
var cds = new win32.COPYDATASTRUCT {
dwData = new IntPtr(0),
cbData = open.Length * 2 + 1,
lpData = open
};
var handle = running_already.First().Value.MainWindowHandle;
win32.SendMessage(handle, win32.WM_COPYDATA, IntPtr.Zero, ref cds);
return;
}
protected override void WndProc(ref Message m) {
if (m.Msg == win32.WM_COPYDATA) {
var st = (win32.COPYDATASTRUCT) Marshal.PtrToStructure(m.LParam, typeof (win32.COPYDATASTRUCT));
string open = st.lpData;
util.postpone(() => on_file_drop(open), 100);
}
base.WndProc(ref m);
}
-- LogWizard Meet the Log Viewer that makes monitoring log files a joy!
|
|
|
|
|
Wow, it works up to the point at which I just need to call the file open event. Great! thanks a lot;)
robin
|
|
|
|