C# replace do..while with for loop - c#

I usually know how to replace a do..while loop with a for loop, but in this case variable check2 is used 2 times for different loops with got me confused on how to correctly replace it with for loop.
public static void RelayChat(ref int con, ref string Message)
{
string temp_SubName = "relaychat";
WriteSub(ref temp_SubName);
int check2 = 0;
if (Message.Length == 0) return;
var check = UserConToCheck(con);
if (CheckMute(get_UserName(check)) || get_UserChat(check) > 4) return;
if (get_UserChat(check) < 8) set_UserChat(check, get_UserChat(check) + 1);
if (Message.Length > 100) Message = Message.Substring(0, 99);
if (Message[0] == '!')
{
if (UserCommand(con, Message.Substring(1).Split(" ".ToCharArray()))) return;
}
if (PlayerAdapter.get_Reference(check).get_IsAdmin(1))
{
if (Message.Substring(0, 1) == "#")
{
string[] temp_cmds = Message.Substring(1).Split(",".ToCharArray());
admin.AdminCommand(PlayerAdapter.get_Reference(check), ref temp_cmds);
return;
}
}
if (Message.StartsWith(":g::", true, null))
{
do
{
check2++;
var guild1 = get_UserGuild(check);
var guild2 = get_UserGuild(check2);
if (guild1 == null || guild2 == null || guild1.ToLower() != guild2.ToLower()) continue;
var thisuser = get_UserName(check);
var targetuser = get_UserName(check2);
var found = targetuser != null && thisuser != null &&
IgnoreUserList.ContainsKey(targetuser) &&
IgnoreUserList[targetuser].Contains(thisuser);
if (found == false)
{
Winsock.PrivMsg(get_UserCon(check2), "14,1,[ " + get_UserGuild(check2) + " ] Chat - " + get_UserName(check) + " : " + Message.Substring(4));
}
} while (check2 != UserMax);
return;
}
if (check <= 0) return;
do
{
check2++;
bool found = false;
var user = get_UserName(check2);
if (user != null && IgnoreUserList.ContainsKey(user))
{
found = Convert.ToBoolean(IgnoreUserList[get_UserName(check2)].Contains(get_UserName(check)));
}
if (found) return;
if (get_UserLanded(check2) != get_UserLanded(check)) return;
if (get_UserLanded(check2))
{
if (get_UserInBar(check2) == false &&
get_UserInUniversalBar(check2) == false) return;
if (get_UserInUniversalBar(check2) && get_UserInUniversalBar(check))
{
Winsock.PrivMsg(get_UserCon(check2), "13,0,[ " + get_UserName(check) + " ] : " + Message);
}
else if (get_UserInBar(check2) && get_UserInBar(check))
{
if (get_UserLastPlanet(check2) !=
get_UserLastPlanet(check)) return;
Winsock.PrivMsg(get_UserCon(check2), "13,0,[ " + get_UserName(check) + " ] : " + Message);
}
}
else if (get_UserLanded(check2) == false)
{
if (get_UserSector(check2) != get_UserSector(check)) return;
if (get_UserZone(check2) != get_UserZone(check)) return;
Winsock.PrivMsg(get_UserCon(check2), "13,0,[ " + get_UserName(check) + " ] : " + Message);
}
} while (check2 != UserMax);
}
I know that the function might be a lot complex, but please ignore that fact and focus on just the do..while loops. Thank you!

Just declare the for loop to use the existing variable.
int check2 = 0;
if (Message.StartsWith(":g::", true, null))
{
for (; check2 < UserMax; check2++)
{
// Do stuff
}
return;
}
if (check <= 0) return;
for (; check2 < 200; check2++)
{
// Do stuff
}

Related

Cannot manage to add addresses to list with PlinApi

