|
Hello,
I have a MDI parent form that has some children,application requires
that the child form appear only one time and if it is minimized it restores its position if there is any try to open a new instance of it.
I don't know how to let the child form to restore its position after being minimized?
Thanks.
Dad
|
|
|
|
|
First of all at child form try this code:
<br />
this.LocationChanged += new System.EventHandler(this.ChildFormLocationChanged);
and then write:
private void ChildFormLocationChanged(object sender, EventArgs e)<br />
{<br />
if (this.WindowState != FormWindowState.Minimized)<br />
{<br />
x = this.Location.X; <br />
y = this.Location.Y;<br />
}<br />
else <br />
{<br />
MessageBox.Show(x.ToString() + " " + y.ToString());<br />
}<br />
}
Hope, this might help you.
Njoy coding..
Anindya Chatterjee
|
|
|
|
|
Hi All,
Ive been doing alot of ready about backgroud workers and would like to learn how they work and get one into my small simple application i am building. The problem is that i am finding it hard to understand how to get it into my applciation and how they really work. I was wondering if someone could point me in the direction or even give me an example of it working in my code!
bear in mined that i am a beginner to programming, not just C# so any help would be very much appreciated!
My Port Scanner Application:
[Form1.cs]
using System;<br />
using System.Collections.Generic;<br />
using System.Data;<br />
using System.Drawing;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
using System.Net.Sockets;<br />
using System.IO;<br />
<br />
namespace GUI_Port_Scanner<br />
{<br />
public partial class Form1 : Form<br />
{<br />
string _ip;<br />
int _startPort;<br />
int _endPort;<br />
string fileName = "scan_results.txt";<br />
TcpClient TcpScan = new TcpClient();<br />
<br />
public Form1()<br />
{<br />
InitializeComponent();<br />
}<br />
<br />
private void Form1_Load(object sender, EventArgs e)<br />
{<br />
_ip = "";<br />
_startPort = 0;<br />
_endPort = 0;<br />
}<br />
<br />
private void startScanBtt_Click(object sender, EventArgs e)<br />
{<br />
<br />
if (prevItems.Checked == true)<br />
{<br />
listBoxClosed.Items.Clear();<br />
listBoxOpen.Items.Clear();<br />
}<br />
<br />
_ip = hostTxt.Text;<br />
_startPort = Int32.Parse(startPortTxt.Text);<br />
_endPort = Int32.Parse(endPortTxt.Text);<br />
<br />
for (int _currentPort = _startPort; _currentPort <= _endPort; _currentPort++)<br />
{<br />
try<br />
{<br />
TcpScan.Connect(_ip, _currentPort);<br />
listBoxOpen.Items.Add(" => Port " + _currentPort + " is open.");<br />
}<br />
catch<br />
{<br />
listBoxClosed.Items.Add(" => Port " + _currentPort + " is closed.");<br />
}<br />
}<br />
<br />
toFileBtt.Enabled = true;<br />
}<br />
<br />
private void toFileBtt_Click(object sender, EventArgs e)<br />
{<br />
StreamWriter writeTo = new StreamWriter(fileName, true);<br />
if (listBoxOpen.Items.Count != 0)<br />
{<br />
writeTo.WriteLine();<br />
writeTo.WriteLine(" Open Ports:");<br />
writeTo.WriteLine("*-----------------------------*");<br />
}<br />
foreach (object item in listBoxOpen.Items)<br />
{<br />
writeTo.WriteLine(item.ToString());<br />
}<br />
if (listBoxClosed.Items.Count != 0)<br />
{<br />
writeTo.WriteLine();<br />
writeTo.WriteLine(" Closed Ports:");<br />
writeTo.WriteLine("*-----------------------------*");<br />
}<br />
foreach (object item in listBoxClosed.Items)<br />
{<br />
writeTo.WriteLine(item.ToString());<br />
}<br />
writeTo.Close();<br />
}<br />
}<br />
}
[Form1.Designer.cs]
namespace GUI_Port_Scanner<br />
{<br />
partial class Form1<br />
{<br />
private System.ComponentModel.IContainer components = null;<br />
<br />
protected override void Dispose(bool disposing)<br />
{<br />
if (disposing && (components != null))<br />
{<br />
components.Dispose();<br />
}<br />
base.Dispose(disposing);<br />
}<br />
<br />
#region Windows Form Designer generated code<br />
<br />
private void InitializeComponent()<br />
{<br />
this.hostTxt = new System.Windows.Forms.TextBox();<br />
this.startPortTxt = new System.Windows.Forms.TextBox();<br />
this.label1 = new System.Windows.Forms.Label();<br />
this.label2 = new System.Windows.Forms.Label();<br />
this.endPortTxt = new System.Windows.Forms.TextBox();<br />
this.label3 = new System.Windows.Forms.Label();<br />
this.startScanBtt = new System.Windows.Forms.Button();<br />
this.listBoxOpen = new System.Windows.Forms.ListBox();<br />
this.listBoxClosed = new System.Windows.Forms.ListBox();<br />
this.label4 = new System.Windows.Forms.Label();<br />
this.label5 = new System.Windows.Forms.Label();<br />
this.prevItems = new System.Windows.Forms.CheckBox();<br />
this.toFileBtt = new System.Windows.Forms.Button();<br />
this.SuspendLayout();<br />
this.hostTxt.Location = new System.Drawing.Point(72, 12);<br />
this.hostTxt.Name = "hostTxt";<br />
this.hostTxt.Size = new System.Drawing.Size(196, 20);<br />
this.hostTxt.TabIndex = 1;<br />
this.startPortTxt.Location = new System.Drawing.Point(72, 38);<br />
this.startPortTxt.Name = "startPortTxt";<br />
this.startPortTxt.Size = new System.Drawing.Size(65, 20);<br />
this.startPortTxt.TabIndex = 2;<br />
this.label1.AutoSize = true;<br />
this.label1.Location = new System.Drawing.Point(12, 15);<br />
this.label1.Name = "label1";<br />
this.label1.Size = new System.Drawing.Size(32, 13);<br />
this.label1.TabIndex = 3;<br />
this.label1.Text = "Host:";<br />
this.label2.AutoSize = true;<br />
this.label2.Location = new System.Drawing.Point(11, 41);<br />
this.label2.Name = "label2";<br />
this.label2.Size = new System.Drawing.Size(54, 13);<br />
this.label2.TabIndex = 4;<br />
this.label2.Text = "Start Port:";<br />
this.endPortTxt.Location = new System.Drawing.Point(203, 38);<br />
this.endPortTxt.Name = "endPortTxt";<br />
this.endPortTxt.Size = new System.Drawing.Size(65, 20);<br />
this.endPortTxt.TabIndex = 5;<br />
this.label3.AutoSize = true;<br />
this.label3.Location = new System.Drawing.Point(144, 42);<br />
this.label3.Name = "label3";<br />
this.label3.Size = new System.Drawing.Size(51, 13);<br />
this.label3.TabIndex = 6;<br />
this.label3.Text = "End Port:";<br />
this.startScanBtt.Location = new System.Drawing.Point(274, 12);<br />
this.startScanBtt.Name = "startScanBtt";<br />
this.startScanBtt.Size = new System.Drawing.Size(79, 46);<br />
this.startScanBtt.TabIndex = 7;<br />
this.startScanBtt.Text = "Scan";<br />
this.startScanBtt.UseVisualStyleBackColor = true;<br />
this.startScanBtt.Click += new System.EventHandler(this.startScanBtt_Click);<br />
this.listBoxOpen.BackColor = System.Drawing.SystemColors.MenuText;<br />
this.listBoxOpen.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));<br />
this.listBoxOpen.ForeColor = System.Drawing.Color.LimeGreen;<br />
this.listBoxOpen.FormattingEnabled = true;<br />
this.listBoxOpen.ItemHeight = 16;<br />
this.listBoxOpen.Location = new System.Drawing.Point(12, 104);<br />
this.listBoxOpen.Name = "listBoxOpen";<br />
this.listBoxOpen.Size = new System.Drawing.Size(168, 164);<br />
this.listBoxOpen.TabIndex = 8;<br />
this.listBoxClosed.BackColor = System.Drawing.SystemColors.MenuText;<br />
this.listBoxClosed.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));<br />
this.listBoxClosed.ForeColor = System.Drawing.Color.DarkRed;<br />
this.listBoxClosed.FormattingEnabled = true;<br />
this.listBoxClosed.ItemHeight = 16;<br />
this.listBoxClosed.Location = new System.Drawing.Point(186, 104);<br />
this.listBoxClosed.Name = "listBoxClosed";<br />
this.listBoxClosed.Size = new System.Drawing.Size(167, 164);<br />
this.listBoxClosed.TabIndex = 9;<br />
this.label4.AutoSize = true;<br />
this.label4.Location = new System.Drawing.Point(14, 88);<br />
this.label4.Name = "label4";<br />
this.label4.Size = new System.Drawing.Size(63, 13);<br />
this.label4.TabIndex = 10;<br />
this.label4.Text = "Open Ports:";<br />
this.label5.AutoSize = true;<br />
this.label5.Location = new System.Drawing.Point(188, 88);<br />
this.label5.Name = "label5";<br />
this.label5.Size = new System.Drawing.Size(69, 13);<br />
this.label5.TabIndex = 11;<br />
this.label5.Text = "Closed Ports:";<br />
this.prevItems.AutoSize = true;<br />
this.prevItems.Location = new System.Drawing.Point(73, 65);<br />
this.prevItems.Name = "prevItems";<br />
this.prevItems.Size = new System.Drawing.Size(122, 17);<br />
this.prevItems.TabIndex = 13;<br />
this.prevItems.Text = "Clear Previous Items";<br />
this.prevItems.UseVisualStyleBackColor = true;<br />
this.toFileBtt.Enabled = false;<br />
this.toFileBtt.Location = new System.Drawing.Point(274, 61);<br />
this.toFileBtt.Name = "toFileBtt";<br />
this.toFileBtt.Size = new System.Drawing.Size(79, 23);<br />
this.toFileBtt.TabIndex = 14;<br />
this.toFileBtt.Text = "Copy to File";<br />
this.toFileBtt.UseVisualStyleBackColor = true;<br />
this.toFileBtt.Click += new System.EventHandler(this.toFileBtt_Click);<br />
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);<br />
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;<br />
this.ClientSize = new System.Drawing.Size(365, 347);<br />
this.Controls.Add(this.toFileBtt);<br />
this.Controls.Add(this.prevItems);<br />
this.Controls.Add(this.label5);<br />
this.Controls.Add(this.label4);<br />
this.Controls.Add(this.listBoxClosed);<br />
this.Controls.Add(this.listBoxOpen);<br />
this.Controls.Add(this.startScanBtt);<br />
this.Controls.Add(this.label3);<br />
this.Controls.Add(this.endPortTxt);<br />
this.Controls.Add(this.label2);<br />
this.Controls.Add(this.label1);<br />
this.Controls.Add(this.startPortTxt);<br />
this.Controls.Add(this.hostTxt);<br />
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;<br />
this.Name = "Form1";<br />
this.Text = "Input | Output - Port Scanner";<br />
this.Load += new System.EventHandler(this.Form1_Load);<br />
this.ResumeLayout(false);<br />
this.PerformLayout();<br />
<br />
}<br />
<br />
#endregion<br />
<br />
private System.Windows.Forms.TextBox hostTxt;<br />
private System.Windows.Forms.TextBox startPortTxt;<br />
private System.Windows.Forms.Label label1;<br />
private System.Windows.Forms.Label label2;<br />
private System.Windows.Forms.TextBox endPortTxt;<br />
private System.Windows.Forms.Label label3;<br />
private System.Windows.Forms.Button startScanBtt;<br />
private System.Windows.Forms.ListBox listBoxOpen;<br />
private System.Windows.Forms.ListBox listBoxClosed;<br />
private System.Windows.Forms.Label label4;<br />
private System.Windows.Forms.Label label5;<br />
private System.Windows.Forms.CheckBox prevItems;<br />
private System.Windows.Forms.Button toFileBtt;<br />
}<br />
}<br />
Also any critisism on any other aspect of the program would be great, along with tips and tricks
Thank you very much!
Relentless.
|
|
|
|
|
Threading is a complex concept to handle early on.
Never post your designer.cs file, the IDE generates that.
I see no progress bar or threading code here. Use the BackgroundWorker class, it's really easy, and it has a report progress event you can use to run a progress bar.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Thanks for the quick reply,
Noted about the designer.cs file and i have not put any background worker or progress bar in yet, i will do some more research then and get back to you.
Thanks again,
Relentless.
Knowledge is Power.
|
|
|
|
|
|
Here are a selection of links which helped me learn about threading.
What I would say is don't let what seems like a complex area put you off learning about threading(the basics are just that - basic).
Threading is a basic requirement for decent user interfaces - running processes in the background while allowing the user to still access a form.
Jump in and have fun:
Codeproject threading demonstration
Good General Tutorial on threading
Codeproject sync async threads
Regards
Guy
Continuous effort - not strength or intelligence - is the key to unlocking our potential.(Winston Churchill)
|
|
|
|
|
Thank you very muc for your quick replys,
So far i understand that i will need to create a function that will initialize the background worker, i will then need to put the code i want it to do in the background i.e:
for (int _currentPort = _startPort; _currentPort <= _endPort; _currentPort++)<br />
{<br />
try<br />
{<br />
TcpScan.Connect(_ip, _currentPort);<br />
listBoxOpen.Items.Add(" => Port " + _currentPort + " is open.");<br />
}<br />
catch<br />
{<br />
listBoxClosed.Items.Add(" => Port " + _currentPort + " is closed.");<br />
}<br />
}
That is as far as i have got thanks a lot for the resorces i will keep reading and playing around with the code. Still a bit confusing.
Relentless.
Knowledge is Power.
|
|
|
|
|
One important point - you can't manipulate UI stuff in the thread, you need instead to send messages to the Progress event, which runs on the main thread, and can update your UI.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
OK thanks again,
Will have to do a lot more reading thanks again.
Relentless.
Knowledge is Power.
|
|
|
|
|
*grin* yeah, there's a number of threading classes, BackgroundWorker definately is the one that makes everything easy for you
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
I have a list and I wish to print the list to the screen or a form in a windows application.
In a console application, I would do it as follows:
foreach ( )
console.Writeline();
I have tried using MessageBox and it shows the list output, but that is not what I want. I want to have the output appear line by line (similar to writeline) and also well formatted.
Could someone point me in the right direction?
|
|
|
|
|
You could use a ListBox and append the items from the list into the ListBox.
|
|
|
|
|
Ok Thanks, I will try that. Althought I would think that <cr> might be a problem
|
|
|
|
|
Jerry Graham,
Label listLabel = new Label();
listLabel.Location = new Point(10, 10);
listLabel.AutoSize = true;
foreach (string item in theList)
{
listLabel.Text += item + Environment.NewLine;
}
form.Controls.Add(listLabel);
Something like that?
Regards,
Gareth.
|
|
|
|
|
|
Hello,
I'm wondering how to go about getting the mouse cursor position relative to a child window in a given windows application. I'm writing an application that is interested in this data.
Example: The user clicks a button in some winforms app, and my application uses low level hooks (or some other strategy) to determine the mouse coordinates relative to the clicked button in the other application.
Any thoughts on how to proceed? I'm hooking the mouse so I get the screen coordinates when the mouse is clicked, but I'm not sure how to get position (relative or absolute) of the control that generated the "click" event.
Thanks!
|
|
|
|
|
You can get screen co-ordinates and do the conversion yourself, it's pretty straightforward, just get the position of the window in question.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
Yeah, I guess my question is really how to get the position of a child window that generated the event (where the child window is running in a separate application from the code that is interested in its position). I see that Spy++ is able to identify the positions of child windows, I just don't know what APIs are being used.
|
|
|
|
|
Snippet:
protected override void OnStart(string[] args)<br />
{<br />
try<br />
{<br />
Log("Service Starting");<br />
<br />
serviceThread = new Thread(new ThreadStart(runThread));<br />
serviceThread.Start();<br />
}<br />
catch(Exception ex)<br />
{<br />
Log("Error: " + ex.Message);<br />
}<br />
}<br />
<br />
protected override void OnStop()<br />
{<br />
Log("Service Stopping");<br />
serviceThread.Abort();<br />
}<br />
<br />
private void runThread()<br />
{<br />
Log("Service Thread Running");<br />
while(true)<br />
{<br />
DCNAuditProcessor.DoCheck();<br />
Thread.Sleep(waitTime);<br />
}<br />
}
The runThread Method never runs. The service starts succussfully, but this code is never executed. The Serivce Starting notification and Service Stopping notification is succcessfully logged to a log file, but never the Service Thread Running.
I have reviewed the event logs and there are no errors.
Any ideas would be appreciated.
-Thanks
|
|
|
|
|
I dunno, I don't spot any glaring problems in what you posted.
Does the list of Services show it as Running?
|
|
|
|
|
I found out what the issue was, but still not exactly sure why.
This line of code is calling a method of a class that belongs to another DLL: DCN_Audit.DCNAuditProcessor.DoCheck();
The name of this DLL was DCN_Audit.dll. The name of the service was DCN_Audit.exe. I was able to trap this error by calling the method directly: Error: Could not load type DCN_Audit.DCNAuditProcessor from assembly CF_DCN_Audit, Version=1.0.3048.28191, Culture=neutral, PublicKeyToken=null.
This would start the service, and it showed as Running in the Services Manager. The thread would stop because the error loading the dll, so no processing was taking place. Also, no errors were visable in the Event Viewer.
So, it appears that the dll cannot have the same name as the exe. I changed the dll project name and re-compiled producing a dll name of DCN_Audit_Processor.dll, and everything processed successfully.
I just wanted to post in case others had similar issues.
-Thanks
|
|
|
|
|
ooten14 wrote: the dll cannot have the same name as the exe
That doesn't make sense. But glad to hear it's working.
|
|
|
|
|
Alright, so I have an application that is in constant communication with the database. As happens in the real world, sometimes (devilishly often in this case actually) the line goes down. When this happens, the application needs to be restored to its initial state. A similar thing will need to happen when the user allows the application to time out. Someone suggested on the ALT.NET list that I simply unload the application domain. After poking around online for what this actually means this seems like a great idea. I am having trouble however getting my test program to work and I fear that I must be missing something.
My test has two components
ApplicationDomainChild - a simple windows form with a label displaying a number and a button that when you press increments the number. I've compiled this to an .exe.
ApplicationDomainParent - another windows form with a single button and the following:
<br />
public partial class Form1 : Form {<br />
private AppDomain _second_domain = null;<br />
private string _second_assembly_path = @"ApplicationDomainChild.exe";<br />
<br />
private void Form1_Load(object sender, EventArgs e) {<br />
_second_domain = AppDomain.CreateDomain("Second Domain");<br />
_second_domain.ExecuteAssembly(_second_assembly_path);<br />
}<br />
private void button1_Click(object sender, EventArgs e) {<br />
try {<br />
AppDomain.Unload(_second_domain);<br />
_second_domain = AppDomain.CreateDomain("Second Domain");<br />
_second_domain.ExecuteAssembly(_second_assembly_path);<br />
}<br />
catch (Exception ex) {<br />
System.Diagnostics.Debug.WriteLine(ex);<br />
}<br />
}<br />
}<br />
What I expect to happen is for both forms to open at the same time, me to be able to increment the values in ApplicationDomainChild as much as I want by pressing the button and then be able to press the button on ApplicationDomainParent and have that shut down and restart the ApplicationDomainChild.
What happens however is that first ONLY ApplicationDomainChild runs. After 'X' out of the window ApplicationDomainParent runs. When I press the restart domain button the child program starts up again and I can interact with it, but if I attempt to click the restart button again I get a ThreadAbortException followed by an AppDomainUnloadException (or something like that). Can someone tell me what I'm doing wrong?
|
|
|
|
|
I know its bad form to give myself a self-bump but please can someone just give me a hint? The problem seems to be that my child application is running on the same thread as the parent, therefore only one of them can process input at a time. Do I need to somehow start the appdomain in a different thread? I thought threads were inside of appdomains.
|
|
|
|
|