Implement "Enter To Send"

This commit is contained in:
Alan Moon 2025-06-20 16:46:19 -07:00
parent 4ce4fb963e
commit 8b4834822e
4 changed files with 28 additions and 11 deletions

View File

@ -41,6 +41,7 @@
rtxtChatbox.Size = new Size(512, 54);
rtxtChatbox.TabIndex = 1;
rtxtChatbox.Text = "";
rtxtChatbox.KeyDown += rtxtChatbox_KeyDown;
//
// btnSend
//
@ -63,7 +64,7 @@
rtxtChat.TabIndex = 3;
rtxtChat.Text = "";
//
// frmChat
// Chat
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
@ -75,7 +76,7 @@
FormBorderStyle = FormBorderStyle.FixedSingle;
Icon = (Icon)resources.GetObject("$this.Icon");
MaximizeBox = false;
Name = "frmChat";
Name = "Chat";
StartPosition = FormStartPosition.CenterScreen;
Text = "QtC.NET Client - Chat Room";
FormClosing += frmChat_FormClosing;

View File

@ -50,7 +50,7 @@ namespace qtc_net_client_2.Forms
private async void btnSend_Click(object sender, EventArgs e)
{
if(!string.IsNullOrWhiteSpace(rtxtChatbox.Text))
if (!string.IsNullOrWhiteSpace(rtxtChatbox.Text))
{
// construct message
QtCNETAPI.Models.Message message = new() { Content = rtxtChatbox.Text };
@ -62,6 +62,12 @@ namespace qtc_net_client_2.Forms
}
}
private void rtxtChatbox_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
btnSend_Click(sender, e);
}
private void _gatewayService_OnServerMessageReceived(object? sender, EventArgs e)
{
var msgEventArgs = (ServerMessageEventArgs)e;

View File

@ -67,6 +67,7 @@
rtxtChatbox.Size = new Size(511, 54);
rtxtChatbox.TabIndex = 4;
rtxtChatbox.Text = "";
rtxtChatbox.KeyDown += rtxtChatbox_KeyPress;
//
// lblUsername
//
@ -79,7 +80,7 @@
lblUsername.TabIndex = 7;
lblUsername.Text = "Username";
//
// frmDirectMessage
// DirectMessage
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
@ -95,7 +96,7 @@
Icon = (Icon)resources.GetObject("$this.Icon");
Margin = new Padding(4, 3, 4, 3);
MaximizeBox = false;
Name = "frmDirectMessage";
Name = "DirectMessage";
StartPosition = FormStartPosition.CenterScreen;
Text = "QtC.NET Client - Direct Message With ${USER}";
Load += frmDirectMessage_Load;

View File

@ -51,7 +51,7 @@ namespace qtc_net_client_2.Forms
private async void btnSend_Click(object sender, EventArgs e)
{
if(InvokeRequired)
if (InvokeRequired)
{
await Invoke(async delegate ()
{
@ -76,22 +76,31 @@ namespace qtc_net_client_2.Forms
}
}
private void rtxtChatbox_KeyPress(object sender, KeyEventArgs e)
{
// mimick clicking send
if(e.KeyCode == Keys.Enter)
btnSend_Click(sender, e);
}
private void Messages_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
if(e.NewItems != null)
if (e.NewItems != null && e.NewItems.Count > 0)
{
if (InvokeRequired)
{
Invoke(delegate ()
{
rtxtChat.Text += e.NewItems.Cast<string>().FirstOrDefault();
AudioService.PlaySoundEffect("sndMessage");
var msg = e.NewItems.Cast<string>().FirstOrDefault();
rtxtChat.Text += msg;
if (!msg!.Contains(_apiService.CurrentUser.Username)) AudioService.PlaySoundEffect("sndMessage");
});
}
else
{
rtxtChat.Text += e.NewItems.Cast<string>().FirstOrDefault();
AudioService.PlaySoundEffect("sndMessage");
var msg = e.NewItems.Cast<string>().FirstOrDefault();
rtxtChat.Text += msg;
if (!msg!.Contains(_apiService.CurrentUser.Username)) AudioService.PlaySoundEffect("sndMessage");
}
}
}