Here is my code
do
{
tLINError = HardwareManage.readMessages();
if (tLINError.Equals(TLINError.errOK))
{
TLINRcvMsg tLINRcvMsg = HardwareManage.GetTLINMsg();
if (tLINRcvMsg.Data != null && tLINRcvMsg.Type.Equals(TLINMsgType.mstStandard))
{
byteFrame = tLINRcvMsg.FrameId;
if (byteFrame == 0x3D && tLINRcvMsg.Data[0] != 0x7F
&& tLINRcvMsg.ChecksumType.Equals(TLINChecksumType.cstEnhanced)
)
{
///*
if (tLINRcvMsg.Data[0] == pMsg.Data[0])
{
int k;
bool verifica = true;
for (k = 0; k < LAST_BYTE_RESPONSE.Length && verifica; k++)
{
if (tLINRcvMsg.Data[k + 1] != LAST_BYTE_RESPONSE[k])
{
verifica = false;
}
}
if (verifica)
{
listAddress.Add(hexValue);
}
}
//*/
}
}
}
} while (tLINError.Equals(TLINError.errOK) && byteFrame != 0x3D);
My PLin-Usb Api is of Peak-System (developed in 2019).
I cannot manage to insert the address found in the list, but with the debug in that point i manage to do it. Any suggestion?

Comma misplaced on RightToLeft label

I'm trying to partially recreate the Windows 10 calculator but have run into some trouble. I have a code that looks like this for when the user enters a comma:
if (label_number.Text != null)
{
label_number.Text += ",";
}
else
{
label_number.Text = "0,";
}
So if the user enters the comma while the string is null I want the string to read out "0," but it only adds the "," to the label. And when I add a comma to a number, it comes out like this ",07839" instead of "07839,". Why is this happening? Like the title says my label is RightToLeft, but this also happens when that option is false.
Edit, here is the entire MouseClick EventHandler
'''
private void MouseClickEvent(object sender, MouseEventArgs e)
{
var obj = (sender as Label);
// Left click
if (e.Button == MouseButtons.Left)
{
if (obj.Name == "button_quit") // Quit
{
Application.Exit();
}
else if (obj.Name == "button_minimize") // Minimize
{
this.WindowState = FormWindowState.Minimized;
}
else if (obj.Name == "button_c") // Reset calc
{
label_input.Text = String.Empty;
label_number.Text = String.Empty;
}
else if (obj.Name == "button_ce") // Clear number
{
label_number.Text = String.Empty;
}
else if (obj.Name == "button_delete") // Delete
{
if (label_number.Text.Length == 1)
{
label_number.Text = String.Empty;
}
else if (label_number.Text.Length > 1)
{
label_number.Text = label_number.Text.Remove(label_number.Text.Length - 1, 1);
}
}
else if (obj.Name == "button_divide" || obj.Name == "button_multiply" || obj.Name == "button_subtract" || obj.Name == "button_add") // / * - +
{
switch (obj.Name)
{
case "button_divide":
break;
case "button_multiply":
break;
case "button_subtract":
break;
case "button_add":
break;
}
}
else if (obj.Name == "button_comma") // Comma
{
if (label_number.Text != String.Empty)
{
label_number.Text += ",";
}
else
{
label_number.Text = "0,";
}
}
else if (obj.Name == "button_equals") // Calculate equation
{
}
else // Number input
{
if (label_number.Text.Length < 13)
{
if (obj.Name != "button_0")
{
if (label_number.Text.Length == 1 && label_number.Text == "0")
{
label_number.Text = obj.Name[obj.Name.Length - 1].ToString();
}
else
{
label_number.Text += obj.Name[obj.Name.Length - 1].ToString();
}
}
else
{
label_number.Text += obj.Name[obj.Name.Length - 1].ToString();
}
}
}
}
}
'''

How can I avoid of using same iterator while moving to another-if statement in same loop?

