|
Here is my question.
Not sure how to ask this really.
I have had very bad luck passing values between forms.
If I open a form and pass a value that works fine. But If I have to pass
some values back to a "Parent" form it does not work so well.
The only way that I have found to do this is using the registry.
Write values to the registry then from the parent window close the form
and read the values that were set.
Is there a better way to return a value from a form?
Thanks
Will
|
|
|
|
|
You can use public members as a starting point.
public class MyForm : Form
{
public int MyTestInt;
}
public int MyMethodInAnotherForm()
{
int returnValue = 0;
MyForm dlg = new MyForm();
dlg.MyTestInt = 0;
if (dlg.ShowDialog() == DialogResult.Ok)
{
returnValue = dlg.MyTestInt;
}
dlg.Dispose();
return returnValue;
}
|
|
|
|
|
Thank you for the reply.
I think I asked this wrong..
I have a Main Form lets call it FormMain.
On this form is a TextBox where a user will enter in a Part Number
Now I have to look for the Part Number. If I can not find an exact match I can
A) Display a list of Part Numbers that are close
B) Allow the user to enter a New Part Number
In order to do this I open another Form.. Lets call it FormPart
Now On FormPart lets say that the user decides to add this New Part, so they input
the Description.. and other information. Now the Stored Procedure that I use to enter the
New Part in the database returns the ID Number for that Part. I need to send this ID back to FormMain and continue processing. It is really easy to send a value to a form, It is also easy to return a value if there is no end user interaction. But because of the type of application, I have to allow the user to either Select an Existing part or enter a new Part.
Again thank you for the answer
Hopefuly I have described my problem better this time. If I can find a solution I will post it. But from where I am looking, the registry looks like the best place for temp storage of this ID
Will
|
|
|
|
|
Will,
You are thinking to hard and missing the simple solution. I have coded hundreds of dialogs that process similar logic.
Just because your second form processes data does not mean it can't store the result (PartID) in a public member variable for the other form to pick up. If you are using Modal dialogs (ShowDialog()) then the example template I gave you will work well.
If you are using Modeless dialogs, then the second dialog needs to call back to the first (simply pass a reference to the first form in the constructor).
Think of the question you are asking FormPart: "What is the PartId the user has chosen?"
It doesn't matter to the first form if the part was just created or has been in the database for a century. The first form just needs a valid PartId. Have FormPart do whatever processing it needs to do and store the resultant PartId in a public member variable.
If you are still having trouble, I would be happy to trade a few samples with you until you get the idea. Using the registry is a very sloppy way of handling data transfer within an single process. I would hate to see you continue on that path.
You can contact me directly at mdpotter55@yahoo.com
|
|
|
|
|
The simplest way is to pass a reference of the first form to the second form, either by passing it to the other form's constructor or assigning it to a property. Don't just use Form , though. Declare the type of the parameter or property the actual type of your form so that you can easily access public or internal members.
Typically, though, it's easy to "return" a value. For instance, you could declare a property that can get (or optionally set) the part name for which similar part names should be found. When that form returns, get the value (which the popup form sets):
private void someButton_Click(object sender, EventArgs e)
{
using (PopupForm form = new PopupForm())
{
form.PartName = "asdf";
form.ShowDialog(this);
someTextBox.Text = form.PartName;
}
} Using this approach allows you to reuse that popup form from other forms when you need it.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Is there an event that lets you know when a new row is being added to a datatable edited in datagrid? There is an event for RowDeleting. Any way to know when a row is being added?
|
|
|
|
|
It's actually on the DataTable class, along with several other events. The DataGrid is implemented abstractly, working with any data source that implements IList or IListSource . Anything specific to how that data is manipulated would be specific to the data source. In this case, all the events you'd need for knowing when a row was inserted, changed, or deleted are defined on the DataTable class. See the class member documentation in the .NET Framework SDK for more information.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
As far as I can see there is no event for when a row is inserted. If there is can you tell me what the event is called. anybody?
|
|
|
|
|
Handle DataTable.RowChanging or DataTable.RowChanged (whichever is appropriate). The DataRowChangeEventArgs.Action specifies whether a DataRow is being added, changed, committed, deleted, or rollbacked.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
It is possible to lock the Excel process my app creates at runtime so that if the PC user opens another Excel file it does not get opened into 'my' excel process?
Would this be an Excel thing that I can control?
|
|
|
|
|
There is no way to "lock" the Excel process.
You can't really handle the WorkbookLoad event either, because that event fires AFTER the new workbook is loaded...
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
I've had this crazy idea in one of my personal projects to have a go at skinning a bit like WindowBlinds (think just my app for now).
My question is how do you draw on the area outside of the ClipRectangle? I need to be able to draw where the current XP theme is usually drawn.
For example where the control box is on a form.
Anyone got any ideas on this?
Thanks
|
|
|
|
|
You need to override WndProc in your Form derivative and handle the WM_NCPAINT (0x0085) notification message. See the documentation for the WM_NCPAINT message in the Platform SDK in the MSDN Library Online[^] for more information about the WPARAM and LPARAM values. I will tell you the WPARAM is the HRGN (handle to a region) that you can wrap in a .NET Region class using the static Region.FromHrgn method. To get a Graphics object, P/Invoke GetDCEx to get the HDC (handle to a device context) for the HRGN . You can then use Graphics.FromHdc to get a Graphics object. Just be sure to call Graphics.ReleaseHdc then Graphics.Dispose when you're done drawing otherwise you'll have a memory leak.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks i'll give that a shot
|
|
|
|
|
I am working on this same issue. I use a standard form with no title bar or controls. I then put placeholder images for the standard items like titlebar, control box, form control buttons. I then read in from a skin file what images to use for what items and where the position needs to be. This way I can even use a background image that is non-rectangular with regions to create a nicely skinned application.
Yes, I program in VB, but only to feed my addiction to a warm place to sleep and food to eat!
Visit my Code Project blog (Mobile Audio project)[^]
|
|
|
|
|
I would like to retrieve the IP address assigned to my machine from a VPN connection. Obviously I can go Dns.GetHostByName(Dns.GetHostName()).AddressList to get the list of addresses assigned to my local machine, and this does indeed return both my LAN address and my VPN address, however, is it possible to distiguish in code which address is my VPN address and which one is my LAN address?
#include "witty_sig.h"
|
|
|
|
|
You'll either have to use:
1) The Win32 API IPHelper functions to iterate through the network adapters and get the addresses for each of them
or
2) Use the System.Management classes (WMI) to iterate through the adapters, looking for the VPN adapter. The class to find in the WMI query will be Win32_NetworkAdapter .
There is no ".NET Way" to do this...
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Shucks. Thanks for the information.
#include "witty_sig.h"
|
|
|
|
|
I have a form that contains 200 textboxes in a table format, all named the same except with a number appended (i.e tbName1-tbName200). If I want to process these textboxes in a loop to capture the text values, how should I do this? Do I look for all of the components in the form and examine each component to see if it matches the name I'm looking for; seems to me that this is a difficult way to find the actual textbox object.
--Mike
|
|
|
|
|
You could add a Panel or GroupBox behind the TextBoxes, then just iterate through the control in the Panel or GroupBox.
The following is just an shove in the right direction, I believe this approach will work.
private void CycleTextBoxes()
{
foreach( Control ctrl in gbTextBoxGroupBox.Controls)
{
TextBox tbTemp = (TextBox)ctrl;
if(tbTemp!=null)
{
}
}
}
|
|
|
|
|
You will get an exception. Instead, use TextBox tbTemp = ctrl as TextBox; The as keyword performs a safe cast, returning null if the cast isn't valid instead of throwing an InvalidCastException . If that cast wasn't valid, only then when tbTemp be null.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
The following is code for manipulating INI files:
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
...
...
...
public class INI
{
public INI(string Path)
{
INIPath = Path;
}
public string INIPath=null;
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section,string key,string val,string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath);
[DllImport("kernel32")]
private static extern int WritePrivateProfileSection(string section,string Entries,string filePath);
public void ClearSection(string SectionName)
{
WritePrivateProfileSection(SectionName,"",this.INIPath);
}
public void WriteINI(string Section,string Key,string Value)
{
WritePrivateProfileString(Section,Key,Value,this.INIPath);
}
public string GetINI(string Section,string Key,string DefaultValue)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section,Key,"",temp,255,this.INIPath);
if(i==0 && DefaultValue!="NOADD")
{
WriteINI(Section,Key,DefaultValue);
return DefaultValue;
}
if(i==0 && DefaultValue=="NOADD")
return "NOADD";
return temp.ToString();
}
}
|
|
|
|
|
Try to use this function
private void avoidTextboxes()
{
FieldInfo[] fields = this.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
TextBox _btn;
foreach (FieldInfo fi in fields){
if (null != (_btn = fi.GetValue(this) as TextBox))
{
_btn.Text += " was modified";
}
}
}
xedom developers team
|
|
|
|
|
Thanks to you all for your time and suggestions, I'll try some of them out. Since I'm processing an input form of 21 columns x 25 rows, for each textbox name, if I loop through all of controls to find the textbox's text value, I'll potentially be doing this hundreds of times for each one.
So what I may use is a HashTable to store the TextBox's name and the TextBox Object so I can look for the key and then if it's there, just get the TextBox object and it's associated Text value. Any thoughts on this?
--Mike
|
|
|
|
|
Hi Everyone,
Is it possible to create a dotNet MDI app (C#) and use pinvoke to
create win32 client forms? This would mean using the CreateWindowEx
function and possibly sending over the dotNet parent Handle. If you
can do this, what steps should I take and watch out for? For example,
do I need to send over the HINSTANCE of the app and/or do I need to
set the MdiParent to the win32 child. Lots of questions here. Any
help would be totally awesome!
Cheers guys,
DrD.
|
|
|
|