|
One of the best way to pass some value is by overloading the constructor.
For e.g if you want to pass an integer id or something like that from form1 to form2.
you need to do is make a small function on form2 like :
public Form2(int id)
{
InitializeComponent();
this.ID = id;
}
And while opening the form2 in form1 you need to do is
Form2 frm = new Form2(3); //3 can be replaced by any variable.
frm.showdialog();
or
frm.show();
|
|
|
|
|
|
The method that jaypatel512 recommended is OK for value types going from Form1 to Form2 but not recommended for reference types and unworkable for passing values back from Form2 to Form1.
For Form1 to Form2 (giving far more flexibility), use properties and/or methods in Form2.
For Form2 to Form1 use events in Form2 that are subscribed to by Form1.
Example using all the above - I've wired everything up to both form's click events.
using System;
using System.Windows.Forms;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Click += new EventHandler(Form1_Click);
}
void Form1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.FloatEvent += new EventHandler<FloatEventArgs>(form2_FloatEvent);
form2.Int32Property = 32;
form2.SetStringValue("Hello World!");
Console.WriteLine("Int: {0}, String: {1}", form2.Int32Property, form2.GetStringValue());
form2.Show();
}
void form2_FloatEvent(object sender, FloatEventArgs e)
{
Console.WriteLine("Sender: {0}",sender);
Console.WriteLine("Float: {0}", e.FloatValue);
}
}
using System;
using System.Windows.Forms;
public partial class Form2 : Form
{
public event EventHandler<FloatEventArgs> FloatEvent;
private string _StringValue;
public Form2()
{
InitializeComponent();
Click += new EventHandler(Form2_Click);
}
void Form2_Click(object sender, EventArgs e)
{
OnFloatEvent(new FloatEventArgs(Int32Property));
}
public int Int32Property
{
get;
set;
}
public void SetStringValue(string stringValue)
{
_StringValue = stringValue;
}
public string GetStringValue()
{
return _StringValue;
}
protected virtual void OnFloatEvent(FloatEventArgs e)
{
EventHandler<FloatEventArgs> eh = FloatEvent;
if (eh != null)
eh(this, e);
}
}
using System;
public class FloatEventArgs : EventArgs
{
private float _FloatValue;
public FloatEventArgs(float floatValue)
{
_FloatValue = floatValue;
}
public float FloatValue
{
get { return _FloatValue; }
}
}
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
It was exactly my problem!(Form2 to Form1)
Thank you very much.
|
|
|
|
|
No problem
Events (and their use of delegates) are a fundemental part of most modern OOP languages including C#.
Check out my article here[^] for an in depth step by step guide.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Hi i am building an application in which i want to generate a report. That report consists of data from multiple tables. I tried doing this but when the datagrid is populated with the dataset/datatable the record from second table comes on second row & from third table comes on third row. I am using access database. can someone help me out of this.
Thanks in advance. the code is as shown below.
private void Form1_Load(object sender, EventArgs e)
{
try
{
con = new OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=e:/ReportTest/ReportTest/ContractSystemDB.mdb");
con.Open();
ds = new DataSet();
dt = new DataTable("ashish");
cmd = new OleDbCommand("select * from CommonData",con);
rd = cmd.ExecuteReader();
if (rd.HasRows)
{
while (rd.Read())
{
da = new OleDbDataAdapter("select * from InitialApproval where InitialApproval.PrNo='"+rd.GetString(0)+"'",con);
da.Fill(ds);
da.Dispose();
da1 = new OleDbDataAdapter("select EstimatedVal,FwdDtByPSD from PSDApproval where PSDApproval.PrNo='" + rd.GetString(0) + "'",con);
da1.Fill(ds);
da1.Dispose();
da2 = new OleDbDataAdapter("select POValue,rcd_dtCC,saving from CommonData where CommonData.PrNo='" + rd.GetString(0) + "'", con);
da2.Fill(ds);
da2.Dispose();
}
}
contractSystemDBDataSetBindingSource.DataSource = ds;
MyGrid.DataSource = contractSystemDBDataSetBindingSource.DataSource;
MessageBox.Show("Record Added To Grid View");
}
catch (Exception ex)
{
MessageBox.Show("Error==>" + ex);
}
finally
{
con.Close();
}
}
|
|
|
|
|
Your question is not so clear . .but as much i can understand you want to show all the tables columns in one data grid...
For that you cannot use such a code but u may need to use UNION in your query.
So you need to select all the data in single query using union and then populate data ina single table.
You can google union .. you will get it . ..
It should be something like this :
da1 = new OleDbDataAdapter("select * from InitialApproval where InitialApproval.PrNo='"+rd.GetString(0)+"' UNION select EstimatedVal,FwdDtByPSD from PSDApproval where PSDApproval.PrNo='" + rd.GetString(0) + "' UNION select POValue,rcd_dtCC,saving from CommonData where CommonData.PrNo='" + rd.GetString(0) + "'", con);
da.Fill(ds);
|
|
|
|
|
Hi all
Im facing silly problems abt TabIndex.
Scenario is I have 5 textBox. 2 of them are in a container and other's are inside 3 tableLayoutPanel control. I want to line up or create a serial Taborder only to those all textBoxes.So that I can use key_press event on them.
If you not clear pls let me know
Thanks
|
|
|
|
|
I am not exactly sure what it is that you are asking, but it seems to me that all you need to do is to set the TabOrder for your TextBoxes to the order that you want them accessed in.
For your future reference, it is considered rude to use txtspk in CP forums (abt?, pls?). In this case I don't think it alters the understanding of your problem but it might do in another case. Remember, us old fogeys (who are often the ones that have the answers) mostly avoid posts containing txtspk. So pls dnt do it.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
I'm loading a listBox with a range (360 to -360), but I want to user to start off at value 0 (what they see). How can I do that? I've tried to use listBox.Items.MoveCurrentToPosition() and listbox.SelectedIndex = x; but neighter have the desired results.
|
|
|
|
|
See what no help and one day of sleep does?
listbox1.ScrollIntoView(value_of_collection);
is what the doctor ordered.
I swear I searched many times on CP, google but never seen one example of someone setting this. Quite strange....and even more strange that I overlooked the method so many times in intellisense/msdn.
Anyways, maybe that will help someone else.
|
|
|
|
|
Gawwwwd.
So I did that example at work a few days ago. Now I come home and work on my project and what do you know, it ain't working. Before I jump off a cliff I decided to look into it. 10,000 compiles and 10,000 "come on man!" later, and I finally got it. I mean, it's so irritating and funny when some little dumb thing like this happen. Now i'm certain I tried this method before posting my original post, but didn't work...yadda yadda.
drumroll please...
The reason is that I was using a tab container and this listBox was on the second tab, NOT in the VIEW of the application when it loaded.
So I ended up responding to the Tab events something like this:
private void tabControl1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
TabControl tc = sender as TabControl;
if (tc.SelectedIndex == 1 && selectedIndexTracker !=1)
{
selectedIndexTracker = 1;
angleListBox.ScrollIntoView(15);
}
}
|
|
|
|
|
Hi All,
I've got this on an abstract class:
public abstract IDictionary<string,MatchKey> MatchKeys { get; set; }
As I want consumers to be able to specify the type of Dictionary collection (that implements IDictionary<>) in the sub-class. When I inherit from this abstract class with this:
private Dictionary<string, MatchKey> _matchKeys = new Dictionary<string, MatchKey>();
public override Dictionary<string, MatchKey> MatchKeys
{
get { return _matchKeys; }
set { _matchKeys = value; }
}
I get a compile error "type must be IDictionary to match overridden member". Am trying to do something ridiculous here? Seems to make sense but it's obviously not correct.
Cheers,
|
|
|
|
|
Try using IDictionary instead of Dictionary in your derived class.
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia) Why are you using VB6? Do you hate yourself? (Christian Graus)
|
|
|
|
|
Indeed, that works. I was thinking that in the derived class I would be able to simply use Dictionary in its place.
|
|
|
|
|
That would break the "contract" though, the base class claims it can also accept other types of IDictionary when you really don't, so you would be going against the Liskov substitution principle (except the base class is abstract, but is that a good enough reason to not let it apply?)
|
|
|
|
|
I've just popped off to have a read on what that principle is! Interesting reading, thanks.
I was still under the impression that coding against Interfaces was a good idea. By letting consumers specify the IDictionary<T> there is a level of functionality guaranteed to be available irrespective of the concrete type used.
|
|
|
|
|
You're welcome. Interfaces aren't all bad though..
Personally I would restrict a type as much as possible (with as context, the rest of the program) unless the code is meant to be used be other people/programs
|
|
|
|
|
I'm actually working on some base classes that are going to form the data objects for a fairly hefty set of scenarios. They are used throughout our systems and need to be extensible in some situations and tied down in others (Wcf DataContracts for instance).
I've tried staying on one side of the road (Interfaces/Abstract Class) but I'm now experimenting with a little of both where appropriate.
|
|
|
|
|
If the abstract base class had just a getter, it wouldn't break the contract or the Liskov substitution principle, yet it still won't compile. C# doesn't support covariance of return types on overridden members.
|
|
|
|
|
I've got a real head scratcher here. One of my dialogs the text displayed at runtime begins one pixel farther to the left than at design time, and some spaces are 1 pixel wider at run time. I initially noticed this when the effect caused text to wrap differently.
When I created a new test form in the same project and copied the interface components into it, the problem persisted. When I created a new solution and copied the offending forms source into the new solution the problem went away.
This is happening in VS2k8. The solution it's occurring in was originally created in 2k3 and then converted to the new version.
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots.
-- Robert Royall
|
|
|
|
|
I remember reading an article somewhere (can't find a reference at the moment) about this sort of thing. IIRC it is caused by either the designer using GDI+ and the run-time using GDI, or vice versa.
This may not be the cause in your case, but perhaps if you root around using the above info, you might be able to resolve the problem, or discount it anyway. I'll continue racking my brain, and if I remember the source, I'll come back.
[Edit]
Brain finally started working.
Look at this[^].
Lots of other goodies from googling 'text alignment gdi vs gdiplus'
Good luck!
[/Edit]
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Interesting and that did send me down the right path. In the properties viewer under Behavior is an option called UseCompatableTextRendering . The description of it is "Specifies whether text rendering should be compatible with previous releases of Windows Forms. Setting it to true changes the rendering in the designer to match what's used in the original application in either case. Changing it in my new app (same form code) causes both the design and runtime rendering to be changed.
This strongly implies that there's a setting at the project/solution level that's overruling the designer and forcing legacy rendering in either case.
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots.
-- Robert Royall
|
|
|
|
|
<blockquote class="FQ"><div class="FQA">dan neely wrote:</div>This strongly implies that there's a setting at the project/solution level that's overruling the designer and forcing legacy rendering in either case. </blockquote>
and there is: To use the new rendering call: Application.SetCompatibleTextRenderingDefault(false).
http://blogs.msdn.com/jfoscoding/archive/2005/10/13/480632.aspx[^]
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots.
-- Robert Royall
|
|
|
|
|
Hi,
i used hl7 to xml converter article, it returns a xml document as a string.
can i get it as a document itself, how? how can i read the xml document and populate my database?
|
|
|
|