I'm trying to use continue; but looks like - incorrect;
Every 3 times I have same "i" value. How can I skip iterator when running if(currentShiftCD != 3 & currentShiftAB == 3) and if(currentShiftDC != 3 & currentShiftBA == 3) for the first time after switching bools firsThreeDays and secondThreeDays
I want output to startlike: 1,2,3,4,5,6..19 but have with same values:
for (int i = 1; i <20; i++)
{
if (firstThreeDays)
{
if (currentShiftAB != 3)
{
currentShiftAB++;
Console.WriteLine(i + " A-B");
}
if (currentShiftCD != 3 & currentShiftAB == 3)
{
currentShiftCD++;
Console.WriteLine(i + " C-D");
}
if (currentShiftAB == 3 & currentShiftCD == 3)
{
firstThreeDays = false;
secondThreeDays = true;
currentShiftAB = 0;
currentShiftCD = 0;
continue;
}
}
if (secondThreeDays)
{
if (currentShiftBA != 3)
{
currentShiftBA++;
Console.WriteLine(i + " B-A");
}
if (currentShiftDC != 3 & currentShiftBA == 3)
{
currentShiftDC++;
Console.WriteLine(i + " D-C");
}
if (currentShiftBA == 3 & currentShiftDC == 3)
{
secondThreeDays = false;
firstThreeDays = true;
currentShiftBA = 0;
currentShiftDC = 0;
continue;
}
}
}
Console.ReadLine();
The problem is in the sequencing of your first two if statements, inside the firstThreeDays as well as secondThreeDays. Use if, else-if, else-if construct.
// Suppose your loop has executed once. Now, currentShiftAB is 2.
if (currentShiftAB != 3) // currentShiftAB is not 3.It enters this if.
{
currentShiftAB++; // currentshiftAB is now 3.
Console.WriteLine(i + " A-B"); //It prints 3 A-B
}
// Now, currentShiftAB is 3 but currentShiftCD is not 3.
// your code enters this if condition as well at the same time.
// That's why you are getting duplicate output for i=3.
if (currentShiftCD != 3 & currentShiftAB == 3)
{
currentShiftCD++;
Console.WriteLine(i + " C-D");
}
I wouldn't use continue to "solve" your problem just use the else clause with your first if
for (int i = 1; i <20; i++)
{
if (firstThreeDays)
{
if (currentShiftAB != 3)
{
currentShiftAB++;
Console.WriteLine(i + " A-B");
}
if (currentShiftCD != 3 & currentShiftAB == 3)
{
currentShiftCD++;
Console.WriteLine(i + " C-D");
}
if (currentShiftAB == 3 & currentShiftCD == 3)
{
firstThreeDays = false;
secondThreeDays = true;
currentShiftAB = 0;
currentShiftCD = 0;
}
}
else if (secondThreeDays)
{
if (currentShiftBA != 3)
{
currentShiftBA++;
Console.WriteLine(i + " B-A");
}
if (currentShiftDC != 3 & currentShiftBA == 3)
{
currentShiftDC++;
Console.WriteLine(i + " D-C");
}
if (currentShiftBA == 3 & currentShiftDC == 3)
{
secondThreeDays = false;
firstThreeDays = true;
currentShiftBA = 0;
currentShiftDC = 0;
}
}
}
Console.ReadLine();
This will guarantee that the if (secondThreeDays) block doesn't get run in the same iteration as the if (firstThreeDays) block.

Get name of players and numbers which players has "shot"

