|
Use SelectedIndex or SelectedText
--Colin Mackay--
"In the confrontation between the stream and the rock, the stream always wins - not through strength but perseverance." (H. Jackson Brown)
Enumerators in .NET: See how to customise foreach loops with C#
|
|
|
|
|
thanks man, u helped me alot, i am a beginner in vb.net
hope the questions i asked wont make u feel boring...
|
|
|
|
|
ok, I'm completely new to VB and have come up with a problem on something that I think it really easy.
What I have are 2 forms, form1 and form2. Form2 gets called from a context popup on a listview on form1. Now, on form2 I have a textbox variable that I want transfered back to the listview on form1 when you click on the OK button on form2. However I am unable to access the listview on form1 from form2. I figure it's a scope issue. I have found a work around on MSDN that works, but to me it seems wrong.
Private Sub MenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem6.Click
Dim frmAction As New fclsRoomAction
frmAction.ShowDialog()
lstRoomAction.Items.Add(frmAction.tbActionDesc.Text)
End Sub
Here I have called my 2nd form, but I set my listview with the variable AFTER I have closed the form. Just dosn't seem right to me because I have closed the file BEFORE I read my variable. I think this has something to do with the way .net does garbage upkeep. I'm not sure. Like I said I'm new to this.
So... Now I ask. Is this the standard way of passing variables between forms or is there a more elegent way of doing it. I was originally hoping to be able to do it on form2 as there I can create 2 buttons, accept and cancel, which determine whether I copy the variable over or not. But as I cannot access my listview on form2 I was unable to do this.
Hopefully this makes sense. Thanks.
Randy.
|
|
|
|
|
The general way I'd do this would be to create a property for each value that I want to expose on Form2. Create an event handler for the Closing event and populate the values that the propery will expose.
e.g.
'The value to return to the other form
Private myValue As String
' This is the propery that exposes the value needed by
' the other form
Public ReadOnly Property ValueForOtherForm() As String
Get
Return Me.myValue
End Get
End Property
' When the Form closes ensure that the value is transered so
' that the property works.
Private Sub Form2_Closing(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) _
Handles MyBase.Closing
myValue = Me.tbSomeControl.Text
End Sub
Then, back in Form1, you can do this:
Private Sub MenuItem6_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MenuItem6.Click
Dim frmAction As New fclsRoomAction
frmAction.ShowDialog()
lstRoomAction.Items.Add(Form2.ValueForOtherForm)
End Sub
I hope this helps.
--Colin Mackay--
"In the confrontation between the stream and the rock, the stream always wins - not through strength but perseverance." (H. Jackson Brown)
Enumerators in .NET: See how to customise foreach loops with C#
|
|
|
|
|
Thanks for the help. I'm pretty new to the oop aspect of VB, but I assume thats what you were describing there as I know that properties in objects have get/set.
So after spending some time reading up on objects and browsing your code, I understand whats going on, but it troubles me how I'm able to reference a variable from a form that dosn't exist anymore. It's been closed after all and all variable on that form should be gone. It just feel wrong for me to code like that. Is this how VB.net is normally done? Can't you reference a control (eg. textbox) that exists on form1 from form2 and put it directly into it's text property without having to create storage variables? I can't seem to get it to work. I must be missing something...
Randy.
|
|
|
|
|
MadCow wrote:
it troubles me how I'm able to reference a variable from a form that dosn't exist anymore
The form "object" exists, but the controls don't. One of the more difficult concepts to understand is that there is managed (that's .NET) and unmanaged (that's Win32 API) stuff going on. The .NET Framework hides most of the Win32 implementation but you do get the occasional glimps that it is there sitting in the background.
Let me try and explain better. The WinForms part of .NET Framework is a wrapper around the Win32 API which is what drives Windows. When the form is closed .NET cleans up all the controls by disposing of them immediately rather than wait for the garbage collector to do it. [The way controls work in Win32 mean there is actually limited space for them so it is very important to let the system know when you are done with them]. The Form object still exists, but the underlying (Win32) controls on it don't because .NET has told Windows they are no longer required. The managed things, like the exposed string propery do still exist because they have no unmanaged element.
MadCow wrote:
It's been closed after all and all variable on that form should be gone
Just because a form has been closed doesn't mean it doesn't exist. It just means that the User Interface part is, well, "Closed".
MadCow wrote:
Is this how VB.net is normally done?
Personally, I'd create a controller object to store all that stuff and access it from the various forms that need it. I don't know if this is going to be too much information to take in at this point, but you could also read this article about the Model-View-Controller pattern[^] on Microsoft's web site. It also has links to other related articles about how to write good software.
MadCow wrote:
Can't you reference a control (eg. textbox) that exists on form1 from form2 and put it directly into it's text property without having to create storage variables? I can't seem to get it to work
I'm guessing that you need to cast the variable referencing form1 into the correct type. You probably have the base class in the .NET framework and you need to cast it to the specific type to access anything that is added in the derived class.
I'm sorry that I am not able to recommend a good book on OO concepts, I learned all that stuff years ago and all I have is lecture notes. However the article mentioned above is in a section of the Microsoft Web site[^] that contains a lot of examples of good Object Orientation.
I hope all this helps and my length reply, and even lengthier set of further reading material has not put you off.
--Colin Mackay--
"In the confrontation between the stream and the rock, the stream always wins - not through strength but perseverance." (H. Jackson Brown)
Enumerators in .NET: See how to customise foreach loops with C#
|
|
|
|
|
Thanks for all of the help. I checked out those articles on Microsoft's site, but as you said, it's a bit beyond me right now. But I have it bookmarked and will go back to it in a few months.
I ended up just using the method that dosn't make me feel comfortable. I figure if it works I won't worry about it. The explanation that you gave for a form being closed, but not necessarily non-existant helped.
And I will look into casting my reference form object. You had it bang on when you said that I was trying to do stuff with my base class object. Right now I'm just working on other stuff and leaving that form stuff for a while. I need to work on something thats going to work for a change.
Thanks again for all the help! Take care.
Mooooo. Grrrr.. Mooo..
|
|
|
|
|
Hi
I have determined how to obtain the free space available on the drives of a local machine. Is there someway I can obtain the same for a remote machine
Need urgently.. Any help is appreciated in advance
Thanks!
NEVILLE GEORGE
|
|
|
|
|
You could use Windows Management Instrumentation. You also have to have rights to the target machine to get a connection to it.
RageInTheMachine9532
|
|
|
|
|
please tell me how to add checkbox or radio button to a specific column of listview.if it is possible for any one then please give me small sample code to implement it using vb.net.
tbhattacharjee
|
|
|
|
|
i got .htc file along with asp.net project and i found there is instruction written in vbscript.so please tell me what kind of file it is and what the use of this file and tell is it client side or server side file.
tbhattacharjee
|
|
|
|
|
please tell me in detail.
tbhattacharjee
|
|
|
|
|
For COM, see here[^].
For COM+, see here[^].
RageInTheMachine9532
|
|
|
|
|
please tell me how to sort the datagrid when i will click on any column header.if it is possible for any one then please give me small sample code to implement it using vb.net.
tbhattacharjee
|
|
|
|
|
When the "AllowSorting" property is set to true, a triangle appears in each column header indicating the direction of the sort. The user can click on any column header to sort the grid by that column. Clicking the column a second time changes the direction of the sort.
make sure "Allowproperty" is set true
shihab
|
|
|
|
|
Doubt on Multilingual: We are developing a software which has facility of mulilingual support.We are using the Language and Localizable properties for this option.In this every time if we had some user defined captions ,i have to change in the certain Resx file of a form.so that if i compile this the changes will take effect.But now i need to compile it automatically.
ie)I have to acess the resource file which will be as same like as the form's resx file.
i will be thankful if anyone can help on this.
Shabu
|
|
|
|
|
I have written a class which has:
Sub New: Set Property "Disposed = false"
Sub Dispose: Cleans up and sets property "Disposed=True"
sub Finalize: If Property Disposed = False throws expection
Property Disposed
The class basically stops you from letting object go out of scope without first Disposing them. So in all my uses of the above class I do this....
Sub MySub ()
Dim lObject as MyClass()
....
lObject.Dispose
End Sub
Its impossible for this object to descope without me having called the Dispose method. YET!...In my live code, which is obviously a little more complex, I have put some message boxes in so I can do a simple trace. First of all, my live code draws buttons, and I get loads of mesage boxes coming up, where I thought they would come up one at a time, this suggests to me that every button is being drawn on a different thread, am I right?
Next, how the heck can I find out why my class is being destructed before my sub dispose is called. The thing is when I look at the call stack it only has the current method on it and no parent calls. That's doing my head in.
I started to get lots of problems on my project when I began using the DefaultValue attribute. This is a great feature, I love it, but am I taking the right approach? You see, when .NET starts my app in design mode it fires off lots of property sets. Each time a property changes, in my property handlers, I call a sub called PropertyChanged. That is responsible for invalidating my control area and redrawing. Should I be doing it that way? I instinctively feel I should not, because I want all the properties to be set, THEN, I want to draw. I dont want to redraw each time because the location changes, then again when the forecolor changes then the shape of the button changes. I feel SuspendLayout and ResumeLayout will be used, but I don't know how, because when the user changes properties like color I dont think OnLayout is called, so if my control is only redrawn in OnLayout it wont be redrawn to reflect new values when non-layout type properties change.
Guys, I am sorry this message is such a mish mash, and thank you for your patience. You can see my frame of mind is somewhat dazed!
Chris.
Nursey
|
|
|
|
|
OK...Well, as far as I can tell, your method of "preventing an object from going out of scope" won't work. Scope isn't controlled by how long an object lives, it's the other way around. As an example:
Public Class MyObject
Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click
Dim strText As String
strText = RichTextBox1.SelectedText.Replace(ControlChars.Lf, ControlChars.CrLf)
Clipboard.SetDataObject(strText, True)
End Sub
End Class The object 'strText' goes out of scope when you get to End Sub because strText was declared inside the sub and is not visible outside this sub.
But if you change when the object is declared, you also change its scope:
Public Class MyObject
Dim strText As String
Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click
strText = RichTextBox1.SelectedText.Replace(ControlChars.Lf, ControlChars.CrLf)
Clipboard.SetDataObject(strText, True)
End Sub
End Class Now, strText won't go out of scope until the class it's in is destroyed. It is now visible to all the subs/functions in the class.
Are you trying to keep an object from being destroyed before your finished with it? We need to see a little sample code to figure out whats going on.
Buttons: Buttons (and just about all controls) are created on the thread the form is on, not seperate ones. We need a couple more details about what your ultimately trying to do.
RageInTheMachine9532
|
|
|
|
|
Hey Rage,
I think we are about to run up each other's backside here 8-)
I understand all about scope so I am not sure what you're trying to explain. I also understand that I am typing questions like a shell shocked idiot 9-)
My object get's finalized apparently without ever seeing a call to Dispose. So this is a sequence issue. My sub Finalize was called without me ever having called Dispose, that's impossible when I look at my example sub routine. The odd thing is the only call on the stack was Finalize, so who the hell called it? If the CLR called it then why did it? I haven't let the object go, if I had I would have called finalize first.
Look, I think this problem has strangely gone away...but the very strange thing is that when I put my MsgBox trace in a ton of them came up, they didn't come up one at a time and wait for one to be dismissed before the next one came up. That's what made me think loads of threads had been created to draw each of my controls.
If that was true I would have layed an egg! 8-)
I think for somereason, which is most likely attributable to me hitting a problem then moving to a different part of my project whilst a I think about it, I just seem to get strange behavior, which when my code stablises again, things all seem to come out shiney and new again!
Still, I have a problem of having toolbox bitmaps assigned to my controls in the control library and only two of the four controls display their bitmaps.
At design time, the enabled property of my control seems to stay at true, even when the property grid says false. These are all things that when I return to sanity, I am sure will get worked out, but sometimes its very hard to believe things are as stable in .NET as they should be when at times my Property window won't open, until I exit and restart, or I start to see call stacks with only a destructor on them. All these little "sometimes" bugs just unnerve me.
Thanks anyway.
Nursey
|
|
|
|
|
Your right about the CLR. The garbage collector will call Finalize (never calls Dispose) when your object falls out of scope. You can't really hold it open without it actually being referenced. I haven't seen the code so I don't know exactly what your doing. I don't mean to make anyone out to be stupid, and, frankly, I wrote the entire message and posted it before I figured out it was you. (Just look at the name - Duh! ) Sorry!
RageInTheMachine9532
|
|
|
|
|
s'ok mortal, I forgive yah! 8-)
The whole point is that I was calling Dispose before the CLR could finalize it. It's horrible when problems like this just go away though, coz you wonder what the hell your code could be doing that would cause this. It's like returning to the bad old days of C/C++ and corrupting memory or the krapping on the stack! Ah, them were the days.
Perhaps I need to get my hands on a decent trace tool. Do you know of any good free trace tools out there?
Nursey.
Nursey
|
|
|
|
|
I remember those days! Oh what fun!
Try overriding the Finalize event and move the Displose code to it?
Can't say I know of any good trace tools that don't cost an arm and a leg!
RageInTheMachine9532
|
|
|
|
|
I was making a custom drop-down list in a property grid by using a StringConverter and overriding the GetStandardValuesSupported , GetStandardValues and GetStandardValuesExclusive methods ... it worked fine in a normal VB.net Windows application and gave me the drop down list .... but when I tried using it in a add-in for FrontPage, the same code did not give a drop-down menu but just a text box ....
anyone has any idea why???
Thanks in advance....
|
|
|
|
|
I am a new programmer with VB. I need some help. This is what I ultimately want to do. I want to write a .exe which will copy a program to a specific folder, and then launch that program inside the folder. That program causes a pop up screen to emerge with a button on it that says execute. I want the.exe file to automatically click on the execute button without having to prompt the user and then close that program it opened.
If someone could help me out I would greatly appreciate it. Thank you very much for your time.
|
|
|
|
|
I have a need to access the properties of controls in a VB6 executable from VC++. I've tried various MFC/Windows calls to do this, and know the classnames of the windows that correspond to the controls, but haven't managed to find a procedure that works as of yet.
If anyone has a clue on how to do this, I'd appreciate suggestions... thanks.
Bob
|
|
|
|