Click here to Skip to main content
14,978,337 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to use web browser control to automate login to a site Pin
Richard Deeming20-Jun-21 23:08
mveRichard Deeming20-Jun-21 23:08 
GeneralRe: How to use web browser control to automate login to a site Pin
Mou_kol21-Jun-21 4:19
MemberMou_kol21-Jun-21 4:19 
Questionimage processing Pin
elham mohammed16-Jun-21 5:53
Memberelham mohammed16-Jun-21 5:53 
AnswerRe: image processing Pin
Dave Kreskowiak16-Jun-21 5:54
mveDave Kreskowiak16-Jun-21 5:54 
AnswerRe: image processing Pin
OriginalGriff16-Jun-21 9:34
mveOriginalGriff16-Jun-21 9:34 
QuestionVisual Basic Commission Switch Statement + Calc Pin
Member 1524886715-Jun-21 11:46
MemberMember 1524886715-Jun-21 11:46 
AnswerRe: Visual Basic Commission Switch Statement + Calc Pin
Dave Kreskowiak15-Jun-21 17:44
mveDave Kreskowiak15-Jun-21 17:44 
AnswerRe: Visual Basic Commission Switch Statement + Calc Pin
Richard Deeming15-Jun-21 22:07
mveRichard Deeming15-Jun-21 22:07 
Try something like this:
C#
private void btnCalculateCommission_Click(object sender, EventArgs e)
{
    if (!int.TryParse(txtSalesPersonLevel.Text, out var salesPersonLevel) || salesPersonLevel < 1 || salesPersonLevel > 4)
    {
        MessageBox.Show("Invalid sales person level.", "Input Error");
        txtSalesPersonLevel.SelectAll();
        txtSalesPersonLevel.Select();
        return;
    }
    
    if (!double.TryParse(txtAmountSold.Text, out var amountSold))
    {
        MessageBox.Show("Invalid amount sold.", "Input Error");
        txtAmountSold.SelectAll();
        txtAmountSold.Select();
        return;
    }
    
    double calculatedCommission;
    switch (salesPersonLevel)
    {
        case 1:
        {
           calculatedCommission = 500 + (amountSold * 0.02);
           break;
        }
        case 2:
        {
           calculatedCommission = 750 + (amountSold * 0.03);
           break;
        }
        case 3:
        {
           calculatedCommission = 1000 + (amountSold * 0.04);
           break;
        }
        case 4:
        {
           calculatedCommission = 1250 + (amountSold * 0.05);
           break;
        }
        default:
        {
            // Can't be reached, but the compiler doesn't know that:
           calculatedCommission = 0;
           break;
        }
    }
    
    lblCalculateCommission.Text = calculatedCommission.ToString("C2");
}
If you're using a recent compiler and C# 8 or later, you can use a switch expression instead:
C#
double calculatedCommission = salesPersonLevel switch
{
    1 => 500 + (amountSold * 0.02),
    2 => 750 + (amountSold * 0.03),
    3 => 1000 + (amountSold * 0.04),
    4 => 1250 + (amountSold * 0.05),
    _ => 0 // Not reachable, but the compiler doesn't know that.
};
switch expression - C# reference | Microsoft Docs[^]



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: Visual Basic Commission Switch Statement + Calc Pin
Member 1524886716-Jun-21 9:36
MemberMember 1524886716-Jun-21 9:36 
QuestionGet serial number from local printers connected to workstation by USB Pin
SzakiII15-Jun-21 2:28
MemberSzakiII15-Jun-21 2:28 
AnswerRe: Get serial number from local printers connected to workstation by USB Pin
Gerry Schmitz15-Jun-21 9:21
mveGerry Schmitz15-Jun-21 9:21 
AnswerRe: Get serial number from local printers connected to workstation by USB Pin
Randor 15-Jun-21 11:41
professional Randor 15-Jun-21 11:41 
GeneralRe: Get serial number from local printers connected to workstation by USB Pin
Gerry Schmitz15-Jun-21 13:13
mveGerry Schmitz15-Jun-21 13:13 
GeneralRe: Get serial number from local printers connected to workstation by USB Pin
Randor 15-Jun-21 14:06
professional Randor 15-Jun-21 14:06 
Questionhow to convert Character codes HTML to arabic using c# Pin
michael nabil14-Jun-21 8:30
Membermichael nabil14-Jun-21 8:30 
AnswerRe: how to convert Character codes HTML to arabic using c# Pin
Richard Deeming14-Jun-21 22:02
mveRichard Deeming14-Jun-21 22:02 
GeneralRe: how to convert Character codes HTML to arabic using c# Pin
michael nabil15-Jun-21 3:00
Membermichael nabil15-Jun-21 3:00 
GeneralRe: how to convert Character codes HTML to arabic using c# Pin
Richard Deeming15-Jun-21 3:26
mveRichard Deeming15-Jun-21 3:26 
AnswerRe: how to convert Character codes HTML to arabic using c# Pin
Gerry Schmitz15-Jun-21 6:14
mveGerry Schmitz15-Jun-21 6:14 
QuestionHow to add row headers to DataGridView? Pin
Alex Dunlop9-Jun-21 19:16
MemberAlex Dunlop9-Jun-21 19:16 
AnswerRe: How to add row headers to DataGridView? Pin
OriginalGriff9-Jun-21 20:13
mveOriginalGriff9-Jun-21 20:13 
GeneralRe: How to add row headers to DataGridView? Pin
Alex Dunlop9-Jun-21 21:53
MemberAlex Dunlop9-Jun-21 21:53 
GeneralRe: How to add row headers to DataGridView? Pin
OriginalGriff9-Jun-21 22:07
mveOriginalGriff9-Jun-21 22:07 
GeneralRe: How to add row headers to DataGridView? Pin
Alex Dunlop9-Jun-21 22:10
MemberAlex Dunlop9-Jun-21 22:10 
GeneralRe: How to add row headers to DataGridView? Pin
OriginalGriff9-Jun-21 22:25
mveOriginalGriff9-Jun-21 22:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.