I am working on a Windows form, I have two lists: one for the names of players, and one for the numbers which the players have guessed. With this code I get in listbox name of all players, but the problem is I can get only number from last player which are same with numbers which are generated from system and the numbers which player has guessed, but I need to know how to get numbers which are same from two lists for every players, example:
Player 1:
2, 3 , 28 // has three numbers same from the lists
Player 2:
1
etc..
I wrote this code:
namespace LojaFatitForm
{
public partial class Loja_e_Fatit : Form
{
public Loja_e_Fatit()
{
InitializeComponent();
}
int player = 1;
int Guessed=0;
int ticket = 1;
string t = "-------------------------------------------------------------------------------------";
List<string> nameOfPlayers = new List<string>();
List<string> choosedNumbers = new List<string>();
List<string> winningNumbers = new List<string>();
List<string> guessedNumbers = new List<string>();
private void Loja_e_Fatit_Load(object sender, EventArgs e)
{ }
private void bntGenerateNr_Click(object sender, EventArgs e)
{
int Mosha;
Int32.TryParse(txtMosha.Text, out Mosha);
if (Mosha < 18)
{
MessageBox.Show("Lojtari duhet te jete ne moshen madhore!");
txtMosha.Text = "";
}
if (string.IsNullOrWhiteSpace(txtMosha.Text) || string.IsNullOrWhiteSpace(txtEmri.Text) || string.IsNullOrWhiteSpace(txtMbiemri.Text) || string.IsNullOrWhiteSpace(txtQyteti.Text)
|| string.IsNullOrWhiteSpace(txtNr1.Text) || string.IsNullOrWhiteSpace(txtNr2.Text) || string.IsNullOrWhiteSpace(txtNr3.Text) || string.IsNullOrWhiteSpace(txtNr4.Text)
|| string.IsNullOrWhiteSpace(txtNr5.Text)|| string.IsNullOrWhiteSpace(txtNr6.Text) || string.IsNullOrWhiteSpace(txtNr7.Text))
{
MessageBox.Show("Ju lutemi jepni te dhenat/Numrat tuaja!");
}
else
if(txtNr2==txtNr1 || txtNr2==txtNr3 )
{
MessageBox.Show("Ky numur edhe dhene, jepni nje numer tjeter!");
txtNr2.Text = "";
}
else
if (txtNr3 == txtNr2 || txtNr3==txtNr1)
{
MessageBox.Show("Ky numur edhe dhene, jepni nje numer tjeter!");
txtNr3.Text = "";
}else
if (txtNr4 == txtNr1 || txtNr4==txtNr3 || txtNr4==txtNr2)
{
MessageBox.Show("Ky numur edhe dhene, jepni nje numer tjeter!");
txtNr4.Text = "";
}else
if (txtNr5 == txtNr1 || txtNr5==txtNr2 || txtNr5==txtNr3 || txtNr5==txtNr4)
{
MessageBox.Show("Ky numur edhe dhene, jepni nje numer tjeter!");
txtNr5.Text = "";
}
else
if (txtNr6 == txtNr1 || txtNr6==txtNr2 || txtNr6==txtNr3 || txtNr6==txtNr4 || txtNr6==txtNr5)
{
MessageBox.Show("Ky numur edhe dhene, jepni nje numer tjeter!");
txtNr6.Text = "";
}else
if (txtNr7 == txtNr1 || txtNr7==txtNr2 || txtNr7==txtNr3 || txtNr7==txtNr4 || txtNr7==txtNr5 || txtNr7==txtNr6)
{
MessageBox.Show("Ky numur edhe dhene, jepni nje numer tjeter!");
txtNr7.Text = "";
}
else
{
Random r = new Random();
int Nr1, Nr2, Nr3, Nr4, Nr5, Nr6, Nr7;
Int32.TryParse(txtNr1Gen.Text, out Nr1);
Nr1 = r.Next(1, 39);
txtNr1Gen.Text = Nr1.ToString();
if ((txtNr1Gen.Text == txtNr1.Text) || (txtNr1Gen.Text == txtNr2.Text) || (txtNr1Gen.Text == txtNr3.Text) ||
(txtNr1Gen.Text == txtNr4.Text) || (txtNr1Gen.Text == txtNr5.Text) || (txtNr1Gen.Text == txtNr6.Text) || (txtNr1Gen.Text == txtNr7.Text))
{
winningNumbers.Add(txtNr1Gen.Text);
Guessed++;
}
Int32.TryParse(txtNr2Gen.Text, out Nr2);
nr2: Nr2 = r.Next(1, 39);
if (Nr2 == Nr1)
{
goto nr2;
}
else
{
txtNr2Gen.Text = Nr2.ToString();
if ((txtNr2Gen.Text == txtNr1.Text) || (txtNr2Gen.Text == txtNr2.Text) || (txtNr2Gen.Text == txtNr3.Text) ||
(txtNr2Gen.Text == txtNr4.Text) || (txtNr2Gen.Text == txtNr5.Text) || (txtNr2Gen.Text == txtNr6.Text) || (txtNr2Gen.Text == txtNr7.Text))
{
winningNumbers.Add(txtNr2Gen.Text);
Guessed++;
}
}
Int32.TryParse(txtNr3Gen.Text, out Nr3);
nr3: Nr3 = r.Next(1, 39);
if (Nr3 == Nr2 || Nr3 == Nr1)
{
goto nr3;
}
else
{
txtNr3Gen.Text = Nr3.ToString();
if ((txtNr3Gen.Text == txtNr1.Text) || (txtNr3Gen.Text == txtNr2.Text) || (txtNr3Gen.Text == txtNr3.Text) ||
(txtNr3Gen.Text == txtNr4.Text) || (txtNr3Gen.Text == txtNr5.Text) || (txtNr3Gen.Text == txtNr6.Text) || (txtNr3Gen.Text == txtNr7.Text))
{
winningNumbers.Add(txtNr3Gen.Text);
Guessed++;
}
}
Int32.TryParse(txtNr4Gen.Text, out Nr4);
nr4: Nr4 = r.Next(1, 39);
if (Nr4 == Nr3 || Nr4 == Nr2 || Nr4 == Nr1)
{
goto nr4;
}
else
{
txtNr4Gen.Text = Nr4.ToString();
if ((txtNr4Gen.Text == txtNr1.Text) || (txtNr4Gen.Text == txtNr2.Text) || (txtNr4Gen.Text == txtNr3.Text) ||
(txtNr4Gen.Text == txtNr4.Text) || (txtNr4Gen.Text == txtNr5.Text) || (txtNr4Gen.Text == txtNr6.Text) || (txtNr4Gen.Text == txtNr7.Text))
{
winningNumbers.Add(txtNr4Gen.Text);
Guessed++;
}
}
Int32.TryParse(txtNr5Gen.Text, out Nr5);
nr5: Nr5 = r.Next(1, 39);
if (Nr5 == Nr4 || Nr5 == Nr3 || Nr5 == Nr2 || Nr5 == Nr1)
{
goto nr5;
}
else
{
txtNr5Gen.Text = Nr5.ToString();
if ((txtNr5Gen.Text == txtNr1.Text) || (txtNr5Gen.Text == txtNr2.Text) || (txtNr5Gen.Text == txtNr3.Text) ||
(txtNr5Gen.Text == txtNr4.Text) || (txtNr5Gen.Text == txtNr5.Text) || (txtNr5Gen.Text == txtNr6.Text) || (txtNr5Gen.Text == txtNr7.Text))
{
winningNumbers.Add(txtNr5Gen.Text);
Guessed++;
}
}
Int32.TryParse(txtNr6Gen.Text, out Nr6);
nr6: Nr6 = r.Next(1, 39);
if (Nr6 == Nr5 || Nr6 == Nr4 || Nr6 == Nr3 || Nr6 == Nr2 || Nr6 == Nr1)
{
goto nr6;
}
else
{
txtNr6Gen.Text = Nr6.ToString();
if ((txtNr6Gen.Text == txtNr1.Text) || (txtNr6Gen.Text == txtNr2.Text) || (txtNr6Gen.Text == txtNr3.Text) ||
(txtNr6Gen.Text == txtNr4.Text) || (txtNr6Gen.Text == txtNr5.Text) || (txtNr6Gen.Text == txtNr6.Text) || (txtNr6Gen.Text == txtNr7.Text))
{
winningNumbers.Add(txtNr6Gen.Text);
Guessed++;
}
}
Int32.TryParse(txtNr7Gen.Text, out Nr7);
nr7: Nr7 = r.Next(1, 39);
if (Nr7 == Nr6 || Nr7 == Nr5 || Nr7 == Nr4 || Nr7 == Nr3 || Nr7 == Nr2 || Nr7 == Nr1)
{
goto nr7;
}
else
{
txtNr7Gen.Text = Nr7.ToString();
if ((txtNr7Gen.Text == txtNr1.Text) || (txtNr7Gen.Text == txtNr2.Text) || (txtNr7Gen.Text == txtNr3.Text) ||
(txtNr7Gen.Text == txtNr4.Text) || (txtNr7Gen.Text == txtNr5.Text) || (txtNr7Gen.Text == txtNr6.Text) || (txtNr7Gen.Text == txtNr7.Text))
{
winningNumbers.Add(txtNr7Gen.Text);
Guessed++;
}
}
updateListbox("");
updateListbox("Random numbers generated are:" + txtNr1Gen.Text + " " + txtNr2Gen.Text + " " + txtNr3Gen.Text + " " + txtNr4Gen.Text + " " + txtNr5Gen.Text + " " + txtNr6Gen.Text + " " + txtNr7Gen.Text);
updateListbox(t);
}
bntGenerateNr.Enabled = false;
btnNewPlayer.Enabled = false;
btnSave.Enabled = false;
foreach (var i in choosedNumbers)
{
foreach (var j in winningNumbers)
{
if (i == j)
{
guessedNumbers.Add(i);
}
}
}
foreach (var i in nameOfPlayers)
{
if (Guessed < 1)
{
updateListbox("There are no winners in this round!");
}
else
{
updateListbox("Player: " + i + " has guessed " + Guessed + " Numbers: ");
updateListbox(string.Join(", ", guessedNumbers));
foreach (var j in guessedNumbers)
{
updateListbox(j);
}
}
}
}
private void updateListbox(string value)
{
Lista.Items.Add(value);
Lista.TabIndex = Lista.Items.Count - 1;
}
private void btnBack_Click(object sender, EventArgs e)
{
MainForm main = new MainForm();
this.Hide();
main.ShowDialog();
this.Close();
}
private void btnNewPlayer_Click(object sender, EventArgs e)
{
txtNr1.Text = null;
txtNr2.Text = null;
txtNr3.Text = null;
txtNr4.Text = null;
txtNr5.Text = null;
txtNr6.Text = null;
txtNr7.Text = null;
txtEmri.Text = "";
txtMbiemri.Text = "";
txtMosha.Text = "";
txtQyteti.Text = "";
txtId.Text = "";
player++;
ticket++;
}
private void btnSave_Click(object sender, EventArgs e)
{
updateListbox("Ticket: " + ticket);
// Validimet per numrat e dhene
if (string.IsNullOrWhiteSpace(txtNr1.Text) || string.IsNullOrWhiteSpace(txtNr2.Text) || string.IsNullOrWhiteSpace(txtNr3.Text) || string.IsNullOrWhiteSpace(txtNr4.Text)
|| string.IsNullOrWhiteSpace(txtNr5.Text) || string.IsNullOrWhiteSpace(txtNr6.Text) || string.IsNullOrWhiteSpace(txtNr7.Text))
{
MessageBox.Show("Give some number at any of 7 numbers");
}
else
if (txtNr1.Text == txtNr2.Text || txtNr1.Text == txtNr3.Text || txtNr1.Text == txtNr4.Text || txtNr1.Text == txtNr5.Text || txtNr1.Text == txtNr6.Text || txtNr1.Text == txtNr7.Text)
{
MessageBox.Show("Choose different number from the others at 1st!");
txtNr1.Text = "";
}
else
if (txtNr2.Text == txtNr3.Text || txtNr2.Text == txtNr4.Text || txtNr2.Text == txtNr5.Text || txtNr2.Text == txtNr6.Text || txtNr2.Text == txtNr7.Text)
{
MessageBox.Show("Choose different number from the others at 2nd!");
txtNr2.Text = "";
}
else
if (txtNr3.Text == txtNr4.Text || txtNr3.Text == txtNr5.Text || txtNr3.Text == txtNr6.Text || txtNr3.Text == txtNr7.Text)
{
MessageBox.Show("Choose different number from the others at 3d!");
txtNr3.Text = "";
}
else
if (txtNr4.Text == txtNr5.Text || txtNr4.Text == txtNr6.Text || txtNr4.Text == txtNr7.Text)
{
MessageBox.Show("Choose different number from the others at 4th!");
txtNr4.Text = "";
}
else
if (txtNr5.Text == txtNr6.Text || txtNr5.Text == txtNr7.Text)
{
MessageBox.Show("Choose different number from the others at 5th!");
txtNr5.Text = "";
}
else
if (txtNr6.Text == txtNr7.Text)
{
MessageBox.Show("Choose different number from the others at 6th!");
txtNr6.Text = "";
}
else
if (Convert.ToInt32(txtNr1.Text) < 1 || Convert.ToInt32(txtNr1.Text) > 39)
{
txtNr1.Text = "";
MessageBox.Show("Please write 1st numbers because is out of range!");
}
else
if (Convert.ToInt32(txtNr2.Text) < 1 || Convert.ToInt32(txtNr2.Text) > 39)
{
txtNr2.Text = "";
MessageBox.Show("Please write 2nd numbers because is out of range!!");
}
else
if (Convert.ToInt32(txtNr3.Text) < 1 || Convert.ToInt32(txtNr3.Text) > 39)
{
txtNr3.Text = "";
MessageBox.Show("Please write 3d numbers because is out of range!!");
}
else
if (Convert.ToInt32(txtNr4.Text) < 1 || Convert.ToInt32(txtNr4.Text) > 39)
{
txtNr4.Text = "";
MessageBox.Show("Please write 4th numbers because is out of range!!");
}
else
if (Convert.ToInt32(txtNr5.Text) < 1 || Convert.ToInt32(txtNr5.Text) > 39)
{
txtNr5.Text = "";
MessageBox.Show("Please write 5th numbers because is out of range!!");
}
else
if (Convert.ToInt32(txtNr6.Text) < 1 || Convert.ToInt32(txtNr6.Text) > 39)
{
txtNr6.Text = "";
MessageBox.Show("Please write 6th numbers because is out of range!!");
}
else
if ((Convert.ToInt32(txtNr7.Text) < 1 || Convert.ToInt32(txtNr7.Text) > 39))
{
txtNr7.Text = "";
MessageBox.Show("Please write 7th numbers because is out of range!!");
}
else
{
updateListbox("Player " + txtEmri.Text + " choosed :" + txtNr1.Text + " " + txtNr2.Text + " " + txtNr3.Text +
" " + txtNr4.Text + " " + txtNr5.Text + " " + txtNr6.Text + " " + txtNr7.Text);
choosedNumbers.Add(txtNr1.Text);
choosedNumbers.Add(txtNr2.Text);
choosedNumbers.Add(txtNr3.Text);
choosedNumbers.Add(txtNr4.Text);
choosedNumbers.Add(txtNr5.Text);
choosedNumbers.Add(txtNr6.Text);
choosedNumbers.Add(txtNr7.Text);
}
nameOfPlayers.Add(txtEmri.Text);
}
}
}
I am assuming, since we don't know what is the code of your updateListbox() method, that it replaces everything in the display box.
As such, you would need something like this:
foreach (var i in emrateLojtarve)
{
if (teQelluar < 1)
{
updateListbox("Ne kete gjiro nuk ka fituar asnje lojtar!");
}
else
{
updateListbox("Player: " + i + " ka te qelluara " + teQelluar + " Numra: ");
updateListbox(string.Join(", ", numrateQelluar));
}
}

