|
class SomeLogicClass{
public static void DoSomething(Form form){
}
public static void DoSomething(Control control){
}
public static void RegisterForm(Form form){
}
}
Also, you can create a singleton class. There are a lot of options. However, the path you are taking I have seen before and it will cause problems in the future. Look for some samples in a c# book as a good guide.
A man said to the universe:
"Sir I exist!"
"However," replied the Universe, "The fact has not created in me A sense of obligation."
-- Stephen Crane
|
|
|
|
|
Trying for a more helpful version of the comment you got...
haz13 wrote: Form1.textBox1.text = "hello";
Form1 is a class. You need to access an instance of the class to pass a value through. What you're trying to do is a VB6 hack.
The best way to do this is to create a delegate between the two classes. I assume delegates can be static, although I've never seen one. If not, you will need an instance of NumberProcessing inside Form1.
The other way to do it is to create a property on Form1 which sets the text of textbox1, and pass a reference to the Form1 instance that's in use to NumberProcessing. This is less of a hack than what you're doing, but not as nice as the delegate.
I trust the code you're showing is just pseudo code. There is no level on which textbox1 and form1 are acceptable variable names. Also, if your controls are public, change them to protected at least. That's just another poor design that you'll come to regret later.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Yes the use of delegates can do that,
Declare a delegate event handler, implement that event handler
in the Form1 class and invoke that event from the other class where you are trying to set the textbox value and pass the value as a parameter to the delegate.
e.g
public delegate void TextChangeEventHandler(string text);
public delegate void ChangeText;
Then implement this method :
You can just write
this.ChangeText += new TextChangeEventHandler(ChangeText);
then the method
Public void ChangeText(string Text)
{
textbox1.text = Text;
}
and in Your class u can just raise the event
Form1.TextChangeEvent obj = new ...
and obj.Invoke();
Hope that works for you
Regards,
Pramod
|
|
|
|
|
Thanks for your more helpful version. I am new to this oop and slowly getting my head around it.
I have gone away and read up on delegates but still fail to understand their use.
I understand what you mean in your second method but am slightly confused about loading a reference into my NumberProcessing class. Surely when creating the instance Form1 which is public(of the form class) Form1 properties and methods should be available to other classes within the same namespace. I don't understand why I can't see them?
OK - accepting you can't see the properties and methods of Form 1 in other classes, how do I load a reference of the Form1 object into my static NumberProcessing class?
Here is my code - the problem is when trying to access Form1.SetFormData(); in the NumberProcessing class.
namespace RouletteV1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn1_Click(object sender, EventArgs e)
{
NumberAttributes.Load(1);
}
public void SetFormData()
{
//here is where I will set the textbox properties
textBox1.Text = "Hello";
}
}
}
--------------
namespace RouletteV1
{
public static class NumberAttributes
{
public void Load(short Number)
{
// load the number data into global properties here
NumberProcessing.UpdateNumArray();
}
}
---------------
namespace RouletteV1
{
public static class NumberProcessing
{
public void UpdateNumArray()
{
// Here I want to call the SetFormData()function in Form1 but it won't recognise it - why???
Form1.SetFormData();
}
}
}
Thanks in advance for your help - Please feel free to comment on what you think to the structure of the program - if you think I have made any other fundamental mistakes
Haz
|
|
|
|
|
Forget my last question. It has just clicked!!! Everything makes sense now
Thanks
Haz
|
|
|
|
|
*grin* No worries, glad we got there.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Is it possible to copy a file using .NET where the path is greater than 248 characters? If not, are there any work arounds?
|
|
|
|
|
Before asking this question you should atleast try this I hope this will work.
Use File.Copy() Method
Best Regards,
Rizwan
|
|
|
|
|
This is what I use but my path contains more than 248 characters which throws a System.IO.PathTooLongException exception. I was wondering if there was any way around this.
|
|
|
|
|
Have you tried
FileInfo f = new FileInfo(LongPath);
f.CopyTo(NewLocation);
|
|
|
|
|
Yes - it throws the same exception.
|
|
|
|
|
hello friends,
I want to get a handle to the currently open instance of the microsoft word. I got that successfully but after that I want to get the inputstream to that process (word). I found method for that but I am getting an error at that statement. my code is as under. someone please give me some suggestion. also let me know if I am doing it in a wrong way.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Windows;
using System.IO;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Process p = new Process();
p.StartInfo.FileName = "C:\\Program Files\\Microsoft Office\\OFFICE11\\WINWORD.exe";
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.Start();
StreamWriter sw = p.StandardInput;
sw.WriteLine("This is a test");
Process[] procs = Process.GetProcesses();
foreach (Process proc in procs)
{
if (proc.MainWindowHandle.ToInt32() != 0)
{
if (proc.ProcessName == "WINWORD")
{
System.Windows.Forms.MessageBox.Show(proc.Handle.ToString());
StreamWriter myStreamWriter = proc.StandardInput;
myStreamWriter.WriteLine("This is a Test");
}
}
}
}
}
-vaishnav hardik
|
|
|
|
|
Where did you see that MS Word will process data from the input stream?
|
|
|
|
|
Hi guys,
In my title bar title text I have my application name and then next to it I would like to put TM, in very small letters. How can I do that?
Please help
sasa
|
|
|
|
|
Open up Microsoft Word, Choose Insert --> Symbol.
Select the 'TM' symbol
Copy it
Paste it in the TEXT property of your form.
This is the 'non technical' way of doing it
|
|
|
|
|
If you don't have word, use charmap isntead.
|
|
|
|
|
Problem is already solved What can I do now
|
|
|
|
|
Hi All !
I want to get a Sum after Filtering some special record , for example [price] is a field in my Table1 .
strFilter="SELECT price From Table1 WHERE City='Athen'";
After filtering with this Query in my Report , I want to get a Sum for the all price related for City='Athen' and I show it in a Label .
Thanks A lot ,
Regards ,
s_mostafa_h
|
|
|
|
|
You can create a running total field or a function. If you just want the sum
SELECT sum(price) as sumOfPrice FROM table WHERE city='Athen'
A man said to the universe:
"Sir I exist!"
"However," replied the Universe, "The fact has not created in me A sense of obligation."
-- Stephen Crane
|
|
|
|
|
thanks
infact :
strFilter="SELECT sum(price) as sumOfPrice FROM table WHERE city='"+txtCity.Text+"'";
I want to put the result of this query in a Label .
s_mostafa_h
|
|
|
|
|
I don't know if your particular application is subject to this but it is always wise to use stored procedures or at the very least parameterization of your query.
SELECT sum(price) as sumOfPrice FROM table WHERE city = @city;
A man said to the universe:
"Sir I exist!"
"However," replied the Universe, "The fact has not created in me A sense of obligation."
-- Stephen Crane
|
|
|
|
|
thanks for ur patient ,
I'm very ,
Crystal Report simply provide this .
s_mostafa_h
|
|
|
|
|
Hello Devs,
I'm developing a Chat application and I'm facing a problem. I have the following Method which is called from multiple threads. And when I start sending too much messages in speed (which invokes this method so fast) then client is disconnected. Let me show you the method and then continue my discussion:
public void SendMessage(TextMessage messageToSend)<br />
{<br />
try<br />
{<br />
lock (this.dataStream)<br />
{<br />
byte[] serializedBytes = messageToSend.GetBytes();
byte[] encMessageBytes = encryptorDecryptor.Encrypt(serializedBytes);
serializedBytes = null;<br />
int messageSize = encMessageBytes.Length;<br />
byte[] messageSizeBuffer = new byte[this.messageDataLengthBufferSize];<br />
messageSizeBuffer = BitConverter.GetBytes(messageSize);<br />
this.dataStream.Write(messageSizeBuffer, 0, this.messageDataLengthBufferSize);
this.dataStream.Write(encMessageBytes, 0, messageSize);
this.dataStream.Flush();<br />
messageSizeBuffer = null;<br />
encMessageBytes = null;<br />
}<br />
<br />
}<br />
catch (Exception ex)<br />
{<br />
string str = ex.Message + "\n" + ex.StackTrace;<br />
Disconnect();<br />
if (Disconnected != null)<br />
{<br />
Disconnected(this, EventArgs.Empty);<br />
}<br />
}<br />
}
In my opnion, the cause of this problem is multi threaded environment or it overflows data when i start sending to fast. for example if it is already working to send data on NetworkStream then a new call to this method is made which passes a new object of TextMessage to it which over writes the existing one so it is all messed up.
Note: Actual exception I get on the receiving end when length of data is not one which is supposed to be. See the code for Receiving:
int messageSize = BitConverter.ToInt32(messageDataLengthBuffer, 0);<br />
MemoryStream memstrm = new MemoryStream();<br />
<br />
readByteCount = 0;<br />
while (messageSize > 0)<br />
{<br />
readByteCount = dataStream.Read(this.messageDataBuffer, 0, messageDataBuffer.Length);<br />
if (readByteCount < 1)<br />
{<br />
Disconnect();<br />
return;<br />
}<br />
memstrm.Write(this.messageDataBuffer, 0, readByteCount);<br />
messageSize -= readByteCount;<br />
}<br />
memstrm.Position = 0;<br />
<br />
byte[] decryptedData = encryptorDecryptor.Decrypt(memstrm.ToArray());<br />
receivedMessage = new TextMessage(decryptedData);<br />
<br />
memstrm.Close();<br />
memstrm = null;
On the receving end I read 4 bytes as a size of incoming message then loop the piece of code to get all data untill complete message is received as of the message size. So when i start messages too fast then at the end of the loop "messageSize" usually gets in -ve which must never be less than 0 and when i try to get data decrypted again, it throughs exception "Length of data is invalid".
Is there any way that i can implement a mechanism that if SendMessage is doing its work then it should not be called again untill its done with its existing work? Or any other way to solve this issue?
Its really urgent. Any help will be really appreciated!!!
Best Regards
|
|
|
|
|
I assume that this.dataStream is the stream you are sending? If so better to create a queue of messages to send and then have a thread loop through the queue. Best part is that you will have a minimum of fuss.
private void AThreadedMethod(){
while(go){
if(queue.Count >0){
someObject = queue.Dequeue();
... do your stuff here
}
}
}
A man said to the universe:
"Sir I exist!"
"However," replied the Universe, "The fact has not created in me A sense of obligation."
-- Stephen Crane
|
|
|
|
|
Thanks for reply, Yes dataStream is an object of NetworkStream. I can understand the mechanism of Queing the messages but creating 100 threads for 50 Clients(50 on client end and 50 on server end) will seriously do a rape with CPU;P. One easiest way to solve this problem is to put Thread.Sleep(50); above each call of this method. Which will also not consume CPU but its cheap and will hit performance of application.
Best Regards,
|
|
|
|