|
Hi,
from this and yesterday's posts, it seems to me you don't really have a clue about objects and Controls.
There is no "label's name" involved at all; if you want to operate on a Label, all you need is a reference to that Label; and I would guess Form1.Label1 returns exactly that.
I suggest you buy and study a book on C# or "Windows programming using C#"; in the time you have spent already you could have learned a lot by reading a structured book or course.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
I concede to the fact that I do not know enough about what I am doing. Which is why I am asking questions. I have tried to research and find proper solutions. But when I get stuck I have to ask.
"I would guess Form1.Label1 returns exactly that" is what I thought, so when it didn't work i got to checking things. I added a Messagebox, and Converted the variable being passed to a string and found I was getting this: Systems.Windows.Form Label, Text: Text on my Lable. Now this may be because I converted it to a string, but I think "Text on my Lable" is what is being passed versus Label1.
|
|
|
|
|
FYI: when you do string s="my label is "+label; then the compiler will add .ToString() to it, so string concatenation can be performed; and each class can override ToString() to return whatever string it likes to return; a label returns its class_name and its text. And not it's name.
As I said, variable names most often are irrelevant at run-time, references are the heart of the matter in object-oriented programs.
yogi_bear_79 wrote: Which is why I am asking questions.
Forums like these exist to provide an opinion on a general "how should I ..." type of question, or a pointer on a very specific question ("here is my code, I get 'A' instead of the expected 'B', what's up?"). In both cases the poster is expected to spend some time and effort in acquiring basic knowledge first. Hence: study a book so you know the basics of C#, the basics of OO principles, and the basics of Windows programming.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
("here is my code, I get 'A' instead of the expected 'B', what's up?").
First off, I am not trying to offend. My task was to convert a VB program to C#. I have completed everything except this one task. I have written the code (with help from the forumn, books and the Web). I am completey self-taught, and as a Network/Server administrator rarely do I delve into codeing. Alas, I will always be a rookie in this area. In an effort to keep the posts minimal I have only included snippets that I thought relevant. While I am sure my program could be written better by someone with more experience it does indeed work. I continually learn new/better ways to improve my codeing.
I belive my code should be passing the labels name via an event back to the form. Below I have provided everything I think is needed to see my attempt, maybe someone can see what I am doing wrong.
namespace VDP
{
public partial class frmMain : Form
{
private Poker poker;
public frmMain()
{
InitializeComponent();
poker = new Poker();
poker.UpdateLabels += new EventHandler<LablesEventArgs>(poker_UpdateLabels);
}
void poker_UpdateLabels(object sender, LablesEventArgs e)
{
UpdateLbls(e.Lbl);
}
public void UpdateLbls(Label lbl)
{
lbl.ForeColor = Color.Yellow;
}
}
}
namespace VDP
{
public class LablesEventArgs : EventArgs
{
public LablesEventArgs(Label lbl)
{
Lbl = lbl;
}
public Label Lbl
{
get;
private set;
}
}
}
namespace VDP
{
class Poker{
public event EventHandler<LablesEventArgs> UpdateLabels;
void Results(Label lbl_1)
{
OnUpdateLabels(new LablesEventArgs(lbl_1));
}
protected virtual void OnUpdateLabels(LablesEventArgs e)
{
EventHandler<LablesEventArgs> eh = UpdateLabels;
if (eh != null)
eh(this, e);
}
public void Hand_Analyzer()
{
frmMain Form1 = new frmMain();
Results(Form1.Label1);
}
}
}
|
|
|
|
|
this smells like the classic double Form mistake, although not enough code is available to be certain:
- Initially something is calling new frmMain() , probably your static Main method inside file program.cs
- this creates a new Poker()
- I'll be assuming somehow Hand_Analyzer() gets called on this poker
- now Hand_Analyzer() creates a new frmMain() which is NOT the original form, just a twin, and it is it's Label1 that gets set (although this second frmMain never gets shown on screen).
There are many solutions in this case; the one I prefer is:
- don't pass a GUI component to class Poker, it deals with cards and hands (that is "the business logic") and NOT with the user interaction;
- what you want to pass to the user is a string, hence come up with a StringEventArgs
- and modify UpdateLbls to UpdateLbls(string text)
Result:
1. it works
2. you can change the GUI (say use a RichTextBox or a ListBox instead of a Label) WITHOUT CHANGING class Poker.
That is "separation of concerns", important to get quality and reusability.
BTW: Did you notice you started in the completely wrong way (subject: Passing Label's Name...") as you didn't need the name of the Label, you did not even need the Label itself.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Ok, I tried this before I posted and got stuck as well. I just went through and changed everything to String. Now I am passing a Sting (i.e. "Label1") to public void UpdateLbls(String lbl). This is where I got stuck before. How do I convert the string into a Label so I can set the color?
public void UpdateLbls(String lbl){
lbl.ForeColor = Color.Yellow;
}
|
|
|
|
|
You should pass the result from the business logic to the GUI, so
if the result is a number, then put the number in your delegate parameter;
if, I repeat, if the result is a string (e.g. some text to show, NOT the name of a Control), then pass a string;
if the result is a color, then pass a color.
However, unless you are creating a paint program, I don't expect a color is the result of a calculation.
If you want to indicate a state, then the proper way to do it is by defining an enum holding all possible states, and then to output one of those enum values.
You DO need to study a book and start to understand what it is you do; I will not go on and type one page of such books at a time here.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
This is where I am confused I do want to pass the name of a control. My frmMain has a panel with labels on it. Each label indicates a winning hand. If a hand is won (i.e. Full House) then the label's forecolor should change from White to Yellow. Thus the hand analyzer decides if you have a full house. Then it passes the Label's Name that it wants the forecolor changed on.
Originally I thought I could use Form1.Label1 to pass the Labels name as a variable. That didn't work. So i tried Form1.Label1.Name, that passed the label's name, but as a string. I eventually settled on simply passing a string "Label1". All of these work to the point where the name is passed back to the Forms method UpdateLbls. However it is at this point that I need to be able to tell Label1.Forecolor what to do. But you cannot impliclty convert a String to Label. My book, nor the web has helped me on this matter.
|
|
|
|
|
1. in .NET a name IS a string.
2. in .NET programming you normally don't identify objects by name, you do by reference.
From what you now told, your business logic calculates a state (hand won, hand not won); so that information is what needs to be passed out towards the GUI, not a name, not a string, but either a boolean, or an enum representing all possible states (currently 2).
The next issue is, if only one hand can be winning, how and where to implement that.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Ok, after stressing you out all afternoon, I either caught a clue or took the cheap way out!
Basically, I created a method in my frmMain class. The method takes one argument, an int. The method looks at the passed variable passes through a switch block and updates the appropriate controls.
I pass the int from Hand_analyzer to results, I am using the public event EventHandler UpdateLabels; to pass the int to the frmForm class
Did I learn?
|
|
|
|
|
glad you made some progress.
yogi_bear_79 wrote: Did I learn?
I don't know; maybe yes and no, yes you learned to be creative and got it to work somehow, and no you most likely did not learn the right way to do it.
And I wouldn't say what you learned was proportional to the time invested. Really studying C# and Windows programming in a systematic way would be much more efficient. And result in much better code in the end.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
yogi_bear_79 wrote: how to conver the string into a Label.
The (VBesque) answer to your question can be found in this[^] CP article.
I would have to agree with Luc that you may want to get a better grasp of the concepts behind objects and references; this might make it a lot easier for you to implement what you want to do.
/ravi
|
|
|
|
|
Suppose the sql is :
select empno, empname, deptname,sal from emp, dept where emp.did= dept.did
I need the following:
If the column name is empno replace it by eno,
If the table name is emp then replace it by employee,
If the table is dept then replace it by department,
if the column name is did replace it by deptid.
That means after operation my the sql should be as follows:
select eno, empname, deptname, sal from employee,department where employee.deptid=department.deptid
I will be greatfull if any body help me to complete this.
I got a parser but it only can parse the column name in select statement and table names. But I am not getting the column names in the join condition. After getting the column name or table name how can I replce those?
Suppose I got emp as table but if I replace all the emp by employee then empname is also converting to employeename which is becoming to wrong column name
|
|
|
|
|
Use a Dictionary<string,string> ? Populate it with the old and new values. Split the existing statement and iterate the parts. If a part exists in the dictionary retrieve its replacement. Build a new statement.
|
|
|
|
|
I got some idea. But do you know any god parsers name where i can easily find out the column names, table names, join tables names columns in join condition, column in where clause, column in group by clause and having clause.
|
|
|
|
|
How to change color of RibbonControl, i am using RibbonControl.dll.
yogesh
|
|
|
|
|
I am not familiar with RibbonControl.dll and cannot therefore answer your question. I would suggest that you will get a better response if you ask the author of the control.
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 would like to learn how to do for each. I have been researching. I also know this should be one of those no brainier questions but I am still piecing together how to make foreach for for me.
I know how to count my tables and fields then place the information in. But what I would like is to learn how to use the foreach command. to say something like.
Foreach (table in Database)
{
Foreach (column in table)
{
}
}
instead of counting the tables and columns and their names.
This will allow me to recreate a database from an existing one.
Any websites or information you can provide to point me in the right direction would be appreciated.
Just for reference purposes...
string sConnectString = "Driver={QODBC Driver for QuickBooks};DFQ=C:\\Quickbooks\\sample_company_file.qbw;OpenMode=M;OLE DB Services=-2;";
string sSQL = "SELECT Name FROM Employee";
OdbcConnection cn;
OdbcCommand cmd;
cn = new OdbcConnection(sConnectString);
cmd =new OdbcCommand(sSQL,cn);
try
{
cn.Open();
MessageBox.Show("open");
OdbcDataReader dr = cmd.ExecuteReader();
dr.GetSchemaTable().
MessageBox.Show(dr.GetSchemaTable().TableName);
}
catch (OdbcException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
cn.Close();
}
|
|
|
|
|
Not to poo-poo your question, but why are you writing code to do what sql already can?
Anyway - I'm not sure I actually get your question. If you're looking for the db/table/column structure info, you can use queries (which I'll inlcude at the end of this post) - but I'd try looking at the Interop.SQLDMO objects. Slow & a bit old-fasioned (I'm sure) but it works (I use it in a code generator I made) pretty well.
Aforementioned scripts:
-- select * From information_schema.tables -- (Tables)
-- select * From information_schema.columns -- (Columns)
-- Select * From syscolumns -- (another way to look at columns)
-- sp_fkeys([table] -- (fkeys for table)
-- SELECT FK.Table_name 'FKTableName', CU.column_name 'FKColumnName', pk.table_name 'PKTableName', pt.column_name 'PKColumnName',cu.table_catalog
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS C INNER JOIN
INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS FK ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME INNER JOIN
INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS PK ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME INNER JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS CU ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME INNER JOIN
(SELECT i1.TABLE_NAME, i2.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS i1 INNER JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS i2 ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME
WHERE
(i1.CONSTRAINT_TYPE = 'PRIMARY KEY')) AS PT ON PT.TABLE_NAME = PK.TABLE_NAME
where fk.table_name = '[table]' -- (Pkeys for table)
You're on your own with indexes and constraints (I'm not fiddling with that in my code generator).
Does that help, or did I miss what you're asking altogether?
|
|
|
|
|
I have a quickbooks database that I need to replicate into sql server.
The quickbooks database I have an ODBC connection to. But inorder to port the data from quickbook to sql server I have to generate the tables in sql server as they are in the quickbooks database. Then I can port the data from quickbooks to sql Server.
SQL Server has no odbc connection to quickbooks otherwise I agree with you.
I have found a piece of code that might help but I currently do not understand it.
http://stackoverflow.com/questions/47239/how-can-i-generate-database-tables-from-c-classes[^]
|
|
|
|
|
I have an application that looks for the next job to process by querying a que table. If it finds a record it does some processing for the corresponding job and then removes that record from the que table. I want to have this application running on more then one server but I'm not sure the best way to keep each instance of the application from trying to process the same record in the que table. Somehow when one instance of the application begins processing a record in the que table it needs to flag that record so that another instance of the application will not process it. What is the best way to do this?
|
|
|
|
|
If you return the info from your table and update the "Processing" flag from within a transaction, the other processes wont overlap due to l ocking caused by the transaction.
|
|
|
|
|
|
I want to know how can I do a random rename to all files in a specefic folder, for example, rename every file name from the current file.jpg to a GUID.jpg then it will be unique and random?
|
|
|
|
|
You've just asked, and answered, a question all in one go. 'grats.
|
|
|
|