Deselect DataGridView rows - winforms

I am using C++/CLI Windows Forms Application.
I have a DVG, and I want to deselect rows by clicking blank area of DVG. I tried several ways, and none of them works.
1)
System::Void Form1::dataGridView1_MouseDown(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
{
if (e->Button == System::Windows::Forms::MouseButtons::Left)
{
if (dataGridView1->HitTest(e->X, e->Y)->Equals(DataGrid::HitTestInfo::Nowhere))
{
dataGridView1->ClearSelection();
}
}
}
2) This variant causes error (Error 1 error C3063: operator '==': all operands must have the same enumeration type)
)
if (e->Button == System::Windows::Forms::MouseButtons::Left)
{
if ((dataGridView1->HitTest(e->X, e->Y)->Type) == DataGrid::HitTestType::None)
{
dataGridView1->ClearSelection();
}
}

The name of your variable is dataGridView1. That implies to me that you're using DataGridView, not DataGrid. You should be using DataGridView::HitTestInfo::Nowhere instead of DataGrid::HitTestInfo::Nowhere, and DataGridViewHitTestType instead of DataGrid::HitTestType.
In your first example, you're comparing a DataGridView::HitTestInfo to DataGrid::HitTestInfo::Nowhere. You're calling the Equals(object, object) method, so it's a valid line of code, but those are different classes that will never return equal, which is why the selection is never getting cleared.
In your second example, you're comparing a DataGridViewHitTestType to a DataGrid::HitTestType, which will generate the compiler error.

Related

textbox validated method does not work while assigning value to textbox

I am using c#.net 2.0 winforms. I use errorprovider control in my form to validate a textbox. While I programatically assign value to that textbox. textbox validated method does not take the value from the textbox or considers it a blank value. How can I validate my textbox by without entering value in the textbox. Here is the code
private void textBox6_Validated(object sender, EventArgs e)
{
bTest6 = txtRegExPinIsValid(textBox6.Text);
if (bTest6)
{
this.errorProvider1.SetError(textBox6, "");
}
else
{
this.errorProvider1.SetError(textBox6, "This field must contain Exactly 6 digits");
}
}
private bool txtRegExPinIsValid(string textToValidate)
{
Regex TheRegExpression;
string TheTextToValidate;
string TheRegExTest = #"^\d{6}$";
TheTextToValidate = textToValidate;
TheRegExpression = new Regex(TheRegExTest);
// test text with expression
if (TheRegExpression.IsMatch(TheTextToValidate))
{
return true;
}
else
{
return false;
}
}
While performing update operation I fill the textbox with values from the ms access table. If the value is correct, just leave it otherwise I have to update it. Please help me. Thanks in advance
I would recommend placing the validation code in a separate method. Call that method from both the Validated event and the location in your code where you need to programatically validate, as shown below:
// Call this from wherever you need to validate a TextBox
void PerformValidation(TextBox textBox)
{
bTest6 = txtRegExPinIsValid(textBox6.Text);
if (bTest6)
{
this.errorProvider1.SetError(textBox6, "");
}
else
{
this.errorProvider1.SetError(textBox6, "This field must contain Exactly 6 digits");
}
}
private void textBox6_Validated(object sender, EventArgs e)
{
PerformValidation(textBox6);
}

Swap two check boxes values

I have 2 check boxes. I want to uncheck when I check the other and vice-versa. So , always, I'll have only one checked and the other unchecked. What I'm having now is when I check one, the other is unchecked (good). Now, when I go to the unchecked and check it, both become unchecked in the first click. Here is my example:
def CB1Function():
CB2.setChecked(0)
CB1.stateChanged.connect(CB1Function)
def CB2Function():
CB1.setChecked(0)
CB2.stateChanged.connect(CB2Function)
In other words, for example, I have 3 options with 3 checkboxes, I'm supposed to select only one option at a time.
Thanks
It might be a silly answer but why not use radiobuttons?
A radio button or option button is a type of graphical user interface element that allows the user to choose only one of a predefined set of options.
Source: http://en.wikipedia.org/wiki/Radio_button
Sounds like a radiobutton indeed. But if you insist on checkboxes, try something like this:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
checkBox2.Checked = !checkBox1.Checked;
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
checkBox1.Checked = !checkBox2.Checked;
}
EDIT: if you have more then two checkboxes (and even with two) you could make a procedure that loops through all checkboxes and changes them, something like (pseudocode):
private void CheckAllCheckboxes(checkbox ACheckbox) //ACheckbox is the checkbox just clicked
{
bool JustChecked = ACheckbox.checked; //did you enable or disable ACheckbox?
for (int i = 0; i <= AllCheckBoxes.count; i++)
{
if (AllCheckboxes[i] != ACheckbox) //every checkbox except the one just checked...
{
AllCheckBoxes[i].checked = !JustChecked //set the opposite
}
}
}

