Im currently trying to make an instant messaging application.
There is a client and a server. the server works perfectly but for some reason when I call a certain function to update the UI the TextBox doesn't get text added to it.
Below is an example of my code - The Update UI is called from a different form in my applicaiton:
public ChatWindow()
{
InitializeComponent();
Thread timerThread = new Thread(Main.ReceiveLoop);
timerThread.Start();
}
private void txtChatLog_TextChanged(object sender, EventArgs e)
{
}
private void btnSendMessage_Click(object sender, EventArgs e)
{
string clientReply = txtReply.Text;
string Message = "ClientMsg§" + clientReply;
var time = DateTime.Now;
txtChatLog.AppendText($"{time} client: {clientReply}");
txtChatLog.AppendText(Environment.NewLine);
Main main = new Main();
main.ChatResponse(Message);
txtReply.Text = "";
}
public void UpdateChatLog(string message)
{
var time = DateTime.Now;
string newMessage = message.Split('$')[1];
string messageToDisplay = $"{time} Server: {newMessage}";
MessageBox.Show(messageToDisplay);
txtChatLog.AppendText(messageToDisplay);
txtChatLog.AppendText(Environment.NewLine);
}
private void ChatWindow_Load(object sender, EventArgs e)
{
}
The client is defiantly receiving the message from the server as I checked with a messagebox.show();
Also when the send message button is pressed the rich textbox is updated. But for some reason it just wont update through the UpdateChatLog method.
Any Help would be really appreciated.
Thankyou advance!
From code which you have pasted, you don`t call UpdateChatLog method.
Try to add UpdateChatLog(message); to btnSendMessage_Click method.
Try refreshing textbox : txtChatLog.Refresh()
Related
i am making web browser in windows form by using c# where i can set values of loaded html's input fields automatically by clicking on button. when i simply put code in click event of button its work fine`
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Document.GetElementById("username").SetAttribute("value","admin");
webBrowser1.Document.GetElementById("password").SetAttribute("value","12345");
}`
but when i try to this via threading it gives me error
Specified cast is not valid?
private void button1_Click(object sender, EventArgs e)
{
Thread thread1 = new Thread(new ThreadStart(setvalues));
thread1.Start();
}
void setvalues()
{
webBrowser1.Document.GetElementById("username").SetAttribute("value","admin");
webBrowser1.Document.GetElementById("password").SetAttribute("value","12345");
Thread.Sleep(8000);
}
}
where i am doing mistake in code ? any error? am beginner i need help
You can't access forms controls in a separate thread. Try this in setvalues():
Invoke((Action)(() => {
webBrowser1.Document.GetElementById("username").SetAttribute("value","admin");
webBrowser1.Document.GetElementById("password").SetAttribute("value","12345");
}));
I have looked at various answers, i have googled my nut off for a good few hours now, and still cant seem to get this to work.
I am trying to update a textbox on a form. i have simplified the code i am using in the hope that it was just something i was adding unnecessarily, but still cant get it to work.
I know the text is being passed to the textbox and stored in the box, but it will not display in the actual box.
In form one (Form_DMM);
private void BtnTest_Click(object sender, EventArgs e)
{
ErrorHandling EH = new ErrorHandling();
EH.updatetbtest();
}
in separate class;
public void updatetbtest()
{
string FailedMessagePB = "Test Message" + "\n";
Form_DMM FormDMM = new Form_DMM();
FormDMM.TextBoxAppend(FailedMessagePB);
FormDMM.TextBoxAppend2 = FailedMessagePB;
FormDMM = null;
}
passed back to form one;
public void TextBoxAppend(string WriteMessage)
{
TB_Issues.AppendText(WriteMessage + "\n");
System.Windows.Forms.Application.DoEvents();
TB_Issues.Invalidate();
TB_Issues.Update();
TB_Issues.Refresh();
MessageBox.Show(TB_Issues.Text);
}
public string TextBoxAppend2
{
get
{
return TB_Issues.Text;
}
set
{
TB_Issues.Text = TB_Issues.Text + value + "\n";
System.Windows.Forms.Application.DoEvents();
TB_Issues.Invalidate();
TB_Issues.Update();
TB_Issues.Refresh();
MessageBox.Show(TB_Issues.Text);
}
}
As you can see i have two separate attempts at updating the textbox, neither of which will display the test message in the textbox, but the messagebox that pops up will show the test message. it will even show the double test message from the TB_Issues.AppendText().
Can someone please help and tell me where i'm going wrong. This is driving me insane!
you can use like this
public void updatetbtest(Form_DMM FormDMM)
{
string FailedMessagePB = "Test Message" + "\n";
FormDMM.TextBoxAppend(FailedMessagePB);
FormDMM.TextBoxAppend2 = FailedMessagePB;
}
and in your buttontest_Click
private void BtnTest_Click(object sender, EventArgs e)
{
ErrorHandling EH = new ErrorHandling();
EH.updatetbtest(this);
}
From the problem posted the error is in the ErrorHandling class.
In the method updatetbtest you create a new Insance of the form object. So you create a separate form object, change the text and then you lose any reference because you set the variable to null. The message box is displayed because the TextBoxAppend method is called and the messagebox is a separate instance. The new instance you create is never displayed.
You have to hand over your calling form instance to the updatetbtest method.
Something like this:
private void BtnTest_Click(object sender, EventArgs e)
{
ErrorHandling EH = new ErrorHandling();
EH.updatetbtest(this);
}
public void updatetbtest(Form_DMM form)
{
string FailedMessagePB = "Test Message" + "\n";
form.TextBoxAppend(FailedMessagePB);
}
You try to create new form. But you must use existing.
You can pass existing form calling EH.updatetbtest(this);
And of course adding parameter to declaration like updatetbtest(Form_DMM FormDMM). And delete declaration and new in the function body.
And dammit, I'm getting frustrated. I'm doing my first mini-game, I think the name in english is Tic-Tac-Toe, and so, I have a main menu where I choose the option 2 players, then i get a inputbox asking for the 2 players names (i used a visual basic reference) and store it in 2 variables that i send to my constructor (?). I'm potuguese so I don't really know how you guys call it, but I'll show you the code.
So, in the first Form, I have:
private void doisJogadoresToolStripMenuItem_Click(object sender, EventArgs e)
{
jogadorTempUm = Microsoft.VisualBasic.Interaction.InputBox("Jogador 1");
jogadorTempDois = Microsoft.VisualBasic.Interaction.InputBox("Jogador 2");
jog = new DoisJogadores(jogadorTempUm, jogadorTempDois);
DoisJogadores novoDois = new DoisJogadores();
novoDois.ShowDialog();
}
That gets sent to the other Form:
public DoisJogadores(string teste1, string teste2)
{
jogador1 = teste1;
jogador2 = teste2;
}
And I save it in the class:
private string jogador1
private string jogador2
And the values get saved there. But I tried to place them in a textbox to show the players names and it just goes blank.
Anyone that can help me?
You are showing not that form which you are passing values to. Here is a fix
private void doisJogadoresToolStripMenuItem_Click(object sender, EventArgs e)
{
jogadorTempUm = Microsoft.VisualBasic.Interaction.InputBox("Jogador 1");
jogadorTempDois = Microsoft.VisualBasic.Interaction.InputBox("Jogador 2");
// pass values to form you are creating
DoisJogadores novoDois =
new DoisJogadores(jogadorTempUm, jogadorTempDois);
novoDois.ShowDialog();
}
Also make sure you are assigning jogador1 and jogador2 to textboxes. E.g. you can subscribe to form's load event in DoisJogadores form:
private void DoisJogadores_Load(object sender, EventArgs e)
{
textbox1.Text = jogador1;
textbox2.Text = jogador2;
}
I have a web browser control in a child form and it captures some data from the displayed web page. I need to use this data in the browser form to be passed to the parent form, but without having to start a new instance of it as it's already open.
The parent form needs to recieve this data from the browser and update some textboxes with the variables set from parsing the page.
I have this in the parent form:
private void browserToolStripMenuItem_Click(object sender, EventArgs e)
{
System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(RunBrowser));
t.SetApartmentState(ApartmentState.STA);
t.Start();
}
public static void RunBrowser()
{
Application.Run(new BrowserForm());
}
I have tried many things in the child form, but I cannot get it to work at all. The best I can get is to pass a variable to the parent and display it via a MessageBox, but it refuses to update the TextBox at all.
BTW I have been trying to solve this now for nearly 12 hours straight, that is the only reason I am asking here.
I solved it finally, but not in an ideal way.
In the parent I open it like this:
private void BrowserToolStripButton_Click(object sender, EventArgs e)
{
using (BrowserForm form = new BrowserForm())
{
form.ShowDialog(this);
}
}
I have this method in the Parent also:
public void SendStringsToParent(string s, string s2, string s3)
{
textBox.Text = s;
textBox2.Text = s2;
textBox3.Text = s3;
}
Then in the Child (Browser) form I have this:
private void Button1_Click(object sender, EventArgs e)
{
string stringToSend = "sending these";
string stringToSend2 = "strings to the";
string stringToSend3 = "parent form";
MainForm parent = (MainForm)this.Owner;
parent.SendStringsToParent(stringToSend, stringToSend2, stringToSend3);
}
This is working, although I have had to work around the fact that it is a modal form. If there is any way to do this this while still having full control over both forms, I would love to hear from someone.
Please check For this Method..
But if you are passing private data this not will be helpful.
In Your Browser page:
protected void Button1_Click(object sender, EventArgs e)
{
string modulename = "Agile Software Development ";
string url;
url = "page2.aspx?module=" +modulename;
Response.Redirect(url);
}
In Your Parent page
string RetrievedValue;protected void Page_Load(object sender, EventArgs e)
{
this.TextBox1.Text = Request.QueryString["module"];
// RetrievedValue = this.TextBox1.Text;
}
I am using YoutubeExtractor's dll.. videoDownloader_ProgressChanged and videoDownloader_DownloadFinished events are working in console application but in winform, it doesnt work.. I dont understand why..
private void btnStart_Click(object sender, EventArgs e)
{
string link = textBox1.Text;
start(link);
}
static void start(string link)
{
IEnumerable<VideoInfo> videoInfos = DownloadUrlResolver.GetDownloadUrls(link);
DownloadVideo(videoInfos);
}
private static void DownloadVideo(IEnumerable<VideoInfo> videoInfos)
{
VideoInfo video = videoInfos
.First(info => info.VideoFormat == VideoFormat.Standard360);
var videoDownloader = new VideoDownloader(video, Path.Combine("C:/Downloads", video.Title + video.VideoExtension));
videoDownloader.DownloadFinished += new EventHandler(videoDownloader_DownloadFinished);
videoDownloader.ProgressChanged += new EventHandler<ProgressEventArgs>(videoDownloader_ProgressChanged);
videoDownloader.Execute();
}
static void videoDownloader_ProgressChanged(object sender, ProgressEventArgs e)
{
//some code..
}
static void videoDownloader_DownloadFinished(object sender, EventArgs e)
{
//some code..
}
my second question is, I want to access a form control in a static videoDownloader_ProgressChanged event. e.ProgressPercentage paramter gives me percent of video downloaded. I want to show it in label. But I cant access label because of static event.. I tried to use delegate but nothing changed..
Please modify both Start() and DownloadVideo() routines to instance methods. Remove 'static' keyword from them and event handlers as well.
Thread off 'videoDownloader.Execute()' and BeginInvoke() in the changed/finished handlers.
Don't call methods that take forever, (in computer terms), in GUI event handlers. If it takes more than about 50ms, thread it off. Any net thingy, eg. something with 'YouTube' in it, will take longer than that just to establish a connection!