I have a string named " e.RequestMessage.Text " . In a class this string I want to show has a value int a textbox in a windows form .
I have the project that contains a class which shows my string in a console. So I want to show this sting in a textbox on a windows form. I added a windows form to it (the class runs in front)
How can I accomplish this?
using System;
using Eneter.Messaging.DataProcessing.Serializing;
using Eneter.Messaging.EndPoints.TypedMessages;
using Eneter.Messaging.MessagingSystems.MessagingSystemBase;
using Eneter.Messaging.MessagingSystems.TcpMessagingSystem;
using Eneter.ProtoBuf;
using message.declarations;
namespace ServiceExample
{
class Program
{
private static IDuplexTypedMessageReceiver<MyResponse, MyRequest> myReceiver;
static void Main(string[] args)
{
// Instantiate Protocol Buffer based serializer.
ISerializer aSerializer = new ProtoBufSerializer();
// Create message receiver receiving 'MyRequest' and receiving 'MyResponse'.
// The receiver will use Protocol Buffers to serialize/deserialize messages.
IDuplexTypedMessagesFactory aReceiverFactory = new DuplexTypedMessagesFactory(aSerializer);
myReceiver = aReceiverFactory.CreateDuplexTypedMessageReceiver<MyResponse, MyRequest>();
// Subscribe to handle messages.
myReceiver.MessageReceived += OnMessageReceived;
// Create TCP messaging.
IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory();
IDuplexInputChannel anInputChannel
= aMessaging.CreateDuplexInputChannel("tcp://127.0.0.1:8060/");
// Attach the input channel and start to listen to messages.
myReceiver.AttachDuplexInputChannel(anInputChannel);
Console.WriteLine("The service is running. To stop press enter.");
Console.ReadLine();
// Detach the input channel and stop listening.
// It releases the thread listening to messages.
myReceiver.DetachDuplexInputChannel();
}
// It is called when a message is received.
private static void OnMessageReceived(object sender, TypedRequestReceivedEventArgs<MyRequest> e)
{
Console.WriteLine("Received: " + **e.RequestMessage.Text**);
// Create the response message.
MyResponse aResponse = new MyResponse();
aResponse.Length = e.RequestMessage.Text.Length;
// Send the response message back to the client.
myReceiver.SendResponseMessage(e.ResponseReceiverId, aResponse);
}
}
}
Windows form code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ServiceExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
private void button1_Click(object sender, EventArgs e)
{
sum = x + y;
MessageBox.Show("Ans=" + sum);
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
string myString = sum.ToString();
textBox3.Text = myString;
}
Where do you construct the form?
One thing you can do is create a custom constructor for the form that takes in a String and just pass e.RequestMessage.Text to it.
Something like:
public Form1(String messageText)
{
InitializeComponent();
aTextBox.Text = messageText;
}
And then in ServiceExample:
Form1 form1 = new Form1(e.RequestMessage.Text);
Have you tried to update the value of your textbox as part of the event? This would be accomplished as:
public void OnMessageReceived(params)
{
yourTextBox.Text = e.RequestMessage.Text;
}
Unless I misunderstood your question, this should do what you expect.
Related
This is my first assignment using C# and I've learned briefly about programming with Python in my pre-u. The C# form that I would like to show the user upon logging in can't be shown and instead, the program displays the Built errors msg, where I click on 'Yes' to continue running the last successful build. However, it will show one of the previous forms (which I have deleted from the program) instead - making it hard for me to test run and check for any other errors in my program.
The form I would like to show is TechnicianHome.
Error
There were built errors. Would you like to continue and run the last successful build?
Any help is much appreciated.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace ASSIGNMENT
{
public partial class TechnicianHome : Form
{
public static string name;
public TechnicianHome()
{
InitializeComponent();
}
public TechnicianHome (string n)
{
InitializeComponent();
name = n;
}
private void TechnicianHome_Load(object sender, EventArgs e)
{
lblWelcome.Text = "Welcome" + name;
}
private void button1_Click(object sender, EventArgs e)
{
ServiceRequests vr = new ServiceRequests();
this.Hide();
vr.ShowDialog();
}
private void button1_Click_1(object sender, EventArgs e)
{
UpdateProfile up = new UpdateProfile();
this.Hide();
up.ShowDialog();
}
}
}
I am relatively new to coding. I wanted to create a trainer for the game Assault Cube. I have already implemented an "unlimited ammo" option which works perfectly fine and now i want to make a god mode (unlimited health) option. It doesn't work, and I dont understand why. I have taken the correct offsets from the game (checked with cheat-engine), did all the stuff I did with the ammo also with the health and added the new health values that should be frozen. Is there a problem in the program? Here 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;
using System.Threading.Tasks;
using System.Windows.Forms;
using Memory;
namespace AssaultHack
{
public partial class Form1 : Form
{
Mem meme = new Mem();
public static string RifleAmmo = "ac_client.exe+0x00109B74,150";
public static string PlayerHealth = "ac_client.exe+0x0010A280,338,34,98,8";
public Form1()
{
InitializeComponent();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
int PID = meme.GetProcIdFromName("ac_client");
if(PID > 0)
{
meme.OpenProcess(PID);
Thread WA = new Thread(writeAmmo) { IsBackground = true };
Thread PH = new Thread(godMode) { IsBackground = true };
PH.Start();
WA.Start();
}
}
private void writeAmmo()
{
while(true)
{
if (checkBox1.Checked)
{
meme.WriteMemory(RifleAmmo, "int", "99999");
Thread.Sleep(2);
}
Thread.Sleep(2);
}
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
}
private void godMode()
{
while(true)
{
if (checkBox2.Checked)
{
meme.WriteMemory(PlayerHealth, "int", "99999");
Thread.Sleep(2);
}
Thread.Sleep(2);
}
}
}
}
In all the games there are 2 types of vars:
1.netvars: this type of vars is managed by the host of the server and you cant change them only if you was the host of the server (in this case you can change all the players vars) or if you exploited the send socket in the server
2.localvars: this type of vars is stored and managed in your game data locally and you can change this vars anytime like: field of view/viewMatrix/angles.
In your case Assault Cube the health/shield/xyz/score/ammo is netvars and you cant change them only if you was the host. but you can reverse enginner the game to find things that can help you to create exploit like flying/etc...
Trying to receive data, from mk, using DataReceived and handler event, what i do is -
push a button on a program (code is below) then LED on mk will turn on, then the data should be sent back to program (expecting 1, on byte value, but also tried string value, doesn't work). Sending side is working, but receiving....not
seems like i'm missing something. Any help apreciate it. Thx in Further
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.Ports;
namespace WindowsFormsApplication11
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e) // As i understood, here we configure where i data will be shown,
// trying to get it on TextBox1
{
SerialPort sp = (SerialPort)sender;
richTextBox1.Text += sp.ReadExisting() + "\n";
}
private void button1_Click(object sender, EventArgs e) // There are a main actions, first i receive data then send data by a click.
{
serialPort1.Write("\u0001");
serialPort1.Close();
System.ComponentModel.IContainer components = new System.ComponentModel.Container(); //
serialPort1 = new System.IO.Ports.SerialPort(components);
serialPort1.PortName = "COM4";
serialPort1.BaudRate = 9600;
serialPort1.DtrEnable = true;
serialPort1.Open();
serialPort1.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
}
}
}
The serial port is on a different thread than the UI. So when you receive a character, as you haven't invoked the UI, you get an exception and the UI is not updated.
Invoke the UI first in your DataReceivedHandler. You could do something like that:
public static class ControlExt
{
public static void InvokeChecked(this Control control, Action method)
{
try
{
if (control.InvokeRequired)
{
control.Invoke(method);
}
else
{
method();
}
}
catch { }
}
}
public partial class Form1 : Form
{
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
this.InvokeChecked(delegate
{
richTextBox1.Text += serialPort1.ReadExisting() + "\n";
richTextBox1.SelectionStart = Text.Length;
richTextBox1.ScrollToCaret();
});
}
}
Given the code below:
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 NetworksApi.TCP.CLIENT;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Form1 client;
public Form1()
{
InitializeComponent();
}
private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox3.Text!= "" &&textBox4.Text!="")
{
client = new Form1();
client.ClientName = textBox4.Text;
client.ServerIp = textBox3.Text;
client.Connect();
}
else
{
MessageBox.Show("Fill it completely");
}
}
private void button3_Click(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
System.Environment.Exit(System.Environment.ExitCode);
}
}
}
I get the following error message whenever I try to compile:
'WindowsFormsApplication1.Form1' does not contain a definition for
ClientName and no extension method 'ClientName' accepting a first
argument of type.
Do you have any idea on how to fix this?
There is no ClientName property on a Windows Form class. However, since you are inheriting from Form, you can add one. But that doesn't make sense either. Are you sure you want a variable of type Form1 to have properties for ClientName, ServerIP, and a method for Connect()? Much more likely you want either some other pre-existing class or to make your own.
public class ClientService
{
public string ClientName {get; set;}
public string ServerIp {get; set;}
public void Connect()
{
//logic here
}
}
And change your UI logic to
if (!String.IsNullOrEmpty(textBox3.Text) && !String.IsNullOrEmpty(textBox4.Text))
{
var client = new ClientService();
client.ClientName = textBox4.Text;
client.ServerIp = textBox3.Text;
client.Connect();
}
else
{
MessageBox.Show("Fill it completely");
}
This is the documentation for the Form class in .NET: https://msdn.microsoft.com/en-us/library/system.windows.forms.form(v=vs.110).aspx
Notice there's no member for ClientName listed. You cannot reference it because it doesn't exist.
I need to update an windows form label with a string received from the serial port. I've got two problems with the code I made already.
Because the reading of the serial port needs another thread I use a delegate method to update the label text.
The first problem is that the form window wont open when I start the program (it does open when I don't call initSerialPort() in Form1_Load()).
The second problem is that it seems like it doesn't reach Debug.Write(message) when I call _self.SetText(message) in Read(). When I comment out _self.SetText(message) it does log the message but also doesn't open the form window because initSerialPort() is called in Form1_Load()
I'm kind of an noob with C#, just so you know ;)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;
using System.Diagnostics;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
delegate void SetTextCallback(string text);
private static SerialPort _serialPort;
private static Boolean _continue;
private static StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
private static Thread readThread = new Thread(Read);
private static Form1 _self;
private static Label _lbl;
public Form1()
{
InitializeComponent();
_self = this;
_lbl = label1;
}
private void Form1_Load(object sender, EventArgs e)
{
initSerialPort();
}
public void setMessage(string mes)
{
label1.Text = mes;
}
private static void initSerialPort()
{
// Create a new SerialPort object with default settings.
_serialPort = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
// Set the read/write timeouts
_serialPort.ReadTimeout = 500;
_serialPort.WriteTimeout = 500;
_serialPort.Open();
_continue = true;
readThread.Start();
readThread.Join();
_serialPort.Close();
_serialPort = null;
}
public static void Read()
{
Debug.Write("testread");
while (_continue)
{
try
{
String message = _serialPort.ReadLine();
_self.SetText(message);
Debug.Write(message);
}
catch (TimeoutException) { }
}
}
private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.label1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.label1.Text = text;
}
}
}
}
Don't Join to the new thread you create. That is going to block the thread until your Read method completes, which means it never completes.