Debugging a Custom Windows.Forms.ComboBox

Problem - whenever I set a custom ComboBox to "ON," it will change to "ON" momentarily, but it will then occasionally switch to "OFF."
Basically I have a method:
void value_SelectedIndexChanged(object sender, EventArgs e)
{
if((ComboBox)sender.Focused == true)
{
if(value.SelectedIndex == false) // OFF
// set a bunch of variables for OFF
else // ON
// set a bunch of variables for ON
}
}
After I set the ComboBox to "ON," it then executes the code path for "ON" selected index. But then, the selected index switches to "OFF," for a reason I do not understand. As a result, the OFF variables code path then executes.
To attempt to fix it, I put a log statement to capture the sender and EventArgs e whenever this "value_SelectedIndexChanged" method gets called. However the sender equals the class and the EventArgs is System.EventArgs.
Please advise me on how to debug this problem.
I don't quite understand why you structure your code that way. Wouldn't it make more sense this way:
void value_SelectedIndexChanged(object sender, EventArgs e)
{
switch(value.SelectedIndex)
{
case 0: // OFF
// set a bunch of variables for OFF
break;
case 1: // ON
// set a bunch of variables for ON
break;
}
}
The problem might be caused by the "set a bunch of variables for OFF"-actions. Have you tested that by setting a breakpoint in the handler and then stepping through everything?

How to restrict special symbols like % in textbox in wpf?

I would like to develop a textbox which restricts special symbols like %. I used textbox's keydown event to restrict '%' . I already used the code as
if(Keyboard.Modifiers == ModifierKeys.Shift && e.key == key.D5)
{
e.handle=true;
return;
}
when i implement this in mvvm architecture, I got problem with the dependency property that recognizes only shift as one key and D5 as another when I converted systemkey into string format.
How can I recognize % symbol?
you can listen to the PreviewTextInput event instead of the KeyDownEvent :
myTextBox.PreviewTextInput += PreviewTextInputHandler;
and then:
private void PreviewTextInputHandler(Object sender, System.Windows.Input.TextCompositionEventArgs e)
{
e.Handled = !AreAllValidChars(e.Text);
}
this is one such function I use in my App, you would have to tweak it a bit to test the right caracters, but this you know how to do.
as for getting the % caracter, well you just have to write something like:
if (e.Text == '%') ...;

OnPropertyChanged reaction different the first time and the second time

I have got a collection of viewModels(InputViewModel) in an other viewModel(ScenarioManager).
each InputviewModel has an instance of a Class(RestOfInput) which contains properties able to raise the OnPropertyChanged.
when one of these properties changes the event is handled by this method (in InputViewModel) :
public void TestAfterChanges(object sender, PropertyChangedEventArgs e)
{
MessageBox.Show("not ref");
bool isInTheList = false;
RestOfInput roi = sender as RestOfInput;
string prop = e.PropertyName;
if (prop!="NameFile")
{
Difference d = new Difference();
d.Length = prop;
d.Value1 = reference.RoI.getValueByPropertyName(prop);
d.Value2 = roi.getValueByPropertyName(prop);
foreach (Difference diff in _ListOfDifferences)
{
if (diff.Length==prop)
{
if ( (Math.Abs(d.Value2-d.Value1)>0.001*d.Value1))
{
//replace by le new one
_ListOfDifferences.Insert(_ListOfDifferences.IndexOf(diff), d);
_ListOfDifferences.Remove(diff);
}
else
{
//if change make the field value equal to the ref then remove from difference list
_ListOfDifferences.Remove(diff);
}
isInTheList = true;
}
}
if ((Math.Abs(d.Value2 - d.Value1) > 0.001 * d.Value1) && isInTheList==false)
{
_ListOfDifferences.Add(d);
}
}
}
this method gives just a summary of the differences between this particular case and the reference case.
Now if the reference case changes I have to update all the cases and the event is handled
in ScenarioManager :
public void refCaseChanging(object sender, PropertyChangedEventArgs e)
{
MessageBox.Show("ref");
string propname = e.PropertyName;
foreach (InputViewModel item in _casesList)
{
if (item!=inpVM)
{
item.RoI.OnPropertyChanged(propname);
}
}
}
inpVM is the reference case.
Then I have this behavior :
-if I change a field in a case which is not the reference case : everything is ok.
-if I change a particular field in the reference case : the first time, everything is ok.
But the second time, only the reference case and the first case (in the collection) which is not the reference case are updated>
It is like the foreach loop is broken..
Any explications.
If the message is not clear please tell me ( not easy to explain ;) )
An exception could explain that processing stops (although one expects that it would be caught and displayed somewehre).
Have you tried to ask VS to halt your program when an exception is thrown ? (if you have never done this before, go to Debug / Exceptions and check the check box for CLR exceptions)

Resources