In a project I want a combobox to filter the rows according to the text written there. (overriten class of combobox). I am performing it simply as below:
private void Cmb1_TextUpdate(object sender, EventArgs e)
{
if (isFound(Cmb1.Text)) //if text is not found in dataTable then false
dataTable.DefaultView.RowFilter = string.Format("Name LIKE '%{0}%'", Cmb1.Text);
}
It filters the results. But for example if there is 1 element left after filtering dropdown height decreases to fit the text. It is okey until now. But after I delete filter text items come back but dropdown height is not changing (still in a height value for 1 item).
To fix it I need to unfocus and focus again so it refreshes.
I tried to write this.DropDownHeight = value; at the end of Cmb1_TextUpdate and also override the OnDropDown event
I tried enter link description here subject already but no luck (I think refresh works there but for me not):
NOT: It works when I add these:
DroppedDown = false; DroppedDown = true;
but slows down working of combobox
Related
I want to fill a ComboBox when the GotFocus event is triggered.
I tried to fill my ComboBox in the GotFocus event, but it doesn't seem to work.
Items are visible in the dropdown menu but when I want to select one of them the collection is cleared
Click on the ComboBox :
Try to select the first item :
Here is my code :
Private Sub agence_GotFocus(sender As Object, e As RoutedEventArgs) Handles agence.GotFocus
strsql = "Select age_cpt, age_abrege + ' ' + age_nom as age_abregenom from gen_agence where age_soc = " & societe.SelectedValue
Dim da As New SqlDataAdapter(strsql, connSQLServer)
Dim ds As New DataSet()
da.Fill(ds, "t")
agence.ItemsSource = ds.Tables("t").DefaultView
agence.DisplayMemberPath = "age_abregenom"
agence.SelectedValuePath = "age_cpt"
End Sub
How can I manage that ?
I don't want to use MVVM.
The problem is that this event fires multiple times.
Below I will show a demo code in Sharpe (I do not have VB.Net loaded in the Studio), but the code is simple, you should repeat it yourself in BASIC without any problems.
private void OnGotFocus(object sender, RoutedEventArgs e)
{
Debug.WriteLine(++eventCount);
ComboBox comboBox = (ComboBox)sender;
// Try the second option by excluding the following line
if (comboBox.ItemsSource == null)
comboBox.ItemsSource = "First,Second,Third".Split(',');
}
The code should check for null so as not to reconnect the collection.
Commenting out this line will reproduce your problem.
After launching for execution and trying to select an item, you will see in the "Output" window that the event has occurred several times.
It looks like changing the list at the moment of its expansion somehow breaks the logic of the internal bindings of the ComboBox.
There are several ways to fix this, but for this we need to know why you chose this initialization of the element - when the focus is on it.
Several ways:
Nulling the source before assigning a new collection.
private void OnGotFocus(object sender, RoutedEventArgs e)
{
Debug.WriteLine(++eventCount);
ComboBox comboBox = (ComboBox)sender;
comboBox.ItemsSource = null;
comboBox.ItemsSource = "First,Second,Third".Split(',');
}
Setting the collection once when loading the ComboBox.
<ComboBox Loaded="OnLoaded"/>
private void OnLoaded(object sender, RoutedEventArgs e)
{
Debug.WriteLine(++eventCount);
ComboBox comboBox = (ComboBox)sender;
comboBox.ItemsSource = "First,Second,Third".Split(',');
}
Other implementation options are possible, but I need to know more details about your task in order to choose how best to do it.
Answer to the additional question:
Nulling the source before assigning a new collection is a good idea. Now I can move my mouse over the values in the dropdown menu. However, when I select a value, the field is cleared.
If it is not a string array, then the selection can be cleared.
When you enter the event, you recreate the tables each time.
And the rows of these tables, although they are the same in content, will not be considered the same instance.
You selected a row from one table, then you rewrote the source, and the new source does not have this row.
The selection is reset.
I already wrote above that in order to choose the optimal solution, I need more details of your problem.
The way you have chosen to initialize the list is very unusual.
Each time you work in the ComboBox, you make queries to the database three to four times.
I have never met such a implementation and I do not understand its meaning.
Explain why do you need this?
I found a way to do what I wanted : Use the DropDownOpened event instead of GotFocus
Private Sub agence_DropDownOpened(sender As Object, e As EventArgs) Handles agence.DropDownOpened
'... Fill the ComboBox
End Sub
Fill a ComboBox in the GotFocus event works in VBA but not in VB.Net.
In VB.Net I can't use GotFocus cause the event is triggered multiple time. It's triggered when :
I open the ComboBox
I pass my mouse over the items
I select an item
Moreover, this can trigger a stackoverflow exception.
I know it's better to fill a ComboBox before open it.
I am in an application migration procedure and I cannot afford to change the entire code structure.
Use the DropDownOpened event was the best option in my case.
Thanks to the people who helped me find a solution.
I'm trying to center my checkboxes in a TableLayoutPanel, but they always end up looking left-aligned due to the nature of the checkbox control. See picture below:
I want each rows checks to be left-aligned, but for it to appear more centered. Something like the following:
I've checked around online, and I can center the checkboxes by setting AnchorStyles.None which is not what I want, because then the checkboxes are not aligned. I have them set to Dock.Fill so you can click anywhere in the cell to activate the checkbox.
I'm currently just padding my table to achieve a similar effect, but it's by far not an acceptable solution long-term. Also, padding the cells will line break the checkbox text without taking up all the available space on the row (since some of the row is being eaten by padding). The same goes for using a spacer-cell on the left of the table, not an ideal solution.
Does anyone have any ideas? Thanks!
This may work for you:
Set all the ColumnStyles of your TableLayoutPanel as .SizeType = SizeType.AutoSize.
Set your TableLayoutPanel.AutoSize = true and TableLayoutPanel.AutoSizeMode = AutoSizeMode.GrowAndShrink;
Add this code to center your checkboxes (as well as your TableLayoutPanel) dynamically:
//SizeChanged event handler of your tableLayoutPanel1
private void tableLayoutPanel1_SizeChanged(object sender, EventArgs e){
//We just care about the horizontal position
tableLayoutPanel1.Left = (tableLayoutPanel1.Parent.Width - tableLayoutPanel1.Width)/2;
//you can adjust the vertical position if you need.
}
UPDATE
As for your added question, I think we have to change some things:
Set your CheckBox AutoSize to false. The solution before requires it to be true.
Add more code (beside the code above):
int checkWidth = CheckBoxRenderer.GetGlyphSize(yourCheckBox.CreateGraphics(),System.Windows.Forms.VisualStyles.CheckBoxState.MixedNormal).Width;
//TextChanged event handler of your CheckBoxes (used for all the checkBoxes)
private void checkBoxes_TextChanged(object sender, EventArgs e){
UpdateCheckBoxSize((CheckBox)sender);
}
//method to update the size of CheckBox, the size is changed when the CheckBox's Font is bolded and AutoSize = true.
//However we set AutoSize = false and we have to make the CheckBox wide enough
//to contain the bold version of its Text.
private void UpdateCheckBoxSize(CheckBox c){
c.Width = TextRenderer.MeasureText(c.Text, new Font(c.Font, FontStyle.Bold)).Width + 2 * checkWidth;
}
//initially, you have to call UpdateCheckBoxSize
//this code can be placed in the form constructor
foreach(CheckBox c in tableLayoutPanel1.Controls.OfType<CheckBox>())
UpdateCheckBoxSize(c);
//add this to make your CheckBoxes centered even when the form containing tableLayoutPanel1 resizes
//This can also be placed in the form constructor
tableLayoutPanel1.Parent.SizeChanged += (s,e) => {
tableLayoutPanel1.Left = (tableLayoutPanel1.Parent.Width - tableLayoutPanel1.Width)/2;
};
Instead of having the checkboxes in cells, having each one inside a panel all inside a groupbox will allow the checkboxes to fill each panel and have a click able area around them. then with the groupbox dock set to fill and the panels' anchors set to top,bottom they all stay centered.
I have to check / uncheck all the checkboxes (toggle) in a column when the user double clicks the column header.
How can I implement this behaviour in the DevExpress DxGrid control?
I have searched the DevExpress support forum but I haven't found a solution.
Also, i am working on MVVM Pattern.
This case works for WinForms, not tested in WPF yet, I posted might it direct you to some lights:
There is a workaround to accomplish this behave, you have to implement yourGrid_DoubleClick Event Handler, then calculate the hit Info of the mouse click, the hit info object will tell you if the double click was on a column, something like:
private void yourGridViewName_DoubleClick(object sender, EventArgs e)
{
DevExpress.XtraGrid.Views.Grid.GridView sndr =
sender as DevExpress.XtraGrid.Views.Grid.GridView;
DevExpress.Utils.DXMouseEventArgs dxMouseEventArgs =
e as DevExpress.Utils.DXMouseEventArgs;
DevExpress.XtraGrid.Views.Grid.ViewInfo.GridHitInfo hitInfo =
sndr.CalcHitInfo(dxMouseEventArgs.Location);
if (hitInfo.InColumn)
{
string x = hitInfo.Column.Name;
//Rest of your logic goes here after getting the column name,
//You might now loop over your grid's data and do your logic
}
}
but you have to notice that this action will not prevent the sorting that column's header do, you might need to disable sorting for this grid
Hope this helped.
I'm trying to build a Listview EDIT/Insert template where I can use a checkbox to enable updating multiple database tables but to little success.. I managed to get the insert working by performing some foul sorcery on the Listview inserting event. But I'd prefer that it works with the Checkbox OnCheckedChanged event as it feels abit more kosher in my mind, and of course the added benefit on it working for the edittemplate..
protected void checktest_clicked(object sender, EventArgs e)
{
//testlabel.Text = testcheck.Checked.ToString(); <-- exists outside of LW
// so it works
//Label hejha = (Label)lwRapport.FindControl("testlabel");
CheckBox trial = (CheckBox)lwRapport.FindControl("upParameter");
if(trial != null)
{
if(trial.Checked == true)
{ testlabel.Text = "finally"; }
if(trial.Checked == false)
{ testlabel.Text = "Nope, not going to happen"; }
}
if (trial == null)
{ testlabel.Text = "not wanted"; }
}
That's my test snippet for checking how the FindControl works and so far I've been quite unsuccessful making it do what I want it to do..
Any Correction on faults / hack / workaround for this matter would be apritiated
EDIT1*
The checkbox is inside of the listview, more precisely in the inserttemplate. The template looks something on the lines like this:
textbox <bind"table1.element">
textbox2 <bind"table1.element2">
checkbox [_]
textbox3 <bind"table2.element">
Observe that the snippet above is just a pseudocode snippet of my layout not the acctual layout. What I'm attempting is to find the checkbox and bind it's checked value to a parameter which passes a couple of checks in the SPROC then executes the UPDATE command
You seem to be not able to find the check box control from the list view. This is because you are searching for the check box inside the list view, and what you should be doing is searching for it inside the selected item.
You can have a look at this. Although it's GridView, I think it will works too.
I've not gone into much research here, but the intuitive thing is not working:
private void SerachButton1_Click(object sender, EventArgs e)
{
String serchTerm = searchTerm1.Text;
String text = usualTextBox.Text;
Int32 index = text.IndexOf(serchTerm);
if (index >= 0)
{
usualTextBox.Select(index, serchTerm.Length);
}
}
SelectedText, SelectionLength and SelectionStart properties are as I expect them after Select is called but there's no visible selection.
What am I doing wrong here?
Edit: I've also tried RichTextBox. When I set background and text colors for the selection it shows up, but it won't unselect automatically when you manually select another part of text or just click on a position in text. Are these two types of selection inherently different and if you select programmatically you have to also deselect programmatically?
You need to set usualTextBox.HideSelection to false so that the selection remains visible when the focus is not in the TextBox.