|
Scott Dorman wrote: The second example is good if you can guarantee that you will never need to do anything more advanced in the property getter or setter.
Well you can change it to a "real" property and field combo any time you want. Accessing classes won't notice the difference.
Robert
|
|
|
|
|
Robert Rohde wrote: Well you can change it to a "real" property and field combo any time you want. Accessing classes won't notice the difference.
Yes, you can change it to a "real" property anytime you want without affecting the callers. My only issue with automatic properties is that it forces both a get and a set on the property and they must both be at the same access level.
|
|
|
|
|
Another issue is newbies who learn the lazy way and never learn the "better" way, and therefore wind up giving more access to their classes because they don't know any better.
|
|
|
|
|
So I wrote a clever little search tool for my list view that searches through all the items as you type. The Item.Select() and Item.EnsureVisible() work fine at runtime until I decide to TAB into the listview. At which point if I try browsing the listview using the arrow keys, I start browsing from the Item that was selected BEFORE I did my search.
Here's the basic gist of my search:
for(int i = 0; i < listView1.Items.Count;i++)<br />
{<br />
if (itemContains(targetstring, (ListViewItem)listView1.Items[i].Clone()))<br />
{<br />
listView1.Items[i].Selected = true;<br />
listView1.Items[i].EnsureVisible();<br />
return;<br />
}<br />
}
I've tried unselecting the previous selected item when a valid item was found, but that didn't seem to work. When I output SelectedItems[0].ToString() it output the correct Item. It's only when I gain focus to the listview using TAB that it treats the last item selected by mouse click as the selected item.
Any ideas here? It'd be much appreciated.
-J.Chalfant
|
|
|
|
|
Well, I figured this out.
Selected != Focus
and setting item[i].Focus = true won't take focus away from my textbox. So I can type away to my hearts content and set focus to however many listview items I want to. And when I TAB to select my listview, my selected item will still have focus.
Here's the new code:
for(int i = 0; i < listView1.Items.Count;i++)<br />
{<br />
if (itemContains(target, (ListViewItem)listView1.Items[i].Clone()))<br />
{<br />
listView1.Items[i].Focused = true;<br />
listView1.Items[i].Selected = true;<br />
listView1.Items[i].EnsureVisible();<br />
return;<br />
}<br />
}
Yay.
|
|
|
|
|
I tried to use modeless lookup form for DataGridView custom ComboBoxColumn without success.
Steps to reproduce issue:
1. Run code
2. Enter some character
3. Press Tab
Observed:
1. Next row receives focus
2. Current form remains active
Expected:
1. Focus should remain in current combobox.
2. Lookup form must become active form.
How to fix ?
Andrus.
using System;<br />
using System.Windows.Forms;<br />
<br />
class ComboBoxColumn : DataGridViewComboBoxColumn {<br />
}<br />
<br />
class ComboBoxCell : DataGridViewComboBoxCell {<br />
<br />
public override Type EditType {<br />
<br />
get {<br />
return typeof(ComboBoxEditingControl);<br />
}<br />
}<br />
<br />
}<br />
<br />
class ComboBoxEditingControl : MyComboBox, IDataGridViewEditingControl {<br />
<br />
protected int rowIndex;<br />
protected DataGridView dataGridView;<br />
protected bool valueChanged = false;<br />
<br />
protected override void OnTextChanged(EventArgs e) {<br />
<br />
base.OnTextChanged(e);<br />
NotifyDataGridViewOfValueChange();<br />
}<br />
<br />
protected virtual void NotifyDataGridViewOfValueChange() {<br />
this.valueChanged = true;<br />
<br />
if (this.dataGridView != null) {<br />
this.dataGridView.NotifyCurrentCellDirty(true);<br />
}<br />
<br />
}<br />
<br />
public Cursor EditingPanelCursor {<br />
<br />
get {<br />
return Cursors.IBeam;<br />
}<br />
<br />
}<br />
<br />
public DataGridView EditingControlDataGridView {<br />
get {<br />
return this.dataGridView;<br />
}<br />
<br />
set {<br />
this.dataGridView = value;<br />
}<br />
}<br />
<br />
public object EditingControlFormattedValue {<br />
<br />
set {<br />
<br />
if (value.ToString() != this.Text) {<br />
<br />
this.Text = value.ToString();<br />
<br />
NotifyDataGridViewOfValueChange();<br />
<br />
}<br />
<br />
}<br />
<br />
get {<br />
<br />
return this.Text;<br />
<br />
}<br />
<br />
}<br />
<br />
public object GetEditingControlFormattedValue(DataGridViewDataErrorContexts <br />
context) {<br />
<br />
return this.Text;<br />
<br />
}<br />
<br />
<br />
<br />
public void PrepareEditingControlForEdit(bool selectAll) {<br />
<br />
}<br />
<br />
public bool RepositionEditingControlOnValueChange {<br />
<br />
get {<br />
return false;<br />
}<br />
}<br />
<br />
public int EditingControlRowIndex {<br />
<br />
get {<br />
return rowIndex;<br />
}<br />
<br />
set {<br />
this.rowIndex = value;<br />
}<br />
}<br />
<br />
public void ApplyCellStyleToEditingControl(DataGridViewCellStyle <br />
dataGridViewCellStyle) {<br />
DropDownStyle = ComboBoxStyle.DropDown;<br />
}<br />
<br />
public bool EditingControlWantsInputKey(Keys keyData, bool <br />
dataGridViewWantsInputKey) {<br />
return !dataGridViewWantsInputKey;<br />
}<br />
<br />
public bool EditingControlValueChanged {<br />
<br />
get {<br />
return valueChanged;<br />
}<br />
<br />
set {<br />
valueChanged = value;<br />
}<br />
}<br />
<br />
<br />
<br />
public class Form1 : Form {<br />
<br />
private DataGridView dataGridView1 = new myDataGridView();<br />
<br />
[STAThread]<br />
public static void Main() {<br />
try {<br />
Application.Run(new Form1());<br />
}<br />
catch (Exception e) {<br />
MessageBox.Show(e.ToString());<br />
}<br />
}<br />
<br />
public Form1() {<br />
this.Controls.Add(this.dataGridView1);<br />
this.Load += new EventHandler(Form1_Load);<br />
}<br />
<br />
private void Form1_Load(object sender, EventArgs e) {<br />
<br />
ComboBoxColumn comboBoxColumn = new ComboBoxColumn();<br />
ComboBoxCell ComboBoxCell = new ComboBoxCell();<br />
comboBoxColumn.CellTemplate = ComboBoxCell;<br />
dataGridView1.Columns.Add(comboBoxColumn);<br />
}<br />
}<br />
}<br />
<br />
public class MyComboBox : ComboBox {<br />
<br />
protected override void OnValidating(System.ComponentModel.CancelEventArgs <br />
e) {<br />
<br />
e.Cancel = true;<br />
base.OnValidating(e);<br />
Form f = new Form();<br />
f.Text = "Modeless lookup form";<br />
f.Show();<br />
}<br />
}<br />
<br />
public class myDataGridView : DataGridView {<br />
<br />
protected override void OnDataError(bool displayErrorDialogIfNoHandler, <br />
DataGridViewDataErrorEventArgs e) {<br />
e.Cancel = false;<br />
}<br />
}
Andrus
|
|
|
|
|
Am trying to add a new row to the gridview. I have found articles where they use footerrow to add new row. The samples are using template columns but am using bound columns.
Can I use footer for adding new row for bound columns too?
Is there any other way to add a row to Gridview which is a better practice?
Thanks
|
|
|
|
|
Insert and empty row in to your datasource, then set that row as editable in the datagrid.
only two letters away from being an asset
|
|
|
|
|
Thank you Mark. That is excellent.
From button click event, how do I found out the editindex of a particular row?
Thanks
|
|
|
|
|
What button are you clicking? Check the eventarg.
only two letters away from being an asset
|
|
|
|
|
You can set the EditIndex like this:
GridView.EditIndex = GridView.Rows.Count;
Hope this helps!!!
____________________________
"The only true wisdom is in knowing you know nothing."
-- Socrates
|
|
|
|
|
Thank you.
Thanks
|
|
|
|
|
Right now the company I work for has three different Visual studio toolbox installation utilities to install the controls into each toolbox. I've taken it upon myself to, in my free time, combine these into a single utility. The obvious route (to me anyways, I could be wrong) is to use reflection and since I don't have much experience with using reflection I thought this would be a good time to learn.
The scenario is such: I'm developing this utility in VS2002 because it's the least common denominator.
For the VS2005 procedure I figured out how to instantiate the Solution2 part of the deal(I think):
<br />
DTE dte = (DTE)System.Activator.CreateInstance(System.Type.GetTypeFromProgID("VisualStudio.DTE.8.0"),true);<br />
Console.WriteLine(dte.Version);<br />
string tmpFile = System.IO.Path.GetFileNameWithoutExtension(Path.GetTempFileName());<br />
string tmpDir = string.Format("{0}{1}", Path.GetTempPath(), tmpFile);<br />
Type t = assem.GetType("EnvDTE80.Solution2");<br />
MethodInfo mif = t.GetMethod("GetProjectTemplate",new Type[] {typeof(string),typeof(string)});<br />
object result = mif.Invoke(dte.Solution,new object[] {"WindowsApplication.zip","CSharp"});<br />
string templatePath = result.ToString();<br />
mif = t.GetMethod("AddFromTemplate",new Type[] {typeof(string),typeof(string),typeof(string),typeof(bool)});<br />
result = mif.Invoke(dte.Solution, new object[]{templatePath, tmpDir, tmpFile, false});<br />
is equivalent to:
<br />
Solution2 solution = DesignTimeEnvironment.Solution as Solution2;<br />
string templatePath = solution.GetProjectTemplate(projectTemplate, "CSharp");<br />
Project proj = solution.AddFromTemplate(templatePath, tmpDir, tmpFile, false);<br />
I know what the following code does, it grabs a handle to the toolbox of the designated instance of the DTE, you then use the property toolbox.toolboxtabs to search through the tabs a for a matching tab name and if it doesn't find it then create the tab name. It's the first two lines that I can't figure out.
EnvDTE.Window window = DesignTimeEnvironment.Windows.Item(EnvDTE.Constants.vsWindowKindToolbox);<br />
EnvDTE.ToolBox toolbox = (EnvDTE.ToolBox)window.Object;<br />
ToolBoxTab CurrentTab = null;<br />
foreach (ToolBoxTab tab in toolbox.ToolBoxTabs)<br />
{<br />
if (tab.Name == toolboxTabName)<br />
{<br />
CurrentTab = tab;<br />
break;<br />
}<br />
}<br />
if (CurrentTab == null)<br />
{<br />
CurrentTab = toolbox.ToolBoxTabs.Add(toolboxTabName);<br />
}<br />
EnvDTE.Window and EnvDTE.ToolBox are interfaces; I thought you couldn't instantiate an interface?
Also EnvDTE is not an instanced object per say but just the class, so how would I construct
similar code using reflection? I tried using a similar approach as I did above with getting the Solution2 but I've gotten nowhere with it. I just can't seem to wrap my head around how to get the handle to the toolbox and then utilize the toolboxtabs property.
I've a feeling this is deceptively simple and I'll be wearing the Code Project Dunce Hat later but I've been banging my head against this for a week or so now on and off and finally decided I need a more experienced person's input.
“I know that I am intelligent, because I know that I know nothing.” ~Socrates
|
|
|
|
|
I haven't worked with integrating into VS before, but I think I help explain some of the code. The first line:
EnvDTE.Window window = DesignTimeEnvironment.Windows.Item(EnvDTE.Constants.vsWindowKindToolbox);
The class/interface DesignTimeEnvironment has a property called Windows (which I'm assuiming are the windows in the IDE). The type of the Windows property has a method called Item. This method takes a constant enumeration to determine which type of window to return.
Now the Item method is returning an interface (of EnvDTE.Window as you say). This doesn't mean the interface was instantiated. The IDE most likely has a class for the Toolbox window, but only exposes to external applications through the interface.
So to use reflection, you would need to get the Windows property (through GetProperty) then execute the Item method.
The same concept applies for the interface on the second line. Internally it's using a class, but only exposes it using the EnvDTE.ToolBox interface.
Take care,
Tom
-----------------------------------------------
Check out my blog at http://tjoe.wordpress.com
|
|
|
|
|
OK this is getting frustrating. I've been experimenting with using triggers for a MSMQ project and thought I'd start with something simple. So I created a private queue on my machine, created a rule that would open an exe (calc.exe), applied it to a trigger on the queue and sent a message to it. Nothing. The message gets to the queue, but the trigger never fires. I've looked high and low for examples, found few, and tried to follow them, but the outcome is always the same, the trigger never seems to be fired.
Anyone have some experience with MSMQ Triggers? What is going wrong here?
only two letters away from being an asset
|
|
|
|
|
Do you have Triggering installed and started?
|
|
|
|
|
Wouldn't be able to add a trigger if not.
only two letters away from being an asset
|
|
|
|
|
Check your task manager. I suspect that Calc is being started up "invisible" under the Network Services user account.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
You are correct, sir. Thanks. The examples, of course, didn't mention this possiblity.
Now to do something more useful.
only two letters away from being an asset
|
|
|
|
|
You're welcome. Those examples, they're really great aren't they?
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Hello. I have this class that overloads the comparisson operator (== ). It is throwing me an overflow exception:
public class DataItem
{
private int _id;
...
public static bool operator== (DataItem item1, DataItem item2)
{
if (item1 != null && item2 != null)
return item1._id == item2._id;
else
return false;
}
public static bool operator!= (DataItem item1, DataItem item2)
{
return !(item1 == item2);
}
...
}
The problem is that in the operator== method, I validate that both items are not null, but (because of the comparisson of the object to null) it calls again the operator== method. So it seems that I cannot validate whether the objects are null or not. Anyone has an idea of what can I do about this?
Thanks in advance.
Hope is the negation of reality - Raistlin Majere
|
|
|
|
|
|
Thanks!
Hope is the negation of reality - Raistlin Majere
|
|
|
|
|
i have a datagridview on a form the single operation i want is that when the data displayed on the gridview the gridview.enabled command must be false but i want the scroll bars to be active i have done the following code
datgridview1.enabled = false; but it also locks the scrollbars both vertical and horizontal i want that this must be active
sas
|
|
|
|
|
As you can see it doesn't work that way. What is it you are trying to accomplish?
only two letters away from being an asset
|
|
|
|