|
hi there, im having trouble trying to pass strings between my windows forms...
i have 2 simple forms, the first has a textbox and a button on it, and the second has a label on it. When the user inputs something into the textbox on the first form and presses the button i would like the text in this textbox to be passed to the 2nd form and be displayed as the label text.
Any help is much appreciated.
|
|
|
|
|
There are many ways to do this, but the most flexible is for the first form to signal an event that is handled by the second, passing the string as an argument. You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
|
depends.
1. Is the form2 being opened when the button is pressed in the form1?
in that case, you should pass the string through constructor of the form2.
somthing like:
class Form1:Form
{
button1_Click
{
Form2 form2 = new Form2("My Text");
}
}
class Form2:Form
{
public Form2(string caption)
{
this.Text = caption;
}
}
2. If the two forms are open together, then it is a bit tricky. You will need to either implement custom events or use static class with timer.
|
|
|
|
|
first is easy,
but for second option I would like to continue ur idea
if both forms are declared globally so each form can use their resources....
so at reload display the desired text.Syed Shahid Hussain
|
|
|
|
|
Bad idea!
Firstly, there are no global variables in C# - for good reason!
Secondly, exposing form internals so that another form or class can access them is bad practice, as it means that the two classes are tied together - you cannot safely change one of them without potentially affecting the other. It is much better to keep internals private, so that nothing outside the class relies on them.You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
the two forms are open at the same time which is why i am having trouble...i've tried passing them using the constructor but this doesn't seem to work and i just get the error coming back saying..." 'Pass.Form1' does not contain a constructor that takes '0' arguments"
|
|
|
|
|
heres an update of my problem since i have tried to change it a little...
i have two simple forms, the first just has just a button on which opens the 2nd form so the 2 forms are open at the same time. On this 2nd form i then have a text box and another button, the user will enter some text into the textbox and then press the button. Once this button is pressed i would like the text to be passed to the first form. I have tried using the following code...
Form1:
public partial class Form1 : Form
{
string text;
public Form1(string receivedText)
{
text = receivedText;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
this.Hide();
f.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
if (text == "")
{
}
else
{
label1.Text = text;
}
}
}
Form2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f = new Form1(textBox1.Text);
this.Hide();
}
}
This does not seem to work though and i get the error..." 'Pass.Form1' does not contain a constructor that takes '0' arguments.
|
|
|
|
|
you probably instantiate form1 with no paramters (not in the code you posted here but at start of your main app?) . Change the text new Form1() to new Form1("") or something.
BTW: if you just doubleclick on the error message it should point you to the row where the error occurred.
|
|
|
|
|
ahhhh thankyou yes that worked, should really have noticed that, thankyou
|
|
|
|
|
There are a number of things wrong here!
What happens if I click the button in form1, then the button in form2, then the button in form1 again, and so on?
Because you are creating a new instance of the other form when you press the button and hiding the current form, you just end up creating an infinite series of hidden forms. When you eventually close the displayed form, you application does not exit, but the user can't see anything on screen, and has no way to get back to any form from which he could actually close the app.
If you must do it that way, then Form1:
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
Hide();
f.ShowDialog();
labelIWantDataIn.Text = f.Data;
Show();
}
Form2:
private void button1_Click(object sender, EventArgs e)
{
Close();
}
And provide a property to access the textbox text.
I would still do it via an event though!You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
try this code:
<b><u></u>Method 1:</b>
on page 1:
Session["Value"]=textBox1.text.tostring();
on page 2:
retrieve value by using
label1.text=Session["value"].tostring();
Method 2:
On page 1:
response.redirect("default2.aspx?value='"+textbox1.text+"'");
on page 2:
string str=request.queryString("value").tostring();
label1.text=str;
|
|
|
|
|
On application i am working there presents two gridview more like master-detail form. When user click on row in master grid, its details got displayed in second grid.
Now i want to export these to excel.
I can export both grid to different excel files, i.e one excel for master grid, and other for detail.
But i want to export them in single file.
Is there a way to achieve this?
need help.
|
|
|
|
|
Can I have code U r Using for Export to Excel? Rashmi.M.K
|
|
|
|
|
sure there is. Look at this article. It specifically for exporting datagridview(s) to excel.
As for two datagrids on the same file I wouldexport one to sheet1 and the second to sheet2 or
Mater and Details.
|
|
|
|
|
thanks for reply.
exporting to excel is not an issue.
also exporting them to separate files is ok.
But my problem is how to correlate them as i do in my application.
Hope you had seen master detail standard ex for notrhwind DB.
Now whenever user click on particular order, its corresponding details displayed in 2nd grid.
I also doing this, and exporting them also(but in 2 files). NO ISSUE.
But how to correlate them in excel file.
so that user can identify this detail belong to that order.
|
|
|
|
|
I would export them as I mentioned and then record a macro for some VLookUP or something
(I don't know what you want exactly). An then adapt the macro to C# code so that the exported
files/sheets are already linked.
[Edit]
Or simpler. I would create a template file that contains all the formulas(Excel or custom VBAs)
and export the datas in that template. Now when the user enters a text in cell F5 for example
the formulas will autocomplete the details for that text. Something like that. It's easier than
creating formulas from C#.
[/Edit]modified on Tuesday, March 2, 2010 8:35 AM
|
|
|
|
|
Hi All
I'm trying to author a control which displays text vertically. I've overridden the OnPaint method and Text property so that if the Text is Changed it calls the new OnPaint method. When the text is changed, the old text does not disappear and the new text is overlaid on the old. How do I stop this from happening?
The OnPaint method looks like this
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
if (!String.IsNullOrEmpty(Text))
{
System.Drawing.Graphics formGraphics = pe.Graphics;
System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 10);
System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);
float x = 150f;
float y = 50f;
System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat(StringFormatFlags.DirectionVertical);
formGraphics.DrawString(Text, drawFont, drawBrush, x, y, drawFormat);
drawFont.Dispose();
drawBrush.Dispose();
formGraphics.Dispose();
}
}
And the Text property is
public override string Text
{
get { return base.Text; }
set
{
base.Text = value;
this.OnPaint(CreateNewPaintEventArgs());
}
}
private PaintEventArgs CreateNewPaintEventArgs()
{
return new PaintEventArgs(CreateGraphics(), new Rectangle(new Point(0, 0), Size));
}
Your help is appreciated.The FoZ
|
|
|
|
|
Don't call OnPaint! Never, ever, call OnPaint directly.
Use Invalidate instead - it causes the framework to correctly call OnPaint with the appropriate graphics object etc.You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
Thanks OriginalGriff.
I changed my Text property to
public override string Text
{
get { return base.Text; }
set
{
base.Text = value;
this.Invalidate();
}
}
and all is well.The FoZ
|
|
|
|
|
Hey,
I have my data in a grid (no datagridview), export a grid (30 cols and over 20.000 rows) takes a lot of time (minimum 2minutes)...
now my question: Is there a way to accelerate this?
for (int i = 0; i < UsingGrid.Cols; i++)
{
for (int j = 0; j < UsingGrid.Rows; j++)
{
worksheet.Cells[j + 1, i + 1] = ConvertToExcelFormat(Convert.ToString(UsingGrid[j, i]));
}
}
Thank you
|
|
|
|
|
|
Why can't you use Response.write with Inline or attachment file type to export data from Grid to Excel. Rashmi.M.K
|
|
|
|
|
|
Hi
I have string say "Deepak"
how do i convert this into a byte array of 44,65,65,70,61,6b. I converted the ascii to
hex-string "44656570616b" using
String.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
thanks
|
|
|
|