|
I having difficulty accessing the object from form1 to another form in WinApp Project, unlike in Visual Studio 6 that you can access directly the object by Form1.Object1.text.
How can I do this either in VB.NET or C# in WinApp Project?
Thanks
|
|
|
|
|
Hi,
You can access an object of one form in another by 3 ways...
1) Make the object public or internal
2) Keep the object private but expose a public property for the same.
3) Pass the object in the constructor of the second form and then in the second form declare a private variable, initialize it in the constructor and access the object.
class form1
{
private ArrayList array = new ArrayList();
....
...
button1_click(...)
{
form2 = new form2(array);
form2.Show();
}
}
class form2
{
private ArrayList objArray;
form2(ArrayList obj)
{
objArray = obj;
}
button2_click(...)
{
MessageBox.Show(objArray.Count.ToString());
}
}
Hope this code helps you to understand the 3rd possibility...
and also hope that I haven't misunderstood your question.
regards,
Aryadip.
Cheers !! and have a Funky day !!
|
|
|
|
|
Thanks for the response.... but i still don't get it....
This is the scenario, I have two forms one is a parent form and the second is the lookup form. I need to access one of the textbox in parent and retrieve the value given by the lookup vise versa.
How can I access the specific textbox in parent and lookup form?
Thanks again..
|
|
|
|
|
hi,
look what you can do in this senario is :
On the button click event (before showing the 2nd form)...
Store the value of the textbox in a public variable...
Pass the object the the parent class to the 2nd form thru constructor
In the 2nd form use the parent form's object to access the public variable of the parent.
Now to access the textbox of the 2nd form from the parent:
declare a public variable in the 2nd form and set itz value with the text box value...
In the parent form you already have the object of the send... bcoz you r showing the 2nd from the parent...
So use the object to access the public variable of the 2nd form...
for example:
// the parent form
class parent : Form
{
// public variable in parent form to hold the textbox value
public string textVal = "";
...
...
// button click event delegate in parent form
private void button_click(...)
{
// store the textbox value in the public variable
textVal = textBox1.Text;
// instantiate child form with parent object in the constructor
childForm form2 = new childForm(this);
// showing the child form as modal dialog
form2.ShowDialog();
// after dialog is closed accessing the textbox value of the child form
MessageBox.Show(form2.textValChild);
// destroying the child form
form2.Close();
}
}
// the child form class
class childForm : Form
{
// public variable in the child form to store the textbox value of the child
public string textValChild = "";
private Form objparent = null;
// child form constructor with the parent form object
public childForm(Form parent)
{
this.objparent = parent;
}
...
...
// in the button click event delegate
private void Textbox2_Click(...)
{
// displaying the parent form textbox value
MessageBox.Show(this.objparent.textVal);
// storing the child form textbox value
this.textValChild = TextBox2.Text;
//hiding the child form... don't close otherwise itz object will be destroyed
this.Hide();
}
}
Hope this helps you...
regards,
Aryadip.
Cheers !! and have a Funky day !!
|
|
|
|
|
hi,
I already pass the value of parent form to the second form.
This time the value passed by the parent form already displayed by the second form in textbox for editing. After editing the second form must pass this value to the parent form.
Now I need to pass the value of the second form back to parent form without declaring another object of the parent form.
Note : The Second form is in MODAL STATE or DIALOG STATE.
Thanks And More Power!
|
|
|
|
|
hi,
no need to declare another object of the parent form... look at my previous code carefully... I have set the value of the second form textbox in the click event of childForm and then instead of closing the second form I have hidden it...has come back to the parent form and then accessed the second form value from the parent...
Please go thru the code carefully to understand what I'm saying... read the Click events carefully...
regards,
Aryadip.
Cheers !! and have a Funky day !!
|
|
|
|
|
When creating the child Form in the Parent form, set the Child form's Owner property to be the parent.
Then make some get property in the parent or a set property in the child to be get/set whenever the Object in the parent form chages (using events usually does the trick..)
|
|
|
|
|
Hi every body!
I want to write a web chat program (.net), But i don't know how to begin!
have you get any source code or link about this? please send me!
Thanks!
xyz
|
|
|
|
|
Please use the search capabilities of Code Project and also try Google. No one here is going to do your searches for you. This is why your posts are being ignored.
Regards,
Eric C. Tomlinson
I won't dignify your question with an answer. Yes, I program in VB6, but only because I'm addicted to eating!
|
|
|
|
|
I have hit a stumbling block......in my code I create a number of tab pages on a tab control. This can differ from person to person.
On each tabpage I have the following:
- Panel1 (name tab?_panel1) which contains:
-- treeview (tab?_treeview)
-- listbox (tab?_listbox)
- Panel2 (name tab?_panel2) which contains:
-- listview (tab?_listview)
-- richtextbox (tab?_richtext)
The "?" obviously refers to the tabpage that owns the control (tab0, tab1 etc.).
Where I've gotten stuck is this....how do I refer back to these controls elsewhere in my code? Specifically....I'm using a common event handler, getting the name of the parent (tabpage) of the parent (panel) from object sender. So I *know* the treeview on tab0 sent the event....how do I do "something" with the listview that is on tab0 (tab0_listview) based on the event tossed by tab0_treeview?
Thanks.....
|
|
|
|
|
What I usually do in this type of situation is to give the controls a name that I can rebuild later (much like you do) and create a lookup routine to find them. Doesn't make much sense without an example, huh.
I would name the controls (as I create them) with a name that can be easily parsed from within any event handlers invoked by the control. In keeping with your example,
Panel1 would be named 'tab_1_panel' and would contain
-- tab_1_treeview
-- tab_1_listbox
Panel2 would be named 'tab_2_panel' and would contain
-- tab_2_treeview
-- tab_2_listbox
In this way when an event is fired, I can parse the '1' or '2' out of the name of the invoking control, build the name of the control I need to access and look it up withing the parent's 'control' collection. If the tab_1_treeview fired an event and I needed access to the tab_1_listbox control, I would do the following from the event handler:
Step1 - ParseName
-- string[] sValues = oSender.Name.Split('_'); // returns an array with 3 elements 'tab', '1' and 'treeview'
-- int nIndex = sValues[1];
Step2 - Build listbox name
-- string sName = "tab_" + nIndex.ToString() + "_listbox";
Step3 - Retrieve control from the parent's controls collection
-- ListBox oListbox = this.Parent.Controls[sName];
Let me know if this works for you
|
|
|
|
|
Lets say i have a program, and i want ot be able to minimize it to the tray, so when the user right clicks on it, it brings up a menu. How would i do this?
//Otis
|
|
|
|
|
Check out the <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsnotifyiconclasstopic.asp">NotifyIcon</a>[<a target=_blank title='New Window' href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsnotifyiconclasstopic.asp">^</a>] class.
-Nathan
---------------------------
Hmmm... what's a signature?
|
|
|
|
|
Thankyou for uhh... notify'ing me of that :P
//Otis
|
|
|
|
|
Otis_69 wrote:
Thankyou for uhh... notify'ing me of that
Hope it helps...
-Nathan
---------------------------
Hmmm... what's a signature?
|
|
|
|
|
Hey, me again, just wondering iff anyone knows how to get rid of the console window... Im making my forms dynamically, using a console application, but if the console window shows, it isnt as attractive...
//Otis
|
|
|
|
|
I think you get what you want, if you create a Windows application and fill the Main-method with some other code before showing a form.
<br />
static void Main() <br />
{<br />
System.Windows.Forms.Application.Run(new MainForm());<br />
}<br />
www.troschuetz.de
|
|
|
|
|
I know, but i made a console program, its an irc server, and now i want to make it use a tray icon, so you can tell tyhe status of the server, but i already coded 75% of it in console, and i cba to switch to a windows application... Isnt there a compiler directive to hide the console?
//Otis
|
|
|
|
|
Don't know a direct way to do this, cause I never coded a console application with .NET. Maybe you could use some API methods to hide the console window.
Is it so complicated to copy the "console" code to a Windows Application to reach the effect you want.?
www.troschuetz.de
|
|
|
|
|
yes. One, is file size, and i dont want the program to be loading all the stuff i dont need. File size saves time (downloading and running) and saving time means more users (most likley me and a bunch of friends) will want it to be fast, small, etc.
Would it be possible to grab the process and set it to hid somehow through that?
//Otis
|
|
|
|
|
OMG, this is my second time writing this, cause last time i wasnt logged in, and i clicked submit, and it said i needed to login, so i hit back to copy/paste my post into notepad, and it was gone... arrgh. Anyways:
I have a class, lets call it "IrcBot". Now, i instanciate it like this:
IrcBot mybot = new IrcBot("server","channel","nick");
now, in my main class, i make a thread (or 10) all going to the same, or different irc servers... like this:
Thread t = new Thread(new ThreadStart(mybot.connect));<br />
t.Start();
seeing as there is no way to terminate the connection from the bot to the irc server, i cant terminate the thread, cause the thread is still doing something... In english, when i do t.Abort(); it doesnt stop and the bot is still in the irc server in that channel... How can i force the thread to stop regardless of whether its doing something or not?
Thanks for the help :P
//Otis
|
|
|
|
|
What i did now, is i made an instance of a timer within my thread, and set the interval to 1 millisecond...
System.timers.timer t = new System.timers.timer(1);
Then i made the event handler thing
t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
and the elapsed method looks like this:
private void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)<br />
{<br />
if(System.Threading.Thread.CurrentThread.ThreadState.Equals(System.Threading.ThreadState.AbortRequested))<br />
{<br />
connection.Disconnect("Gotta go!");<br />
} <br />
}
Why wont that terminate the connection & die when i call abort from the main thread?
//Otis
|
|
|
|
|
Well, i finally figured it out...
In the IrcBot class, i made the variable holding the connection public. Then, before i started the thread, i grabbed that connection, and stored it in a variable. Then when i want to abort the thread, i use the variable and disconnect, and then abort It is kinda crude, but it works
//Otis
|
|
|
|
|
What would you suggest as the easiest way of converting a stream to a string?
|
|
|
|
|