Button unable to click when Thread.Sleep

I understand that when Thread.Sleep is being executed, the buttons on my GUI are not able to be clicked on.
Is there any other ways to delay my codes from flowing yet still able to click on my buttons on the GUI?
For example right now after my codes execute Thread.Sleep(10000); and within this 10 seconds i'm not able to click on my button1 event, is there anyway that I can still click on my button1 event within these 10 seconds?
private void displaydata_event2(object sender, EventArgs e)
{
txt_data.AppendText(in_data + "\n");
string inStr;
inStr = in_data;
//MessageBox.Show(inStr.Length.ToString());
if (inStr.Length == 12)
{
int indexOfSpace = inStr.IndexOf(' ');
string Patient = inStr.Substring(indexOfSpace + 1);
int rx = 0;
int selected = 0;
txtData1.Text = Patient;
rx = Convert.ToInt16(Patient);
selected = Convert.ToInt16(txt_pnorec.Text);
if (rx != selected)
{
MessageBox.Show("Please check patient settings");
}
}
else if (inStr.Length == 24)
{
label2.Text = "Patient is not selected!";
label2.BackColor = Color.Red;
}
else if (inStr.Length == 10)
{
int indexOfSpace = inStr.IndexOf(':');
string Temp = inStr.Substring(indexOfSpace + 1);
txtData2.Text = Temp;
double tempflo;
tempflo = Convert.ToDouble(Temp);
if (tempflo > 20)
{
lbl_temp.Text = "Fever";
lbl_temp.BackColor = Color.Red;
}
}
else if (inStr.Length == 9)
{
int indexOfSpace = inStr.IndexOf(':');
string ECG = inStr.Substring(indexOfSpace + 1);
txtData3.Text = ECG;
}
else if (inStr.Length == 19 || inStr.Length == 20)
{
int indexOfSpace = inStr.IndexOf(':');
string Systolic = inStr.Substring(indexOfSpace + 1);
txtData4.Text = Systolic;
}
else if (inStr.Length == 21 || inStr.Length == 22)
{
int indexOfSpace = inStr.IndexOf(':');
string Diastolic = inStr.Substring(indexOfSpace + 1);
txtData5.Text = Diastolic;
}
else if (inStr.Length == 16)
{
int indexOfSpace = inStr.IndexOf(':');
string Pulse = inStr.Substring(indexOfSpace + 1);
txtData6.Text = Pulse;
}
else if (inStr.Length == 23 || inStr.Length == 17 || inStr.Length == 27 || inStr.Length == 30 || inStr.Length == 35 || inStr.Length == 29)
{
lbl_bp.Text = inStr;//to display status of BP (Normal,prehypotension etc)
string bp;
bp = inStr;
if (bp.Length == 23 || bp.Length == 27 || bp.Length == 31 || bp.Length == 35 || bp.Length == 30)
{
lbl_bp.BackColor = Color.Red;
}
else if (bp.Length == 17)
{
lbl_bp.BackColor = Color.LightGray;
}
}
else if (inStr.Length == 32 || inStr.Length == 25 || inStr.Length == 34 || inStr.Length == 33 || inStr.Length == 26 || inStr.Length == 31)
{
int indexOfSpace = inStr.IndexOf(':');
string Acc = inStr.Substring(indexOfSpace + 1);
txtData7.Text = Acc;
string test = inStr;
if (test.Length == 25 || test.Length == 34 || test.Length == 33 || test.Length == 26)
{
label21.Text = "Check on patient!";
label21.BackColor = Color.Red;
}
else if (test.Length == 32)
{
label21.Text = "";
label21.BackColor = Color.LightGray;
}
}
else
{
}
if (txtData1.Text != "" && txtData2.Text != "" && txtData3.Text != "" && txtData4.Text != "" && txtData5.Text != "" && txtData6.Text != "" && txtData7.Text != "")
{
try
{
connection2.Open();
OleDbCommand command2 = new OleDbCommand();
command2.Connection = connection2;
command2.CommandText = "insert into MedicalRecord (PatientNumber,FirstName,LastName,IC,Temperature,ECG,Systolic,Diastolic,Pulse) values('" + txt_pnorec.Text + "','" + txt_fnamerec.Text + "','" + txt_lnamerec.Text + "','" + txt_icrec.Text + "','" + txtData2.Text + "','" + txtData3.Text + "','" + txtData4.Text + "','" + txtData5.Text + "','" + txtData6.Text + "')";
command2.ExecuteNonQuery();
MessageBox.Show("Info Stored");
connection2.Close();
txtData1.Text = "";
txtData2.Text = "";
txtData3.Text = "";
txtData4.Text = "";
txtData5.Text = "";
txtData6.Text = "";
txtData7.Text = "";
Thread.Sleep(interval);
MessageBox.Show("Start");
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex);
}
txtData1.Text = "";
txtData2.Text = "";
txtData3.Text = "";
txtData4.Text = "";
txtData5.Text = "";
txtData6.Text = "";
txtData7.Text = "";
}
}
Thanks in advance.
There are multiple ways to achieve this. One example is to use a task with a delay:
Task.Delay(10000).ContinueWith(x =>
{
//Place the code you want delayed here.
});
Another example could be to use a BackgroundWorker which is made for this exact purpose.
you can use Async and Await
here is a good tutorial in combination with an GUI:
https://www.youtube.com/watch?v=MCW_eJA2FeY
you need to declare your function as async
public async void Foo()
{
...
}
than you can use:
await Task.Delay(10000);
it will release you GUI for the whole Delay and you can use your GUI while the Delay is waiting.
Task.Delay(10000).ContinueWith(x =>
{
//...
});
Have a look at Task.Delay and ContinueWith.
You can use Windows.Forms.Timer to execute something after delay... (with no UI thread freezing). Split your code on two parts. 1. Before delay 2. After delay
public partial class form1 : Form
{
// i used namespace to ensure that we use 'correct' Timer and we do not
// confuse it with System.Timers.Timer or System.Threading.Timer
System.Windows.Forms.Timer tmrDelay;
void SomeMethodWithDelay()
{
// code before delay here
tmrDelay.Enabled = true;
tmrDelay.Tick += Timer_Tick;
}
private void Timer_Tick(object sender, EventArgs e)
{
Thread.Sleep(5000);
// your code here
// disables timer to stop it
tmrDelay.Enabled = false;
}
}
Benefit from using Timer is that it is a native windows forms tool that was specially designed to help with this kind of problems.
However you can still use modern things like Tasks, for example : Task.Factory.StartNew(() => { Thread.Sleep(5000); // your method logic here });

Categories