单击另一个时,如何从一个radioButton恢复更改C#

时间:2015-09-08 06:54:14

标签: c# radio-button

我有两个收音机按钮。一个值为1,另一个值为最终总值。我将它们分组在一个面板中。当我选择其中一个按钮时,它正确地添加了值,但是当我选择了第一个按钮后,我选择了另一个单选按钮,它将新值添加到最终总数而不忽略第一个单选按钮选择。有一行简单的代码可以忽略第一个选择吗?

6 个答案:

答案 0 :(得分:1)

您可以使用如下所示,如果您发布正在使用的代码,我会给出更明确的输出。

  double total = Convert.ToDouble(txtTotal.Text);
  //On Radiobutton1's click
  if(radio1.Checked)  
  txtTotal.Text = Convert.ToString(total + 1);
  //On Radiobutton2's click
  if (radio2.Checked)  
  txtTotal.Text =Convert.ToString( total + 11);

答案 1 :(得分:1)

使用方法查找最终总计,并从该方法添加值1或11。类似的东西:

public void final()
{
    if(radiobtn1.Checked)
        finalTotal=grandTotal+1;
    else if(radiobtn2.Checked)
        finalTotal=grandTotal+11;
}

或者您可以通过这些radiobuttons的click事件来完成。这将每次找到最终总数。如果你有更多的计算要做,我建议使用如上所示的单独方法。

修改

根据您的代码:

int playerCardFinalTotal = 0;
int pcFinalTotal = 0;

private void btnChangeValue_Click(object sender, EventArgs e)
{        
    if (rdbOne.Checked)
    {
        pcFinalTotal = playerCardFinalTotal + 1;
    }
    else if (rdbEleven.Checked)
    {
        pcFinalTotal = playerCardFinalTotal + 11;
    }
}

<强>的变化:

  1. 您可以使用.Checked本身代替==true!rdbOne.Checked代替==false(仅提示)
  2. 使用另一个变量pcFinalTotal来存储总价值。每次单击任何这些单选按钮时,它将重置为正确的值。

答案 2 :(得分:0)

如果不了解应用程序(基于Web /桌面)和代码,很难回答。但我看到两个选项

  • 在某处保留原始值,并在单选按钮选择中添加原始值以获得总计
  • 减去其他单选按钮的值,然后为当前选择添加值以获得总计

答案 3 :(得分:0)

您可以将添加的值添加到初始化为0(零)的变量。

每当您点击单选按钮时,从您添加到最终总数的值中减去变量的值。

答案 4 :(得分:0)

我尝试过创建一个更改按钮,只是减去所选择的按钮的值。

private void btnChangeValue_Click(object sender, EventArgs e)
{            

    if (rdbOne.Checked == true)
    {
        playerTallyTotals(-1);
        rdbOne.Checked = false;
        pnlValues.Enabled = true;
    }

    else if (rdbEleven.Checked == true)
    {
        playerTallyTotals(-11);
        rdbEleven.Checked = false;
        pnlValues.Enabled = true;
    }
}

传递给:

int playerCardFinalTotal = 0;
private void playerTallyTotals(int playerCardTotalx)
{            
    playerCardFinalTotal = playerCardFinalTotal + playerCardTotalx;
}

答案 5 :(得分:0)

始终保留原始值:

#include <cstdio>

#include <thrust/pair.h>

#include "Utilities.cuh"

#define BLOCKSIZE_MESHGRID_X    16
#define BLOCKSIZE_MESHGRID_Y    16

#define DEBUG

/*******************/
/* MESHGRID KERNEL */
/*******************/
template <class T>
__global__ void meshgrid_kernel(const T * __restrict__ x, size_t Nx, const float * __restrict__ y, size_t Ny, T * __restrict__ X, T * __restrict__ Y) 
{
    unsigned int tidx = blockIdx.x * blockDim.x + threadIdx.x;
    unsigned int tidy = blockIdx.y * blockDim.y + threadIdx.y;

    if ((tidx < Nx) && (tidy < Ny)) {   
        X[tidy * Nx + tidx] = x[tidx];
        Y[tidy * Nx + tidx] = y[tidy];
    }
}

/************/
/* MESHGRID */
/************/
template <class T>
thrust::pair<T *,T *> meshgrid(const T *x, const unsigned int Nx, const T *y, const unsigned int Ny) {

    T *X; gpuErrchk(cudaMalloc((void**)&X, Nx * Ny * sizeof(T)));
    T *Y; gpuErrchk(cudaMalloc((void**)&Y, Nx * Ny * sizeof(T)));

    dim3 BlockSize(BLOCKSIZE_MESHGRID_X, BLOCKSIZE_MESHGRID_Y);
    dim3 GridSize (iDivUp(Nx, BLOCKSIZE_MESHGRID_X), iDivUp(BLOCKSIZE_MESHGRID_Y, BLOCKSIZE_MESHGRID_Y));

    meshgrid_kernel<<<GridSize, BlockSize>>>(x, Nx, y, Ny, X, Y);
#ifdef DEBUG
    gpuErrchk(cudaPeekAtLastError());
    gpuErrchk(cudaDeviceSynchronize());
#endif

    return thrust::make_pair(X, Y);
}

/********/
/* MAIN */
/********/
int main()
{
    const int Nx = 3;
    const int Ny = 4;

    float *h_x = (float *)malloc(Nx * sizeof(float));
    float *h_y = (float *)malloc(Ny * sizeof(float));

    float *h_X = (float *)malloc(Nx * Ny * sizeof(float));
    float *h_Y = (float *)malloc(Nx * Ny * sizeof(float));

    for (int i = 0; i < Nx; i++) h_x[i] = i;
    for (int i = 0; i < Ny; i++) h_y[i] = i + 4.f;

    float *d_x; gpuErrchk(cudaMalloc(&d_x, Nx * sizeof(float)));
    float *d_y; gpuErrchk(cudaMalloc(&d_y, Ny * sizeof(float)));

    gpuErrchk(cudaMemcpy(d_x, h_x, Nx * sizeof(float), cudaMemcpyHostToDevice));
    gpuErrchk(cudaMemcpy(d_y, h_y, Ny * sizeof(float), cudaMemcpyHostToDevice));

    thrust::pair<float *, float *> meshgrid_pointers = meshgrid(d_x, Nx, d_y, Ny);
    float *d_X = (float *)meshgrid_pointers.first;
    float *d_Y = (float *)meshgrid_pointers.second;

    gpuErrchk(cudaMemcpy(h_X, d_X, Nx * Ny * sizeof(float), cudaMemcpyDeviceToHost));
    gpuErrchk(cudaMemcpy(h_Y, d_Y, Nx * Ny * sizeof(float), cudaMemcpyDeviceToHost));

    for (int j = 0; j < Ny; j++) {
        for (int i = 0; i < Nx; i++) {
            printf("i = %i; j = %i; x = %f; y = %f\n", i, j, h_X[j * Nx + i], h_Y[j * Nx + i]);
        }
    }

    return 0;

}

当您提交结果时,您将使用最终值。