Hi there,

I have a form that loads data from the database and the and tose data are being disabled so that the user can’t edit any information on it.
In the same form I have two radio button as received and denied,
Weh I select deny another small form appears telling the user to enter the reason for denying. That small button has two buttons which is ok and cancel.
Whne I select deny and when the small form appears, and then I click cancel in that button “which the code is this.close()” .then the other form appears and the check box radio button is selected as denyed.how can I make it receive when the user clicks cncel in the small form, how can I do this.


thanxxxxx

Answer 1 : How RadioButtons work
Provided that the radio buttons are all in the same container then only one of them is ever true (Checked).
Setting any one of them Checked automatically un-checks the others.
So you could just do rbReceived.Checked = true; When you click on a check box it normally automatically goes Checked, but if you set the AutoCheck property to false then you can manually control the ckecked state.
This way you can do rbDenied.Checked = true; after OK is pressed on you form instead. This way the user will not see it go Denied at all.

Answer 2: How to return DialogResult
Re-reading you post (which is a bit confusing) I wonder if your problem is to do with knowing if OK or Cancel has been pressed.

This is a function of a dialog box. Assuming that you open you "deny reason" form using form.ShowDialog() then it will return a DialogResult value. Testing this result can identify which button was pressed.
To get your form to return the required result just set the DialogResult property for the OK and Cancel buttons.
(Note: I think that doing this automatically closes the form when the button is clicked, so you don't need the form.close(); . Not sure so test this.)

commented: spot on +1
commented: Well Worded :) +1

Yeah, I remember those days, back in the sixties. It was on a beach. Lots of mini scirts and bikinis floating around... But also lots of little transistor radios.
They had a row of buttons on the top to select a radio channel. If you pushed one down, all the others where or went up. (You cannot listen to more than one channel)
We still use that metaphore today in a user interface.

commented: Classic +2

Answer 1 : How RadioButtons work
Provided that the radio buttons are all in the same container then only one of them is ever true (Checked).
Setting any one of them Checked automatically un-checks the others.
So you could just do rbReceived.Checked = true; When you click on a check box it normally automatically goes Checked, but if you set the AutoCheck property to false then you can manually control the ckecked state.
This way you can do rbDenied.Checked = true; after OK is pressed on you form instead. This way the user will not see it go Denied at all.

Answer 2: How to return DialogResult
Re-reading you post (which is a bit confusing) I wonder if your problem is to do with knowing if OK or Cancel has been pressed.

This is a function of a dialog box. Assuming that you open you "deny reason" form using form.ShowDialog() then it will return a DialogResult value. Testing this result can identify which button was pressed.
To get your form to return the required result just set the DialogResult property for the OK and Cancel buttons.
(Note: I think that doing this automatically closes the form when the button is clicked, so you don't need the form.close(); . Not sure so test this.)

Hi, sorry about that I’ll explain it again. I have a from in C#(frmSubmit).in that I have two radio buttons(receive ,deny) in a panel when the deny radio button is clicked another small interface(frmReasonDeny) will open(new frmReasonDeny().ShowDialog(); ) and allos the user to enter the reason for denying. In the frmReasonDeny there is two buttons which is ok and the other is cancel.
The code of the cancel button is this.close();
When the user selects deny radio button the user can enter the reason and then click ok. This section is working
The problem is when the user select deny radio button and then the user clicks cancel button in the frmReasonDeny, it closes the frmReasonDeny form and still the radio button is as deny, how can I make it to check the receive radio button.

How can I solve this???

thanxxxxxxx

Try something like this.

frmReasonDeny frmRD = new frmReasonDeny()
if (frmRD.ShowDialog() == DialogResult.OK)
{
    // Ok statement
    MessageBox.Show("Hurray");
}
else 
{
   //Cancel/quit/anything other then clicking ok
   MessageBox.Show(":(")
}

In frmReasonDeny you want to do

this.DialogResult= DialogResult.OK;

//or 

this.DialogResult= DialogResult.Cancel;

In the respective buttons.

Enjoy.

Whilst your method works fine finito, you are creating extra work for yourself. As nick.crane pointed out, you can set a DialogResult value in the buttons properties. If you set a buttons DialogResult to OK, if the form is opened modally then when you click it the Form's DialogResult value is set to OK and the form closes (i tested this nick :p )
No need to manually code the button click events to set the DialogResult

commented: Thanks I didn't know such a property existed. +2

Another code saver is setting the forms AcceptButton and CancelButton to the OK and Cancel buttons on the form.
Of the two the CancelButton property is most useful as this automatically ensures that the CancelButton action (i.e. set DialogResult=Cancel) is done if the user closes the form using the X button.

commented: Reiteration, but helpful nonetheless. +2

Try something like this.

frmReasonDeny frmRD = new frmReasonDeny()
if (frmRD.ShowDialog() == DialogResult.OK)
{
    // Ok statement
    MessageBox.Show("Hurray");
}
else 
{
   //Cancel/quit/anything other then clicking ok
   MessageBox.Show(":(")
}

In frmReasonDeny you want to do

this.DialogResult= DialogResult.OK;

//or 

this.DialogResult= DialogResult.Cancel;

In the respective buttons.

Enjoy.

Hi there,
I wrote the code for this,
But the dr result is cancel everytime, why is that

private void rbtnDenied_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnDenied.Checked == true)
            {
                DialogResult dr = new DialogResult();
                dr = reasonDeny.ShowDialog();

                if (dr == DialogResult.OK)
                {
                    if (String.IsNullOrEmpty(reasonDeny.getReason()))
                        MessageBox.Show("Enter reason for denying");
                    else
                    {
                        NOAReason = reasonDeny.getReason();
                        rbtnDenied.Checked = true;
                        reasonDeny.Hide();
                    }
                }
                else
                {
                    reasonDeny.Hide();
                    rbtnReceived.Checked = true;
                }
            }    
        }

@finito: Thanks for the Rep. Your comment said "Reiteration" leading me to believe that you missunderstood my post. So I will make it clearer.

When building a custom dialog that requires an OK and Cancel button it is possible to have the form automaticaly detect when these buttons are clicked by setting form.AcceptButton = this.pbOK; and form.CancelButton = this.pbCancel; .
This can be done in the form designer using the properties window.

The AcceptButton is considered to be the default button and it's event is trigger if the Enter key is pressed on any oher control (e.g. textbox) on the form (unless in a panel or groupbox).

The CancelButton's event is triggered if Esc is pressed or the form is closes using the X button.

In both cases the associated button's action is performed. If the button also has it's DialogResult property set ( pbOK.DialogResult = DialogResult.OK; ) then this sets the forms DialogResult property and closes (hides if open using ShowDialog) the form.

hey can somebody explain to me clearly, i am confused, how can i do this
thaxxx

Sorry that we confused you Judith.:S

On your reasonDeny form you have an OK button and a Cancel button.

In the form designer for your reasonDeny form check the properties of the OK and Cancel buttons.
The DialogResult property should be set to OK and Cancel respectively.

From what I understand about what the form does there should be no need for any button click events for the OK and Cancel buttons.

Also, check the forms properties to set the AcceptButton and CancelButton properties to your OK button and Cancel button respectively.

This should get the result you want.:)

commented: could only be clearer with step by step diagrams :p +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.