c# listbox i have trouble with - c#-4.0

I created a list in a button ADD:
{
List <string> Names = new List<string>();
Names.Add(textBox1.Text);
textBox1.Text = " ";
}
I created another button SHOW NAMES and i want these names I entered in the list, to be listed in the listbox? How can this be done?

First, you need to move that first line outside of the button click method, because if you declare the list inside the method, it will be gone once that method returns.
For your SHOW NAMES method, if all you want to do is display the list, you could use a TextBlock instead of a listbox, and it will be a little easier:
TextBlock tb = new TextBlock();
tb.text = string.Concat(Names);

Related

Matlab - Display trailing edge of a long strings in a listbox by hovering the mouse over the string

I have a Matlab listbox on which some strings are very long. I do not want to make listbox too wide just only because of these few long strings.
Is there anyway to display trailing edge of these long strings in my listbox by simply hovering the mouse over those strings without using scroll pane?
Perhaps, you can set the TooltipString property of your listbox. This is what is displayed when you hover the cursor on some object. It will not be a nice or user friendly but is better than nothing.
%Create a listbox
myListbox = uicontrol('Style','listbox');
set(myListbox,'TooltipString','','Callback',#listboxCB);
%Callback function called each time the listbox value is changed
%It should also be called whenever the 'String' property is updated
function listboxCB(obj,evt)
%Get the value
v=get(obj,'Value');
if isempty(v)
set(myListbox,'TooltipString','');
return;
end
%Get the string corresponding to that line
str = get(obj,'String');
str = str{v(1)}; %Show the first one (if 'multiselect' = 'on')
set(myListbox,'TooltipString',str);
end
There may be some clever way by interacting directly with the underlying Java objects.
See Jan's answer using Java objects. Worked great.
% Prepare the Matlab listbox uicontrol
hFig = figure;
listItems = {'apple','orange','banana','lemon','cherry','pear','melon'};
hListbox = uicontrol(hFig, 'style','listbox', 'pos',[20,20,60,60], 'string',listItems);
% Get the listbox's underlying Java control
jScrollPane = findjobj(hListbox);
% We got the scrollpane container - get its actual contained listbox control
jListbox = jScrollPane.getViewport.getComponent(0);
% Convert to a callback-able reference handle
jListbox = handle(jListbox, 'CallbackProperties');
% Set the mouse-movement event callback
set(jListbox, 'MouseMovedCallback', {#mouseMovedCallback,hListbox});
% Mouse-movement callback
function mouseMovedCallback(jListbox, jEventData, hListbox)
% Get the currently-hovered list-item
mousePos = java.awt.Point(jEventData.getX, jEventData.getY);
hoverIndex = jListbox.locationToIndex(mousePos) + 1;
listValues = get(hListbox,'string');
hoverValue = listValues{hoverIndex};
% Modify the tooltip based on the hovered item
msgStr = sprintf('<html>item #%d: <b>%s</b></html>', hoverIndex, hoverValue);
set(hListbox, 'Tooltip',msgStr);
end % mouseMovedCallback
https://www.mathworks.com/matlabcentral/answers/436048-display-trailing-edge-of-a-long-strings-of-a-listbox-by-hovering-the-mouse-over-the-string#answer_352806

Can I use a string as a name in Visual Basic to change an objects properties?

My Idea is not too hard. I have a button, a string called "progname", a TextBox and three Progressbars.
When I enter a number into the TextBox and press the button, the following code run's through.
Dim progname As String
progname = "Progressbar" & TextBox1.Text
Now I have a string called "progname" with relevant value.For an example "Progressbar2".
What I want to achieve is write something like:
progname.Value += 1
Which can't be done, as "Value" is not a Member of "String". How can I do this?
Overall what I want, is to be able to select one of the three progressbars by typing one of the numbers 1-3 into the TextBox and then change that ones porperties.
Yes you can.
A basic example is this, which searches your form for controls with the name matching your string. It then changes the type to a ProgressBar so you can access all the methods ..
Dim progbar As ProgressBar = CType(Me.Controls.Find(progName, False)(0), ProgressBar)
progbar.Value += 1

How to access a FormControl checkbox in an Excel sheet using OpenXML SDK

I have a spreadsheet that has a number of check boxes in various cells on the sheet that I need to get the value of (checked/unchecked) from within a c# program.
I'm using the OpenXML SDK v2.5 and the associated toolbox.
Using the toolbox I can see the check box controls as part of the AlternateControlParts collection. These are not ActiveX checkboxes but are form controls added via the developer tab in Excel.
When I use the SDK I can also see the WorkSheetPart which has a ControlPropertiesParts collection on it which lists all the checkboxes.
My problem is, how do I find which checkbox is in which cell or at least related to which cell?
I have also found the collection
wsPart.ControlPropertiesParts.First().DrawingsPart
.WorkSheetDrawing.DrawingsPart.WorkSheetDrawing
This collection appears to have the alternate content of each of the checkboxes and if I drill down further I can find the anchor points which appear to give the location of the checkboxes relative to the cells on the sheet. However, the col and row Id’s don’t appear to exactly match up and I suspect that the Offset values may also have something to do with it.
If someone can point me in the right direction on how to map the checkboxes to the correct row/cells I would be very grateful.
Thank you for any help.
Regards
Paul
I have a solution, it contains only the logic (The property FormControlProperties is available since Office 2010:
SpreadsheetDocument document;
string sheetName = "sheetName";
string controlName = "Option Button 5";
...
var wbPart = document.WorkbookPart;
var theSheet = wbPart.Workbook.Descendants<Sheet>().FirstOrDefault(s => s.Name == sheetName);
var wsPart = (WorksheetPart)wbPart.GetPartById(theSheet.Id);
var control = wsPart.Worksheet.Descendants<DocumentFormat.OpenXml.Spreadsheet.Control>().FirstOrDefault(c => c.Name == controlName);
var controlProperies = (ControlPropertiesPart)wsPart.GetPartById(control.Id);
bool isChecked = controlProperies.FormControlProperties.Checked == "Checked";
But it is simplier to map the FormControl value to a cell and read the cell value if the you can edit the excel file.

ColumnCount in DataGridView remains 0 after assigning data source

I am having trouble with the following simple code
BindingList<Car> tempList = new BindingList<Car>();
BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = tempList;
dgTempView.DataSource = bindingSource;
Here, dgTempView is a data grid view
After the above lines execute, the column count in the datagrid view remains 0. And when I try adding a Car instance in tempList, I get an error saying that 'no row can be added to a datagridview control that does not have columns' . I am not able to understand what am I missing here
Found my mistake. The members of the Class Car were public instance variables and not properties. The moment I changed them to Auto Properties it worked :)

Setting DisplayMemberPath of ComboBox in code

In my WPF program I have:
string queryString = "Select AccountID, ProjectName from Foo where IsEnabled = 1";
SqlDataAdapter adapter = new SqlDataAdapter(queryString, sConn1);
DataSet dsAccounts = new DataSet();
adapter.Fill(dsAccounts, "Accounts");
cbAccount.ItemsSource = dsAccounts.Tables["Accounts"].AsEnumerable();
cbAccount.DisplayMemberPath = "ProjectName";
When my program runs and I dropdown the ComboBox all the rows are there but they display as blanks. When I click on a row, my SelectionChanged event handler properly identifies the selected row and picks up the proper values.
I believe my problem is with the DisplayMemberPath.
What am I doing wrong?
This is not an answer but rather a workaround. This works:
cbAccount.DataContext = dsAccounts.Tables["Accounts"];
//cbAccount.ItemsSource = dsAccounts.Tables["Accounts"].AsEnumerable();
cbAccount.DisplayMemberPath = "ProjectName";
By setting the DataContext reather than the ItemSource then the DisplayMemberPath is being set properly.
The question remains open, there must be a way to properly set the DisplayMemberPath when one has an ItemSource rather than a DataContext.
I believe the problem is that your table accounts is not serialized to objects.
If you use a list of accounts instead of your tables then it works perfect with the ItemsSource and DisplayMemeberPath.

Resources